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 junit.framework.*;
030:
031: import org.cougaar.core.service.community.CommunityService;
032: import org.cougaar.core.service.community.Community;
033: import org.cougaar.core.service.community.CommunityResponse;
034: import org.cougaar.core.service.community.CommunityResponseListener;
035:
036: import org.cougaar.community.CommunityImpl;
037: import org.cougaar.community.AgentImpl;
038: import org.cougaar.community.util.Semaphore;
039:
040: import javax.naming.directory.BasicAttributes;
041: import javax.naming.directory.BasicAttribute;
042: import javax.naming.directory.ModificationItem;
043: import javax.naming.directory.DirContext;
044: import javax.naming.NamingException;
045:
046: import java.util.*;
047:
048: /**
049: * Test basic CommunityService operations.
050: *
051: */
052: public class BasicTest extends TestBase {
053:
054: protected static final long TIMEOUT = 5000;
055: protected static final String AGENT = "Test_Agent";
056: protected static final String COMMUNITY = "Test_Community";
057: protected static final String SUBCOMMUNITY = "Test_SubCommunity";
058: protected CommunityService commSvc;
059: protected CommunityManagerTestImpl commMgr;
060: protected CommunityResponse commResp; // Callback response
061:
062: protected static final String loggingProps[][] = {
063: { "log4j.category.org.cougaar.community", "INFO" },
064: { "log4j.category.org.cougaar.community.test", "INFO" } };
065:
066: public BasicTest(String name) {
067: super (name, loggingProps);
068: }
069:
070: protected void setUp() {
071: commSvc = new CommunityServiceTestImpl(AGENT);
072: commMgr = CommunityManagerTestImpl.getInstance();
073: commMgr.reset();
074: commResp = null; // Clear response before each test
075: ((CommunityServiceTestImpl) commSvc).getCache().clear();
076: }
077:
078: public static Test suite() {
079: return new TestSuite(BasicTest.class);
080: /* Use following to run specific tests only
081: TestSuite suite= new TestSuite();
082: suite.addTest(new BasicTests("testGetParentCommunities"));
083: return suite;*/
084: }
085:
086: public static void main(String[] args) {
087: junit.textui.TestRunner.run(suite());
088: }
089:
090: /**
091: * Simple test of Join and Create community operation. A community is
092: * created and the requesting agent is added as a member.
093: */
094: public void testJoinCommunity() {
095: final Semaphore s = new Semaphore(0);
096: CommunityResponseListener crl = new CommunityResponseListener() {
097: public void getResponse(CommunityResponse resp) {
098: commResp = resp;
099: s.release();
100: }
101: };
102: //test one agent joins the community
103: try {
104: commSvc.joinCommunity(COMMUNITY, AGENT, commSvc.AGENT,
105: null, true, null, crl);
106: s.attempt(TIMEOUT);
107: } catch (Exception ex) {
108: ex.printStackTrace();
109: fail();
110: }
111: Community community = (Community) commResp.getContent();
112: assertTrue(commResp.getStatus() == CommunityResponse.SUCCESS
113: && community != null && community.hasEntity(AGENT));
114: }
115:
116: /**
117: * Simple test of leave community operation.
118: */
119: public void testLeaveCommunity() {
120: // Set test community to initial state using special backdoor in
121: // CommunityManagerTestImpl
122: Community comm = new CommunityImpl(COMMUNITY);
123: comm.addEntity(new AgentImpl(AGENT));
124: commMgr.addCommunity(comm);
125:
126: final Semaphore s = new Semaphore(0);
127: CommunityResponseListener crl = new CommunityResponseListener() {
128: public void getResponse(CommunityResponse resp) {
129: commResp = resp;
130: s.release();
131: }
132: };
133: //test remove one agent entity
134: try {
135: commSvc.leaveCommunity(COMMUNITY, AGENT, crl);
136: s.attempt(TIMEOUT);
137: } catch (Exception ex) {
138: ex.printStackTrace();
139: fail();
140: }
141: Community community = (Community) commResp.getContent();
142: assertTrue(commResp.getStatus() == CommunityResponse.SUCCESS
143: && community != null && !comm.hasEntity(AGENT));
144: }
145:
146: /**
147: * Simple test of create community operation.
148: */
149: public void testCreateCommunity() {
150: BasicAttributes attrs = new BasicAttributes();
151: attrs.put("Name", COMMUNITY);
152: attrs.put("CommunityType", "Robustness");
153:
154: final Semaphore s = new Semaphore(0);
155: CommunityResponseListener crl = new CommunityResponseListener() {
156: public void getResponse(CommunityResponse resp) {
157: commResp = resp;
158: s.release();
159: }
160: };
161: try {
162: commSvc.createCommunity(COMMUNITY, attrs, crl);
163: s.attempt(TIMEOUT);
164: } catch (Exception ex) {
165: ex.printStackTrace();
166: fail();
167: }
168: Community community = (Community) commResp.getContent();
169: assertTrue(commResp.getStatus() == CommunityResponse.SUCCESS
170: && community != null
171: && community.getName().equals(COMMUNITY)
172: && community.getAttributes().size() == 3);
173: }
174:
175: /**
176: * Simple test of get community operation.
177: */
178: public void testGetCommunity() {
179: Community comm = new CommunityImpl(COMMUNITY);
180: comm.addEntity(new AgentImpl(AGENT));
181: commMgr.addCommunity(comm);
182:
183: final Semaphore s = new Semaphore(0);
184: CommunityResponseListener crl = new CommunityResponseListener() {
185: public void getResponse(CommunityResponse resp) {
186: commResp = resp;
187: s.release();
188: }
189: };
190: Community community = null;
191: try {
192: community = commSvc.getCommunity(COMMUNITY, crl);
193: if (community != null) {
194: s.release();
195: }
196: s.attempt(TIMEOUT);
197: } catch (Exception ex) {
198: ex.printStackTrace();
199: fail();
200: }
201: if (commResp != null) {
202: community = (Community) commResp.getContent();
203: }
204: assertTrue(community != null
205: && community.getName().equals(COMMUNITY)
206: && community.getEntities().size() == 1); //the entity should be "Test_Agent".
207: }
208:
209: /**
210: * Simple test of modify entity attributes operation.
211: */
212: public void testModifyAttributes() {
213: BasicAttributes attrs = new BasicAttributes();
214: BasicAttribute attr = new BasicAttribute("EntityType", "Agent");
215: attrs.put(attr);
216: ModificationItem[] mods = new ModificationItem[1];
217:
218: CommunityImpl comm = new CommunityImpl(COMMUNITY);
219: AgentImpl agent = new AgentImpl(AGENT);
220: agent.setAttributes(attrs);
221: comm.addEntity(agent);
222: attrs.remove("EntityType");
223: attrs.put(new BasicAttribute("EntityType", "Community"));
224: CommunityImpl subcomm = new CommunityImpl(SUBCOMMUNITY);
225: subcomm.setAttributes(attrs);
226: comm.addEntity(subcomm);
227: commMgr.addCommunity(comm);
228:
229: //test add attribute to an agent entity
230: final Semaphore s1 = new Semaphore(0);
231: mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
232: new BasicAttribute("Role", "Member"));
233: try {
234: commSvc.modifyAttributes(COMMUNITY, AGENT, mods,
235: new CommunityResponseListener() {
236: public void getResponse(CommunityResponse resp) {
237: commResp = resp;
238: s1.release();
239: }
240: });
241: s1.attempt(TIMEOUT);
242: } catch (Exception ex) {
243: ex.printStackTrace();
244: fail();
245: }
246: Community community = (Community) commResp.getContent();
247: assertTrue(commResp.getStatus() == CommunityResponse.SUCCESS
248: && community != null
249: && community.getEntity(AGENT).getAttributes().size() == 2);
250:
251: //test modify attribute of an agent entity
252: final Semaphore s2 = new Semaphore(0);
253: mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
254: new BasicAttribute("Role", "Manager"));
255: try {
256: commSvc.modifyAttributes(COMMUNITY, AGENT, mods,
257: new CommunityResponseListener() {
258: public void getResponse(CommunityResponse resp) {
259: commResp = resp;
260: s2.release();
261: }
262: });
263: s2.attempt(TIMEOUT);
264: } catch (Exception ex) {
265: ex.printStackTrace();
266: fail();
267: }
268: community = (Community) commResp.getContent();
269: String value = "";
270: try {
271: value = (String) community.getEntity(AGENT).getAttributes()
272: .get("Role").get();
273: } catch (NamingException e) {
274: e.printStackTrace();
275: fail();
276: }
277: assertTrue(commResp.getStatus() == CommunityResponse.SUCCESS
278: && community != null && value.equals("Manager"));
279:
280: //test remove attribute from an agent entity
281: final Semaphore s3 = new Semaphore(0);
282: mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
283: new BasicAttribute("Role", "Manager"));
284: try {
285: commSvc.modifyAttributes(COMMUNITY, AGENT, mods,
286: new CommunityResponseListener() {
287: public void getResponse(CommunityResponse resp) {
288: commResp = resp;
289: s3.release();
290: }
291: });
292: s3.attempt(TIMEOUT);
293: } catch (Exception ex) {
294: ex.printStackTrace();
295: fail();
296: }
297: community = (Community) commResp.getContent();
298: assertTrue(commResp.getStatus() == CommunityResponse.SUCCESS
299: && community != null
300: && community.getEntity(AGENT).getAttributes().size() == 1);
301:
302: //test add attribute to a community entity
303: final Semaphore s4 = new Semaphore(0);
304: mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
305: new BasicAttribute("Manager", "Agent1"));
306: try {
307: commSvc.modifyAttributes(COMMUNITY, SUBCOMMUNITY, mods,
308: new CommunityResponseListener() {
309: public void getResponse(CommunityResponse resp) {
310: commResp = resp;
311: s4.release();
312: }
313: });
314: s4.attempt(TIMEOUT);
315: } catch (Exception ex) {
316: ex.printStackTrace();
317: fail();
318: }
319: community = (Community) commResp.getContent();
320: //System.out.println(community.toXml());
321: assertTrue(commResp.getStatus() == CommunityResponse.SUCCESS
322: && community != null
323: && community.getEntity(SUBCOMMUNITY).getAttributes()
324: .size() == 2);
325:
326: //test modify attribute of a community entity
327: final Semaphore s5 = new Semaphore(0);
328: mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
329: new BasicAttribute("Manager", "Agent2"));
330: try {
331: commSvc.modifyAttributes(COMMUNITY, SUBCOMMUNITY, mods,
332: new CommunityResponseListener() {
333: public void getResponse(CommunityResponse resp) {
334: commResp = resp;
335: s5.release();
336: }
337: });
338: s5.attempt(TIMEOUT);
339: } catch (Exception ex) {
340: ex.printStackTrace();
341: fail();
342: }
343: community = (Community) commResp.getContent();
344: value = "";
345: try {
346: value = (String) community.getEntity(SUBCOMMUNITY)
347: .getAttributes().get("Manager").get();
348: } catch (NamingException e) {
349: e.printStackTrace();
350: fail();
351: }
352: assertTrue(commResp.getStatus() == CommunityResponse.SUCCESS
353: && community != null && value.equals("Agent2"));
354:
355: //test remove attribute from a community entity
356: final Semaphore s6 = new Semaphore(0);
357: mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
358: new BasicAttribute("Manager", "Role2"));
359: try {
360: commSvc.modifyAttributes(COMMUNITY, SUBCOMMUNITY, mods,
361: new CommunityResponseListener() {
362: public void getResponse(CommunityResponse resp) {
363: commResp = resp;
364: s6.release();
365: }
366: });
367: s6.attempt(TIMEOUT);
368: } catch (Exception ex) {
369: ex.printStackTrace();
370: fail();
371: }
372: community = (Community) commResp.getContent();
373: assertTrue(commResp.getStatus() == CommunityResponse.SUCCESS
374: && community != null
375: && community.getEntity(SUBCOMMUNITY).getAttributes()
376: .size() == 1);
377:
378: }
379:
380: /**
381: * Test of list parent communities operation.
382: */
383: public void testListParentCommunities() {
384: CommunityImpl comm = new CommunityImpl(COMMUNITY);
385: comm.addEntity(new AgentImpl(AGENT));
386: commMgr.addCommunity(comm);
387:
388: //now agent Test_Agent only has one parent: Test_Community
389: Collection parents = null;
390: try {
391: parents = commSvc.listParentCommunities(AGENT,
392: (CommunityResponseListener) null);
393: } catch (Exception ex) {
394: ex.printStackTrace();
395: fail();
396: }
397: assertTrue(parents.size() == 1 && parents.contains(COMMUNITY));
398:
399: //add one more parent: MyTestCommunity, agent Test_agent should have two parents.
400: final Semaphore s1 = new Semaphore(0);
401: try {
402: commSvc.joinCommunity("MyTestCommunity", AGENT,
403: CommunityService.AGENT, null, true, null,
404: new CommunityResponseListener() {
405: public void getResponse(CommunityResponse resp) {
406: s1.release();
407: }
408: });
409: s1.acquire();
410: } catch (Exception ex) {
411: ex.printStackTrace();
412: fail();
413: }
414: try {
415: parents = commSvc.listParentCommunities(AGENT,
416: (CommunityResponseListener) null);
417: } catch (Exception ex) {
418: ex.printStackTrace();
419: fail();
420: }
421: assertTrue(parents.size() == 2 && parents.contains(COMMUNITY)
422: && parents.contains("MyTestCommunity"));
423:
424: //remove one parent.
425: final Semaphore s2 = new Semaphore(0);
426: try {
427: commSvc.leaveCommunity("MyTestCommunity", AGENT,
428: new CommunityResponseListener() {
429: public void getResponse(CommunityResponse resp) {
430: s2.release();
431: }
432: });
433: s2.acquire();
434: } catch (Exception ex) {
435: ex.printStackTrace();
436: fail();
437: }
438: parents = commSvc.listParentCommunities(AGENT,
439: (CommunityResponseListener) null);
440: //System.out.println("parents=" + parents);
441: assertTrue(parents.size() == 1 && parents.contains(COMMUNITY));
442:
443: }
444:
445: public void testSearchCommunity() {
446: CommunityImpl comm = new CommunityImpl(COMMUNITY);
447: BasicAttributes attrs = new BasicAttributes();
448: attrs.put("Role", "Member");
449: attrs.put("TestAttribute", "test");
450: comm.addEntity(new AgentImpl(AGENT, attrs));
451: commMgr.addCommunity(comm);
452:
453: Collection results = null;
454: try {
455: results = commSvc
456: .searchCommunity(COMMUNITY, "(Role=Member)", true,
457: Community.ALL_ENTITIES, null);
458: } catch (Exception ex) {
459: ex.printStackTrace();
460: fail();
461: }
462: assertTrue(results != null && results.size() == 1);
463: }
464:
465: //test those deprecated methods
466: public void testGetParentCommunities() {
467: CommunityImpl comm = new CommunityImpl(COMMUNITY);
468: comm.addEntity(new CommunityImpl(SUBCOMMUNITY));
469: commMgr.addCommunity(comm);
470:
471: CommunityImpl subcomm = new CommunityImpl(SUBCOMMUNITY);
472: subcomm.addEntity(new AgentImpl(AGENT));
473: commMgr.addCommunity(subcomm);
474:
475: String[] parents = commSvc.getParentCommunities(false);
476: assertTrue(parents.length == 1
477: && parents[0].equals(SUBCOMMUNITY));
478: parents = commSvc.getParentCommunities(true);
479: assertTrue(parents.length == 2);
480: }
481:
482: public void testListParentCommunitiesI() {
483: CommunityImpl comm = new CommunityImpl(COMMUNITY);
484: comm.addEntity(new CommunityImpl(SUBCOMMUNITY));
485: commMgr.addCommunity(comm);
486:
487: CommunityImpl subcomm = new CommunityImpl(SUBCOMMUNITY);
488: subcomm.addEntity(new AgentImpl(AGENT));
489: commMgr.addCommunity(subcomm);
490:
491: Collection parents = commSvc.listParentCommunities(AGENT);
492: assertTrue(parents.size() == 1
493: && parents.contains(SUBCOMMUNITY));
494:
495: parents = commSvc.listParentCommunities(SUBCOMMUNITY);
496: assertTrue(parents.size() == 1 && parents.contains(COMMUNITY));
497: }
498:
499: public void testListParentCommunitiesII() {
500: CommunityImpl comm = new CommunityImpl(COMMUNITY);
501: BasicAttributes attrs = new BasicAttributes();
502: attrs.put(new BasicAttribute("CommunityType", "Robustness"));
503: comm.setAttributes(attrs);
504: comm.addEntity(new AgentImpl(AGENT));
505: commMgr.addCommunity(comm);
506:
507: Collection parents = commSvc.listParentCommunities(AGENT,
508: "(CommunityType=Robustness)");
509: assertTrue(parents.size() == 1 && parents.contains(COMMUNITY));
510: parents = commSvc.listParentCommunities(AGENT,
511: "(CommunityType=Security)");
512: assertTrue(parents.size() == 0);
513: }
514:
515: public void testListAllCommunities() {
516: CommunityImpl comm = new CommunityImpl(COMMUNITY);
517: comm.addEntity(new CommunityImpl(SUBCOMMUNITY));
518: commMgr.addCommunity(comm);
519:
520: CommunityImpl subcomm = new CommunityImpl(SUBCOMMUNITY);
521: subcomm.addEntity(new AgentImpl(AGENT));
522: commMgr.addCommunity(subcomm);
523:
524: Collection comms = commSvc.listAllCommunities();
525: //System.out.println("allCommunities=" + comms);
526: assertTrue(comms.size() == 2 && comms.contains(COMMUNITY)
527: && comms.contains(SUBCOMMUNITY));
528: }
529:
530: public void testCloneCommunity() {
531: BasicAttributes attrs = new BasicAttributes();
532: attrs.put(new BasicAttribute("id", "val"));
533: CommunityImpl comm = new CommunityImpl(COMMUNITY, attrs);
534: comm.addEntity(new AgentImpl(AGENT));
535: comm.addEntity(new CommunityImpl(SUBCOMMUNITY));
536:
537: Community clone = (Community) comm.clone();
538:
539: try {
540: assertTrue(clone.getName().equals(COMMUNITY)
541: && clone.getName() != comm.getName()
542: && clone.getAttributes().get("id").contains("val")
543: && clone.getAttributes() != comm.getAttributes()
544: && clone.getAttributes().get("id") != comm
545: .getAttributes().get("id")
546: && clone.getAttributes().get("id").get() != comm
547: .getAttributes().get("id").get()
548: && clone.getEntities().size() == 2
549: && clone.getEntities() != comm.getEntities()
550: && clone.getEntity(AGENT) != comm.getEntity(AGENT)
551: && clone.getEntity(AGENT).equals(
552: comm.getEntity(AGENT))
553: && clone.getEntity(SUBCOMMUNITY) != comm
554: .getEntity(SUBCOMMUNITY)
555: && clone.getEntity(SUBCOMMUNITY).equals(
556: comm.getEntity(SUBCOMMUNITY)));
557: } catch (Exception ex) {
558: fail();
559: }
560: }
561:
562: }
|