1   
2   
3   
4   
5   
6   
7   
8   package gov.noaa.gdsg.xmldbremote.xmldbClient;
9   
10  import junit.framework.*;
11  import org.xmldb.api.base.Collection;
12  import org.xmldb.api.base.XMLDBException;
13  import org.xmldb.api.base.Resource;
14  import org.xmldb.api.base.Service;
15  import org.xmldb.api.base.ErrorCodes;
16  import org.xmldb.api.modules.XMLResource;
17  
18  import java.util.Map;
19  import java.util.HashMap;
20  
21  /***
22   *
23   * @author tns
24   */
25  public class XmldbClientCollectionTest extends TestCase {
26      
27      private Collection collection = null;
28      private String endPointUrl = "http://localhost:8080/mm/services/nmmrdb";
29      private String collectionUri = "xmldb:nmmr://www.ngdc.noaa.gov/nmmrdb/NGDC";
30      private String collectionName = "NGDC";
31      private String collectionUserName = "guest";
32      private String collectionPassword = "guest";
33      private String childCollection = null;
34      private String testRecord = "test_record.xml";
35      
36      public XmldbClientCollectionTest(java.lang.String testName) {
37          super(testName);
38      }
39      
40      public static Test suite() {
41          TestSuite suite = new TestSuite(XmldbClientCollectionTest.class);
42          return suite;
43      }
44      
45      protected void setUp() throws Exception {
46          try {
47              
48              XmldbClientDatabase db = new XmldbClientDatabase();
49              db.setEndPointUrl(endPointUrl);
50              db.init();
51              this.collection = db.getCollection(collectionUri, collectionUserName, collectionPassword);
52          } catch (XMLDBException e){
53              e.printStackTrace();
54              throw new Exception(e);
55          }
56      }
57      
58      protected void tearDown() throws Exception {
59          this.collection.close();
60          this.collection = null;
61      }
62      
63      /*** Test of close method, of class gov.noaa.mm.xmldbImpl.BMSCollection. */
64      public void testClose() throws Exception {
65          System.out.println("testClose");
66          
67          
68          this.collection.close();
69      }
70      
71      /*** Test of createId method, of class gov.noaa.mm.xmldbImpl.BMSCollection. */
72      public void testCreateId() throws Exception {
73          System.out.println("testCreateId");
74          String id = this.collection.createId();
75          
76      }
77      
78      /*** Test of createResource method, of class gov.noaa.mm.xmldbImpl.BMSCollection. */
79      public void testCreateResource() throws Exception {
80          System.out.println("testCreateResource");
81          
82          
83          String id = this.collection.createId();
84          Resource resource = this.collection.createResource(id, "XMLResource");
85          assertNotNull("Resource not created, null returned",resource);
86      }
87      
88      /*** Test of getChildCollection method, of class gov.noaa.mm.xmldbImpl.BMSCollection. */
89      public void testGetChildCollection() throws Exception {
90          System.out.println("testGetChildCollection");
91          
92          
93          Collection childCollection = this.collection.getChildCollection(this.childCollection);
94      }
95      
96      /*** Test of getChildCollectionCount method, of class gov.noaa.mm.xmldbImpl.BMSCollection. */
97      public void testGetChildCollectionCount() throws Exception {
98          System.out.println("testGetChildCollectionCount");
99          
100         
101         int count = this.collection.getChildCollectionCount();
102     }
103     
104     /*** Test of getName method, of class gov.noaa.mm.xmldbImpl.BMSCollection. */
105     public void testGetName()  throws Exception {
106         System.out.println("testGetName");
107         
108         
109         String name = this.collection.getName();
110         assertNotNull("Collection name is null",name);
111         assertEquals(name,this.collectionName);
112     }
113     
114     /*** Test of getParentCollection method, of class gov.noaa.mm.xmldbImpl.BMSCollection. */
115     public void testGetParentCollection()  throws Exception {
116         System.out.println("testGetParentCollection");
117         
118         
119         Collection parent = this.collection.getParentCollection();
120     }
121     
122     /*** Test of getProperty method, of class gov.noaa.mm.xmldbImpl.BMSCollection. */
123     public void testGetProperty()  throws Exception {
124         System.out.println("testGetProperty");
125         
126         
127         String result = this.collection.getProperty("");
128     }
129     
130     /*** Test of getResource method, of class gov.noaa.mm.xmldbImpl.BMSCollection. */
131     public void testGetResource()  throws Exception {
132         System.out.println("testGetResource");
133         
134         
135         String[] resources = this.collection.listResources();
136         if (resources.length == 0){
137             fail("No resources in the collection.");
138         }
139         Resource resource = this.collection.getResource(resources[0]);
140         assertNotNull("Resource returned is null, the resource requested is:" + resources[0],resource);
141     }
142     
143     /*** Test of getResourceCount method, of class gov.noaa.mm.xmldbImpl.BMSCollection. */
144     public void testGetResourceCount()  throws Exception {
145         System.out.println("testGetResourceCount");
146         
147         
148         int count = this.collection.getResourceCount();
149         String[] ids = this.collection.listResources();
150         assertNotNull("listResources returned null ids",ids);
151         assertEquals("resource count and list resources should be the same size",count,ids.length);
152     }
153     
154     /*** Test of getService method, of class gov.noaa.mm.xmldbImpl.BMSCollection. */
155     public void testGetService()  throws Exception {
156         System.out.println("testGetService");
157         
158         
159         Service[] services = this.collection.getServices();
160         assertNotNull("get services returned null",services);
161         assertTrue("No services to check",services.length != 0);
162         for (int i = 0; i < services.length; i++){
163             String name = services[i].getName();
164             assertNotNull("Null name",name);
165             String version = services[i].getVersion();
166             assertNotNull("Null versioin",version);
167             Service service = this.collection.getService(name, version);
168             assertNotNull("Request for service with name:" + name + " version:" + version
169             + " returned null", service);
170             
171             
172             String serviceName = service.getName();
173             assertNotNull("Retrived service name is null",serviceName);
174             String serviceVersion = service.getVersion();
175             assertNotNull("Retrives service version is null",serviceVersion);
176             boolean equals = serviceName.equals(name) && serviceVersion.equals(version);
177             
178             
179             assertTrue("Retrived service different then requested service, "
180             + " retrived service name:" + serviceName + " version:" + serviceVersion
181             + " requested service name:" + name + " version:" + version, equals);
182         }
183     }
184     
185     public void testGetDummyService() throws Exception {
186         System.out.println("testGetDummyService");
187         
188         
189         try {
190             Service service = this.collection.getService("lakjfjdljaljdlfdlkadlfad", ";aldflajdkfdkfalaf;lajdla");
191             fail("Service request did not throw a NO_SUCH_SERVICE exception");
192         } catch (XMLDBException expected){
193             
194         }
195         
196         
197     }
198     
199     
200     /*** Test of getServices method, of class gov.noaa.mm.xmldbImpl.BMSCollection. */
201     public void testGetServices()  throws Exception {
202         System.out.println("testGetServices");
203         
204         
205         Service[] services = this.collection.getServices();
206         assertNotNull("Services returned",services);
207         for (int i = 0; i < services.length; i++){
208             assertNotNull("Service in service array",services[i]);
209             System.out.println("Service returned:" + services[i].getName() + "," + services[i].getVersion());
210         }
211     }
212     
213     /*** Test of isOpen method, of class gov.noaa.mm.xmldbImpl.BMSCollection. */
214     public void testIsOpen()  throws Exception {
215         System.out.println("testIsOpen");
216         
217         
218         this.collection.isOpen();
219     }
220     
221     /*** Test of listChildCollections method, of class gov.noaa.mm.xmldbImpl.BMSCollection. */
222     public void testListChildCollections()  throws Exception {
223         System.out.println("testListChildCollections");
224         
225         
226         this.collection.listChildCollections();
227     }
228     
229     /*** Test of listResources method, of class gov.noaa.mm.xmldbImpl.BMSCollection. */
230     public void testListResources()  throws Exception {
231         System.out.println("testListResources");
232         
233         
234         String[] resources = this.collection.listResources();
235         assertNotNull(resources);
236         assertTrue("listResources returned 0 resources",  resources.length > 0);
237         for (int i = 0; i < resources.length; i++){
238             assertNotNull("The index of " + i + " in the list of resources contains a null resource",resources[i]);
239         }
240     }
241     
242     /*** Test of removeResource method, of class gov.noaa.mm.xmldbImpl.BMSCollection. */
243     public void testRemoveResource()  throws Exception {
244         System.out.println("testRemoveResource");
245        
246         int beginningCount = this.collection.getResourceCount();
247         
248         String[] resources = this.collection.listResources();
249         assertNotNull(resources);
250         assertTrue("NO resources to remove",0 != resources.length);
251         Resource resource = this.collection.getResource(resources[0]);
252         assertNotNull("Requested resource not available", resource);
253         this.collection.removeResource(resource);
254         assertEquals("ending count should be one less than beginning count",
255         beginningCount, this.collection.getResourceCount() + 1);
256     }
257     
258     /*** Test of setProperty method, of class gov.noaa.mm.xmldbImpl.BMSCollection. */
259     public void testSetProperty() throws Exception  {
260         System.out.println("testSetProperty");
261         
262         
263         this.collection.setProperty("prop","val");
264     }
265     
266     private org.w3c.dom.Node loadTestRecord() throws Exception {
267         java.io.InputStream is = this.getClass().getResourceAsStream(this.testRecord);
268         if (is == null){
269             throw new NullPointerException("Input Stream not found for test record:" 
270             + this.testRecord);
271         }
272             javax.xml.parsers.DocumentBuilderFactory factory =
273                 new org.apache.xerces.jaxp.DocumentBuilderFactoryImpl();
274         javax.xml.parsers.DocumentBuilder builder = 
275             factory.newDocumentBuilder();
276         return builder.parse(is);
277     }
278     
279     /*** Test of storeResource method, of class gov.noaa.mm.xmldbImpl.BMSCollection. */
280     public void testStoreNewResource()  throws Exception {
281         System.out.println("testStoreNewResource");
282         
283         int beginningCount = this.collection.getResourceCount();
284         
285         
286         String id = this.collection.createId();
287         XMLResource resource = (XMLResource) this.collection.createResource(id, "XMLResource");
288         assertNotNull("Resource not created, null returned",resource);
289         org.w3c.dom.Node testRecord = this.loadTestRecord();
290         resource.setContentAsDOM(testRecord);
291         this.collection.storeResource(resource);
292         
293         int endingCount = this.collection.getResourceCount();
294         assertEquals("Ending count should have been one more than beginning count",
295         beginningCount + 1, endingCount);
296         
297     }
298     
299     /*** Test of storeResource method, of class gov.noaa.mm.xmldbImpl.BMSCollection. */
300     public void testStoreUpdatedResource()  throws Exception {
301         System.out.println("testUpdatedStoreResource");
302         
303         
304         int beginningCount = this.collection.getResourceCount();
305         
306         String resources[] = this.collection.listResources();
307         assertNotNull("listResources returned null");
308         this.assertTrue("no resources in collection", resources.length > 0);
309         Resource resource = this.collection.getResource(resources[0]);
310         assertNotNull("Resource was null", resource);
311         if (resource instanceof XMLResource){
312             XMLResource xResource = (XMLResource) resource;
313             xResource.setContentAsDOM(this.loadTestRecord());
314             this.collection.storeResource(xResource);
315         }
316         else {
317             fail("Only XMLResource can currently be tested");
318         }
319         
320         
321         
322         
323     }
324     
325     
326     
327     
328     
329     
330     
331     
332     
333 }