Add testing for Service
diff --git a/JPA-RS Incubator/JPA-RS/src/org/eclipse/persistence/jpa/rs/Service.java b/JPA-RS Incubator/JPA-RS/src/org/eclipse/persistence/jpa/rs/Service.java
index f7e6327..249538d 100644
--- a/JPA-RS Incubator/JPA-RS/src/org/eclipse/persistence/jpa/rs/Service.java
+++ b/JPA-RS Incubator/JPA-RS/src/org/eclipse/persistence/jpa/rs/Service.java
@@ -95,10 +95,9 @@
            factory.getMetadataStore().setProperties(properties);

        } catch (Exception e){

            rb.status(Status.NOT_FOUND);

+           return rb.build();

        }

-       if (factory.getMetadataStore() != null){

-           rb.status(Status.OK);

-       }

+       rb.status(Status.OK);

        return rb.build();

    }

    

diff --git a/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/AllJavaSETests.java b/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/AllJavaSETests.java
new file mode 100644
index 0000000..23495d6
--- /dev/null
+++ b/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/AllJavaSETests.java
@@ -0,0 +1,27 @@
+/****************************************************************************

+ * Copyright (c) 2011 Oracle. All rights reserved.

+ * This program and the accompanying materials are made available under the 

+ * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 

+ * which accompanies this distribution. 

+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html

+ * and the Eclipse Distribution License is available at 

+ * http://www.eclipse.org/org/documents/edl-v10.php.

+ *

+ * Contributors:

+ *      tware - 

+ ******************************************************************************/

+package jpars.test;

+

+import jpars.test.bootstrap.TestBootstrap;

+import jpars.test.crud.CRUDTests;

+import jpars.test.service.TestService;

+

+import org.junit.runner.RunWith;

+import org.junit.runners.Suite;

+import org.junit.runners.Suite.SuiteClasses;

+

+@RunWith(Suite.class)

+@SuiteClasses({TestBootstrap.class, CRUDTests.class, TestService.class})

+public class AllJavaSETests {

+

+}

diff --git a/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/crud/CRUDTests.java b/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/crud/CRUDTests.java
index 2a4379b..957c46a 100644
--- a/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/crud/CRUDTests.java
+++ b/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/crud/CRUDTests.java
@@ -108,5 +108,18 @@
         List<DynamicEntity> users = (List<DynamicEntity>)persistenceContext.query("User.all", null);

         assertTrue(users.size() == 3);

     }

+    

+    @Test

+    public void testUpdate(){

+        DynamicEntity entity = persistenceContext.newEntity("User");

+        entity.set("name", "Tom");

+        persistenceContext.create(null, entity);

+        entity = persistenceContext.find("User", entity.get("id"));

+        entity.set("name", "Thomas");

+        persistenceContext.merge("User", null, entity);

+        entity = persistenceContext.find("User", entity.get("id"));

+        assertTrue("Entity name was not correctly updated.", entity.get("name").equals("Thomas"));

+        

+    }

 

 }

diff --git a/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/service/TestService.java b/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/service/TestService.java
index 731c676..9536195 100644
--- a/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/service/TestService.java
+++ b/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/service/TestService.java
@@ -26,7 +26,9 @@
 import java.util.List;

 import java.util.Map;

 

+import javax.persistence.EntityManager;

 import javax.ws.rs.WebApplicationException;

+import javax.ws.rs.core.HttpHeaders;

 import javax.ws.rs.core.MediaType;

 import javax.ws.rs.core.StreamingOutput;

 import javax.ws.rs.core.Response.Status;

@@ -37,8 +39,10 @@
 

 import jpars.test.util.ExamplePropertiesLoader;

 import jpars.test.util.TestHttpHeaders;

+import jpars.test.util.TestURIInfo;

 

 import org.eclipse.persistence.dynamic.DynamicEntity;

+import org.eclipse.persistence.jaxb.JAXBContext;

 import org.eclipse.persistence.jaxb.JAXBMarshaller;

 import org.eclipse.persistence.jpa.rs.PersistenceContext;

 import org.eclipse.persistence.jpa.rs.PersistenceFactory;

@@ -69,17 +73,27 @@
             factory.getMetadataStore().setProperties(properties);

             factory.getMetadataStore().clearMetadata();

             factory.bootstrapPersistenceContext("auction", new URL("file:///C:/EclipseLinkView2/incubator/JPA-RS Incubator/tests/JPA-RS Tests/src/xmldocs/auction-persistence.xml"), properties, true);

+            clearData();

         } catch (Exception e){

-            e.printStackTrace();

             fail(e.toString());

         }

     }

     

     @AfterClass

     public static void teardown(){

+        clearData();

         factory.getMetadataStore().clearMetadata();

     }

     

