001: /*
002: * <copyright>
003: *
004: * Copyright 2001-2004 Mobile Intelligence Corp
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.community.test;
028:
029: import java.util.Properties;
030: import java.util.Collection;
031: import java.util.Iterator;
032:
033: import junit.framework.*;
034:
035: import javax.naming.directory.Attributes;
036: import javax.naming.directory.Attribute;
037: import javax.naming.directory.BasicAttribute;
038: import javax.naming.directory.BasicAttributes;
039:
040: import org.cougaar.core.service.community.Entity;
041: import org.cougaar.core.service.community.CommunityService;
042: import org.cougaar.core.service.community.Community;
043: import org.cougaar.core.service.community.CommunityResponse;
044: import org.cougaar.core.service.community.CommunityResponseListener;
045:
046: import org.cougaar.community.CommunityImpl;
047: import org.cougaar.community.AgentImpl;
048: import org.cougaar.community.util.Semaphore;
049:
050: /**
051: * Test community search operations.
052: */
053: public class SearchTest extends TestBase {
054:
055: protected static final String AGENT = "Test_Agent";
056: protected static final String COMMUNITY = "Test_Community";
057: protected static final String NESTED_COMMUNITY = "Nested_Community";
058: protected CommunityService commSvc;
059: protected CommunityManagerTestImpl commMgr;
060: protected CommunityResponse topCommResp;
061: protected CommunityResponse nestedCommResp;
062: protected Collection searchResults;
063:
064: protected static final String loggingProps[][] = {
065: { "log4j.category.org.cougaar.community", "INFO" },
066: { "log4j.category.org.cougaar.community.test", "INFO" } };
067:
068: public SearchTest(String name) {
069: super (name, loggingProps);
070: }
071:
072: protected void setUp() {
073: commSvc = new CommunityServiceTestImpl(AGENT);
074: commMgr = CommunityManagerTestImpl.getInstance();
075: commMgr.reset();
076: topCommResp = null; // Clear response before each test
077: nestedCommResp = null;
078: ((CommunityServiceTestImpl) commSvc).getCache().clear();
079: searchResults = null;
080: }
081:
082: public static Test suite() {
083: return new TestSuite(SearchTest.class);
084: /* Use following to run specific tests only
085: TestSuite suite= new TestSuite();
086: suite.addTest(new SearchTests("testAddEntity"));
087: return suite;
088: */
089: }
090:
091: /**
092: * Basic search.
093: */
094: public void testBasicSearch() {
095: // Setup test state
096: Community comm = new CommunityImpl(COMMUNITY);
097: comm.addEntity(new AgentImpl("Agent1", new BasicAttributes(
098: "Attr1", "Val1")));
099: comm.addEntity(new AgentImpl("Agent2", new BasicAttributes(
100: "Attr2", "Val2")));
101: comm.addEntity(new AgentImpl("Agent3", new BasicAttributes(
102: "Attr3", "Val3")));
103: commMgr.addCommunity(comm);
104:
105: final Semaphore s = new Semaphore(0);
106: try {
107: searchResults = commSvc.searchCommunity(COMMUNITY,
108: "(Attr2=Val2)",
109: false, // Flat search
110: Community.ALL_ENTITIES,
111: new CommunityResponseListener() {
112: public void getResponse(CommunityResponse resp) {
113: if (resp.getStatus() == CommunityResponse.SUCCESS) {
114: searchResults = (Collection) resp
115: .getContent();
116: }
117: s.release();
118: }
119: });
120: if (searchResults != null) {
121: s.release();
122: }
123: s.attempt(5000);
124: } catch (Exception ex) {
125: ex.printStackTrace();
126: fail();
127: }
128: assertTrue(searchResults != null && searchResults.size() == 1
129: && resultsContains(searchResults, "Agent2"));
130: }
131:
132: /**
133: * Compound search.
134: */
135: public void testCompoundSearch() {
136: // Setup test state
137: Community comm = new CommunityImpl(COMMUNITY);
138: comm.addEntity(new AgentImpl("Agent1", new BasicAttributes(
139: "Attr1", "Val1")));
140: comm.addEntity(new AgentImpl("Agent2", new BasicAttributes(
141: "Attr2", "Val2")));
142:
143: Attributes agent3Attrs = new BasicAttributes();
144: agent3Attrs.put("Attr3", "Val3");
145: agent3Attrs.put("Attr4", "Val4");
146: comm.addEntity(new AgentImpl("Agent3", agent3Attrs));
147: Attributes agent4Attrs = new BasicAttributes();
148: agent4Attrs.put("Attr3", "Val3");
149: agent4Attrs.put("Attr4", "Val4");
150: agent4Attrs.put("Attr5", "Val5");
151: comm.addEntity(new AgentImpl("Agent4", agent4Attrs));
152: commMgr.addCommunity(comm);
153:
154: final Semaphore s = new Semaphore(0);
155: //search for entities have both attributes: Attr3=Val3 and Attr4=Val4, get two results
156: try {
157: searchResults = commSvc.searchCommunity(COMMUNITY,
158: "(&(Attr3=Val3)(Attr4=Val4))",
159: false, // Flat search
160: Community.ALL_ENTITIES,
161: new CommunityResponseListener() {
162: public void getResponse(CommunityResponse resp) {
163: if (resp.getStatus() == CommunityResponse.SUCCESS) {
164: searchResults = (Collection) resp
165: .getContent();
166: }
167: s.release();
168: }
169: });
170: if (searchResults != null) {
171: s.release();
172: }
173: s.attempt(5000);
174: } catch (Exception ex) {
175: ex.printStackTrace();
176: fail();
177: }
178: assertTrue(searchResults != null && searchResults.size() == 2
179: && resultsContains(searchResults, "Agent3")
180: && resultsContains(searchResults, "Agent4"));
181:
182: //search for entities have both attributes: Attr3=Val3 and Attr5=Val5, get one result.
183: try {
184: searchResults = commSvc.searchCommunity(COMMUNITY,
185: "(&(Attr3=Val3)(Attr5=Val5))",
186: false, // Flat search
187: Community.ALL_ENTITIES,
188: new CommunityResponseListener() {
189: public void getResponse(CommunityResponse resp) {
190: if (resp.getStatus() == CommunityResponse.SUCCESS) {
191: searchResults = (Collection) resp
192: .getContent();
193: }
194: s.release();
195: }
196: });
197: if (searchResults != null) {
198: s.release();
199: }
200: s.attempt(5000);
201: } catch (Exception ex) {
202: ex.printStackTrace();
203: fail();
204: }
205: assertTrue(searchResults != null && searchResults.size() == 1
206: && resultsContains(searchResults, "Agent4"));
207:
208: }
209:
210: /**
211: * Alternative search
212: */
213: public void testAlternativeSearch() {
214: // Setup test state
215: Community comm = new CommunityImpl(COMMUNITY);
216: comm.addEntity(new AgentImpl("Agent1", new BasicAttributes(
217: "Attr1", "Val1")));
218: comm.addEntity(new AgentImpl("Agent2", new BasicAttributes(
219: "Attr2", "Val2")));
220:
221: Attributes agent3Attrs = new BasicAttributes();
222: agent3Attrs.put("Attr3", "Val3");
223: agent3Attrs.put("Attr4", "Val4");
224: agent3Attrs.put("Attr5", "Val5");
225: comm.addEntity(new AgentImpl("Agent3", agent3Attrs));
226: Attributes agent4Attrs = new BasicAttributes();
227: agent4Attrs.put("Attr5", "Val5");
228: agent4Attrs.put("Attr6", "Val6");
229: comm.addEntity(new AgentImpl("Agent4", agent4Attrs));
230: commMgr.addCommunity(comm);
231:
232: final Semaphore s = new Semaphore(0);
233: //search result only get one entity
234: try {
235: searchResults = commSvc.searchCommunity(COMMUNITY,
236: "(|(Attr3=Val3)(Attr4=Val4))",
237: false, // Flat search
238: Community.ALL_ENTITIES,
239: new CommunityResponseListener() {
240: public void getResponse(CommunityResponse resp) {
241: if (resp.getStatus() == CommunityResponse.SUCCESS) {
242: searchResults = (Collection) resp
243: .getContent();
244: }
245: s.release();
246: }
247: });
248: if (searchResults != null) {
249: s.release();
250: }
251: s.attempt(5000);
252: } catch (Exception ex) {
253: ex.printStackTrace();
254: fail();
255: }
256: assertTrue(searchResults != null && searchResults.size() == 1
257: && resultsContains(searchResults, "Agent3"));
258:
259: //search result contains two entities
260: try {
261: searchResults = commSvc.searchCommunity(COMMUNITY,
262: "(|(Attr3=Val3)(Attr5=Val5))",
263: false, // Flat search
264: Community.ALL_ENTITIES,
265: new CommunityResponseListener() {
266: public void getResponse(CommunityResponse resp) {
267: if (resp.getStatus() == CommunityResponse.SUCCESS) {
268: searchResults = (Collection) resp
269: .getContent();
270: }
271: s.release();
272: }
273: });
274: if (searchResults != null) {
275: s.release();
276: }
277: s.attempt(5000);
278: } catch (Exception ex) {
279: ex.printStackTrace();
280: fail();
281: }
282: assertTrue(searchResults != null && searchResults.size() == 2
283: && resultsContains(searchResults, "Agent3")
284: && resultsContains(searchResults, "Agent4"));
285: }
286:
287: /**
288: * Negative search
289: */
290: public void testNegationSearch() {
291: // Setup test state
292: Community comm = new CommunityImpl(COMMUNITY);
293: comm.addEntity(new AgentImpl("Agent1", new BasicAttributes(
294: "Attr1", "Val1")));
295: comm.addEntity(new AgentImpl("Agent2", new BasicAttributes(
296: "Attr2", "Val2")));
297:
298: Attributes agent3Attrs = new BasicAttributes();
299: agent3Attrs.put("Attr3", "Val3");
300: agent3Attrs.put("Attr4", "Val4");
301: comm.addEntity(new AgentImpl("Agent3", agent3Attrs));
302: commMgr.addCommunity(comm);
303:
304: final Semaphore s = new Semaphore(0);
305: try {
306: searchResults = commSvc.searchCommunity(COMMUNITY,
307: "(!(Attr3=Val3))",
308: false, // Flat search
309: Community.ALL_ENTITIES,
310: new CommunityResponseListener() {
311: public void getResponse(CommunityResponse resp) {
312: if (resp.getStatus() == CommunityResponse.SUCCESS) {
313: searchResults = (Collection) resp
314: .getContent();
315: }
316: s.release();
317: }
318: });
319: if (searchResults != null) {
320: s.release();
321: }
322: s.attempt(5000);
323: } catch (Exception ex) {
324: ex.printStackTrace();
325: fail();
326: }
327: assertTrue(searchResults != null && searchResults.size() == 2
328: && resultsContains(searchResults, "Agent1")
329: && resultsContains(searchResults, "Agent2"));
330:
331: try {
332: searchResults = commSvc.searchCommunity(COMMUNITY,
333: "(!(Attr3=Val4))",
334: false, // Flat search
335: Community.ALL_ENTITIES,
336: new CommunityResponseListener() {
337: public void getResponse(CommunityResponse resp) {
338: if (resp.getStatus() == CommunityResponse.SUCCESS) {
339: searchResults = (Collection) resp
340: .getContent();
341: }
342: s.release();
343: }
344: });
345: if (searchResults != null) {
346: s.release();
347: }
348: s.attempt(5000);
349: } catch (Exception ex) {
350: ex.printStackTrace();
351: fail();
352: }
353: assertTrue(searchResults != null && searchResults.size() == 3
354: && resultsContains(searchResults, "Agent1")
355: && resultsContains(searchResults, "Agent2")
356: && resultsContains(searchResults, "Agent3"));
357:
358: }
359:
360: /**
361: * Wildcard search
362: */
363: public void testWildcardSearch() {
364: // Setup test state
365: Community comm = new CommunityImpl(COMMUNITY);
366: comm.addEntity(new AgentImpl("Agent1", new BasicAttributes(
367: "Attr1", "Val1")));
368: comm.addEntity(new AgentImpl("Agent2", new BasicAttributes(
369: "Attr2", "Val2")));
370:
371: Attributes agent3Attrs = new BasicAttributes();
372: agent3Attrs.put("Attr3", "Val3");
373: agent3Attrs.put("Attr4", "Val4");
374: comm.addEntity(new AgentImpl("Agent3", agent3Attrs));
375: comm.addEntity(new AgentImpl("Agent4", new BasicAttributes(
376: "Attr3", "Val4")));
377: commMgr.addCommunity(comm);
378:
379: final Semaphore s = new Semaphore(0);
380: //serch for all entities who have the attribute Attr3.
381: try {
382: searchResults = commSvc.searchCommunity(COMMUNITY,
383: "(Attr3=*)",
384: false, // Flat search
385: Community.ALL_ENTITIES,
386: new CommunityResponseListener() {
387: public void getResponse(CommunityResponse resp) {
388: if (resp.getStatus() == CommunityResponse.SUCCESS) {
389: searchResults = (Collection) resp
390: .getContent();
391: }
392: s.release();
393: }
394: });
395: if (searchResults != null) {
396: s.release();
397: }
398: s.attempt(5000);
399: } catch (Exception ex) {
400: ex.printStackTrace();
401: fail();
402: }
403: assertTrue(searchResults != null && searchResults.size() == 2
404: && resultsContains(searchResults, "Agent3")
405: && resultsContains(searchResults, "Agent4"));
406:
407: //search for all entities who have attribute Attr3 and the value contains a 3.
408: try {
409: searchResults = commSvc.searchCommunity(COMMUNITY,
410: "(Attr3=*3)",
411: false, // Flat search
412: Community.ALL_ENTITIES,
413: new CommunityResponseListener() {
414: public void getResponse(CommunityResponse resp) {
415: if (resp.getStatus() == CommunityResponse.SUCCESS) {
416: searchResults = (Collection) resp
417: .getContent();
418: }
419: s.release();
420: }
421: });
422: if (searchResults != null) {
423: s.release();
424: }
425: s.attempt(5000);
426: } catch (Exception ex) {
427: ex.printStackTrace();
428: fail();
429: }
430: assertTrue(searchResults != null && searchResults.size() == 1
431: && resultsContains(searchResults, "Agent3"));
432:
433: }
434:
435: /**
436: * Multi-valued attribute search.
437: */
438: public void testMultiValuedAttributeSearch() {
439: // Setup test state
440: Community comm = new CommunityImpl(COMMUNITY);
441: comm.addEntity(new AgentImpl("Agent1", new BasicAttributes(
442: "Attr1", "Val1")));
443: comm.addEntity(new AgentImpl("Agent2", new BasicAttributes(
444: "Attr2", "Val2")));
445:
446: Attribute attr = new BasicAttribute("Attr3");
447: attr.add("Val3");
448: attr.add("Val4");
449: Attributes attrs = new BasicAttributes();
450: attrs.put(attr);
451: comm.addEntity(new AgentImpl("Agent3", attrs));
452: commMgr.addCommunity(comm);
453:
454: final Semaphore s = new Semaphore(0);
455: try {
456: searchResults = commSvc.searchCommunity(COMMUNITY,
457: "(Attr3=Val4)",
458: false, // Flat search
459: Community.ALL_ENTITIES,
460: new CommunityResponseListener() {
461: public void getResponse(CommunityResponse resp) {
462: if (resp.getStatus() == CommunityResponse.SUCCESS) {
463: searchResults = (Collection) resp
464: .getContent();
465: }
466: s.release();
467: }
468: });
469: if (searchResults != null) {
470: s.release();
471: }
472: s.attempt(5000);
473: } catch (Exception ex) {
474: ex.printStackTrace();
475: fail();
476: }
477: assertTrue(searchResults != null && searchResults.size() == 1
478: && resultsContains(searchResults, "Agent3"));
479: }
480:
481: public void testCombinationSearch() {
482: // Setup test state
483: Community comm = new CommunityImpl(COMMUNITY);
484: comm.addEntity(new AgentImpl("Agent1", new BasicAttributes(
485: "Attr1", "Val1")));
486: comm.addEntity(new AgentImpl("Agent2", new BasicAttributes(
487: "Attr2", "Val2")));
488:
489: Attributes agent3Attrs = new BasicAttributes();
490: agent3Attrs.put("Attr3", "Val3");
491: agent3Attrs.put("Attr4", "Val4");
492: comm.addEntity(new AgentImpl("Agent3", agent3Attrs));
493: Attribute attr = new BasicAttribute("Attr3");
494: attr.add("Val3");
495: attr.add("Val4");
496: Attributes agent4Attrs = new BasicAttributes();
497: agent4Attrs.put(attr);
498: comm.addEntity(new AgentImpl("Agent4", agent4Attrs));
499: commMgr.addCommunity(comm);
500:
501: final Semaphore s = new Semaphore(0);
502: try {
503: searchResults = commSvc.searchCommunity(
504: COMMUNITY,
505: "(|(Attr4=*)(&(Attr1=Val1)(!(Attr3=Val3))))",
506: false, // Flat search
507: Community.ALL_ENTITIES,
508: new CommunityResponseListener() {
509: public void getResponse(CommunityResponse resp) {
510: if (resp.getStatus() == CommunityResponse.SUCCESS) {
511: searchResults = (Collection) resp
512: .getContent();
513: }
514: s.release();
515: }
516: });
517: if (searchResults != null) {
518: s.release();
519: }
520: s.attempt(5000);
521: } catch (Exception ex) {
522: ex.printStackTrace();
523: fail();
524: }
525: assertTrue(searchResults != null && searchResults.size() == 2
526: && resultsContains(searchResults, "Agent1")
527: && resultsContains(searchResults, "Agent3"));
528:
529: }
530:
531: private boolean resultsContains(Collection results,
532: String entityName) {
533: if (results != null) {
534: for (Iterator it = results.iterator(); it.hasNext();) {
535: Entity entity = (Entity) it.next();
536: if (entity.getName().equals(entityName))
537: return true;
538: }
539: }
540: return false;
541: }
542: }
|