+    protected static void clearData(){

+        EntityManager em = factory.getPersistenceContext("auction").getEmf().createEntityManager();

+        em.getTransaction().begin();

+        em.createQuery("delete from Bid b").executeUpdate();

+        em.createQuery("delete from Auction a").executeUpdate();

+        em.createQuery("delete from User u").executeUpdate();

+        em.getTransaction().commit();

+    }

+    

     @Test

     public void testUpdateList(){

         Service service = new Service();

@@ -111,21 +125,7 @@
         entities.add(entity3);

         serializedData.set("serializedData", entities);

 

-        StringWriter writer = new StringWriter();

-        

-        JAXBMarshaller marshaller = null;

-

-        try{

-            marshaller = (JAXBMarshaller)context.getJAXBContext().createMarshaller();

-            marshaller.setProperty("eclipselink.media-type", MediaType.APPLICATION_XML);

-            marshaller.marshal(serializedData, writer);

-        } catch (Exception e){

-            e.printStackTrace();

-            fail(e.toString());

-        }

-        ByteArrayInputStream stream = new ByteArrayInputStream(writer.toString().getBytes());

-

-        StreamingOutput output = service.update("auction", "auctionSerializedData", new TestHttpHeaders(), stream);

+        StreamingOutput output = service.update("auction", "auctionSerializedData", generateHTTPHeader(MediaType.APPLICATION_XML_TYPE, MediaType.APPLICATION_XML), serializeToSteam(serializedData, context, MediaType.APPLICATION_XML));

 

         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

         try{

@@ -133,7 +133,7 @@
         } catch (IOException ex){

             fail(ex.toString());

         }

-        stream = new ByteArrayInputStream(outputStream.toByteArray());

+        InputStream stream = new ByteArrayInputStream(outputStream.toByteArray());

         serializedData = unmarshalEntity(context, "auctionSerializedData", null, MediaType.APPLICATION_XML, stream);

         

         assertNotNull("returned data was null", serializedData);

@@ -149,6 +149,8 @@
             values.remove(value.get("name"));

         }

         assertTrue("Incorrent set of names.", values.isEmpty());

+        

+        clearData();

     }

     

     @Test

@@ -163,13 +165,98 @@
             factory.getMetadataStore().setProperties(properties);

             factory.initialize(properties);

         } catch (Exception e){

-            e.printStackTrace();

             fail(e.toString());

         }

         assertTrue("factory was not recreated at boot time.", factory.getPersistenceContext("auction") != null);

     }

     

-    private DynamicEntity unmarshalEntity(PersistenceContext app, String type, String tenantId, String acceptedMedia, InputStream in) {

+    @Test

+    public void testNamedQuery(){

+        Service service = new Service();

+        service.setPersistenceFactory(factory);

+        PersistenceContext context = factory.getPersistenceContext("auction");

+        

+        DynamicEntity entity1 = context.newEntity("Auction");

+        entity1.set("name", "Computer");

+        context.create(null, entity1);

+        

+        DynamicEntity entity2 = context.newEntity("Auction");

+        entity2.set("name", "Word Processor");

+        context.create(null, entity2);

+        

+        TestHttpHeaders headers = new TestHttpHeaders();

+        headers.getAcceptableMediaTypes().add(MediaType.APPLICATION_JSON_TYPE);

+        List<String> mediaTypes = new ArrayList<String>();

+        mediaTypes.add(MediaType.APPLICATION_JSON);

+        TestURIInfo ui = new TestURIInfo();

+        StreamingOutput output = service.namedQuery("auction", "Auction.all", headers, ui);

+     

+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

+        try{

+            output.write(outputStream);

+        } catch (IOException ex){

+            fail(ex.toString());

+        }

+        String resultString = outputStream.toString();

+        

+        assertTrue("Computer was not in results.", resultString.contains("\"name\" : \"Computer\""));

+        assertTrue("Word Processor was not in restuls.", resultString.contains("\"name\" : \"Word Processor\""));

+        clearData();

+    }

+    

+    @Test

+    public void testNamedQuerySingleResult(){

+        Service service = new Service();

+        service.setPersistenceFactory(factory);

+        PersistenceContext context = factory.getPersistenceContext("auction");

+        

+        DynamicEntity entity1 = context.newEntity("Auction");

+        entity1.set("name", "Computer");

+        context.create(null, entity1);

+        

+        DynamicEntity entity2 = context.newEntity("Auction");

+        entity2.set("name", "Word Processor");

+        context.create(null, entity2);

+        

+        TestHttpHeaders headers = new TestHttpHeaders();

+        headers.getAcceptableMediaTypes().add(MediaType.APPLICATION_JSON_TYPE);

+        List<String> mediaTypes = new ArrayList<String>();

+        mediaTypes.add(MediaType.APPLICATION_JSON);

+        TestURIInfo ui = new TestURIInfo();

+        ui.addMatrixParameter("name", "Computer");

+        StreamingOutput output = service.namedQuerySingleResult("auction", "Auction.forName", headers, ui);

+        

+        String resultString = stringifyResults(output);

+        

+        assertTrue("Computer was not in results.", resultString.contains("\"name\" : \"Computer\""));

+        assertFalse("Word Processor was in results.", resultString.contains("\"name\" : \"Word Processor\""));

+        

+        clearData();

+    }

+   

+    @Test

+    public void testUpdate(){

+        Service service = new Service();

+        service.setPersistenceFactory(factory);

+        PersistenceContext context = factory.getPersistenceContext("auction");

+        

+        DynamicEntity entity1 = context.newEntity("Auction");

+        entity1.set("name", "Computer");

+        context.create(null, entity1);

+        entity1.set("name", "Laptop");

+        entity1.set("description", "Speedy");

+

+        TestURIInfo ui = new TestURIInfo();

+        ui.addMatrixParameter("name", "Computer");

+        StreamingOutput output = service.update("auction", "Auction", generateHTTPHeader(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON), serializeToSteam(entity1, context, MediaType.APPLICATION_JSON));

+

+        String resultString = stringifyResults(output);

+        

+        assertTrue("Laptop was not in results.", resultString.contains("\"name\" : \"Laptop\""));

+        assertTrue("Laptop was not in results.", resultString.contains("\"description\" : \"Speedy\""));

+    }

+    

+    private static DynamicEntity unmarshalEntity(PersistenceContext app, String type, String tenantId, String acceptedMedia, InputStream in) {

         Unmarshaller unmarshaller;

         try {

             unmarshaller = app.getJAXBContext().createUnmarshaller();

@@ -181,4 +268,39 @@
         }

     }

     

+    public static String stringifyResults(StreamingOutput output){

+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

+        try{

+            output.write(outputStream);

+        } catch (IOException ex){

+            fail(ex.toString());

+        }

+        return outputStream.toString();

+    }

+    

+    public static InputStream serializeToSteam(DynamicEntity object, PersistenceContext context, String mediaType){

+        StringWriter writer = new StringWriter();

+        JAXBMarshaller marshaller = null;

+        try{

+            marshaller = (JAXBMarshaller)context.getJAXBContext().createMarshaller();

+            marshaller.setProperty("eclipselink.media-type", mediaType);

+            marshaller.setProperty(JAXBContext.INCLUDE_ROOT, Boolean.FALSE);

+            marshaller.marshal(object, writer);

+        } catch (Exception e){

+            fail(e.toString());

+        }

+        ByteArrayInputStream stream = new ByteArrayInputStream(writer.toString().getBytes());

+        return stream;

+    }

+    

+    public static HttpHeaders generateHTTPHeader(MediaType acceptableMedia, String mediaTypeString){

+        TestHttpHeaders headers = new TestHttpHeaders();

+        headers.getAcceptableMediaTypes().add(acceptableMedia);

+        List<String> mediaTypes = new ArrayList<String>();

+        mediaTypes.add(mediaTypeString);

+

+        headers.getRequestHeaders().put(HttpHeaders.CONTENT_TYPE, mediaTypes);

+        return headers;

+    }

+    

 }

diff --git a/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/util/TestHttpHeaders.java b/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/util/TestHttpHeaders.java
index 42bf456..d51a6f4 100644
--- a/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/util/TestHttpHeaders.java
+++ b/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/util/TestHttpHeaders.java
@@ -22,6 +22,8 @@
 import javax.ws.rs.core.MediaType;

 import javax.ws.rs.core.MultivaluedMap;

 

+import com.sun.jersey.core.util.StringKeyIgnoreCaseMultivaluedMap;

+

 /**

  * A fake HTTPHeaders implementation to test the service class

  * @author tware

@@ -29,6 +31,9 @@
  */

 public class TestHttpHeaders implements HttpHeaders {

 

+    protected List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();

+    protected MultivaluedMap<String, String> requestHeaders = new StringKeyIgnoreCaseMultivaluedMap<String>();

+    

     @Override

     public List<Locale> getAcceptableLanguages() {

         // TODO Auto-generated method stub

@@ -37,9 +42,7 @@
 

     @Override

     public List<MediaType> getAcceptableMediaTypes() {

-        List<MediaType> list = new ArrayList<MediaType>();

-        list.add(MediaType.valueOf(MediaType.APPLICATION_XML));

-        return list;

+        return acceptableMediaTypes;

     }

 

     @Override

@@ -62,15 +65,12 @@
 

     @Override

     public List<String> getRequestHeader(String arg0) {

-        List<String> list = new ArrayList<String>();

-        list.add(MediaType.APPLICATION_XML);

-        return list;

+        return requestHeaders.get(arg0);

     }

 

     @Override

     public MultivaluedMap<String, String> getRequestHeaders() {

-        // TODO Auto-generated method stub

-        return null;

+        return requestHeaders;

     }

 

 }

diff --git a/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/util/TestURIInfo.java b/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/util/TestURIInfo.java
new file mode 100644
index 0000000..11bbaee
--- /dev/null
+++ b/JPA-RS Incubator/tests/JPA-RS Tests/src/jpars/test/util/TestURIInfo.java
@@ -0,0 +1,162 @@
+/****************************************************************************

+ * Copyright (c) 2011 Oracle. All rights reserved.

+ * This program and the accompanying materials are made available under the 

+ * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 

+ * which accompanies this distribution. 

+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html

+ * and the Eclipse Distribution License is available at 

+ * http://www.eclipse.org/org/documents/edl-v10.php.

+ *

+ * Contributors:

+ *      tware - 

+ ******************************************************************************/

+package jpars.test.util;

+

+import java.net.URI;

+import java.util.ArrayList;

+import java.util.List;

+

+import javax.ws.rs.core.MultivaluedMap;

+import javax.ws.rs.core.PathSegment;

+import javax.ws.rs.core.UriBuilder;

+import javax.ws.rs.core.UriInfo;

+

+import com.sun.jersey.core.util.MultivaluedMapImpl;

+

+public class TestURIInfo implements UriInfo {

+

+    protected List<PathSegment> pathSegments = new ArrayList<PathSegment>();

+    

+    public TestURIInfo(){

+        PathSegment segment = new PathSegment() {

+                

+            MultivaluedMap<String, String> matrixParameters = new MultivaluedMapImpl();

+                

+            @Override

+            public String getPath() {

+                // TODO Auto-generated method stub

+                return null;

+            }

+                

+            @Override

+            public MultivaluedMap<String, String> getMatrixParameters() {

+                // TODO Auto-generated method stub

+                return matrixParameters;

+            }

+        };

+        pathSegments.add(segment);

+    }

+    

+    @Override

+    public URI getAbsolutePath() {

+        // TODO Auto-generated method stub

+        return null;

+    }

+

+    @Override

+    public UriBuilder getAbsolutePathBuilder() {

+        // TODO Auto-generated method stub

+        return null;

+    }

+

+    @Override

+    public URI getBaseUri() {

+        // TODO Auto-generated method stub

+        return null;

+    }

+

+    @Override

+    public UriBuilder getBaseUriBuilder() {

+        // TODO Auto-generated method stub

+        return null;

+    }

+

+    @Override

+    public List<Object> getMatchedResources() {

+        // TODO Auto-generated method stub

+        return null;

+    }

+

+    @Override

+    public List<String> getMatchedURIs() {

+        // TODO Auto-generated method stub

+        return null;

+    }

+

+    @Override

+    public List<String> getMatchedURIs(boolean arg0) {

+        // TODO Auto-generated method stub

+        return null;

+    }

+

+    @Override

+    public String getPath() {

+        // TODO Auto-generated method stub

+        return null;

+    }

+

+    @Override

+    public String getPath(boolean arg0) {

+        // TODO Auto-generated method stub

+        return null;

+    }

+

+    @Override

+    public MultivaluedMap<String, String> getPathParameters() {

+        // TODO Auto-generated method stub

+        return null;

+    }

+

+    @Override

+    public MultivaluedMap<String, String> getPathParameters(boolean arg0) {

+        // TODO Auto-generated method stub

+        return null;

+    }

+

+    @Override

+    public List<PathSegment> getPathSegments() {

+        // TODO Auto-generated method stub

+        return pathSegments;

+    }

+

+    @Override

+    public List<PathSegment> getPathSegments(boolean arg0) {

+        // TODO Auto-generated method stub

+        return pathSegments;

+    }

+

+    @Override

+    public MultivaluedMap<String, String> getQueryParameters() {

+        // TODO Auto-generated method stub

+        return null;

+    }

+

+    @Override

+    public MultivaluedMap<String, String> getQueryParameters(boolean arg0) {

+        // TODO Auto-generated method stub

+        return null;

+    }

+

+    @Override

+    public URI getRequestUri() {

+        // TODO Auto-generated method stub

+        return null;

+    }

+

+    @Override

+    public UriBuilder getRequestUriBuilder() {

+        // TODO Auto-generated method stub

+        return null;

+    }

+    

+    public void addMatrixParameter(String key, String value){

+        PathSegment segment = pathSegments.get(pathSegments.size() - 1);

+        List<String> parameters = segment.getMatrixParameters().get(key);

+        if (parameters == null){

+            parameters = new ArrayList<String>();

+        }

+        parameters.add(value);

+        segment.getMatrixParameters().put(key, parameters);

+    }

+

+}