Mercurial > lada > lada-server
changeset 451:89a5dbeb5bd6
Comments.
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Wed, 11 Feb 2015 13:03:07 +0100 |
parents | 808ea2091c1d |
children | 819747a8d037 |
files | src/main/java/de/intevation/lada/rest/ProbeService.java src/main/java/de/intevation/lada/util/data/AbstractRepository.java src/main/java/de/intevation/lada/util/data/DefaultRepository.java src/main/java/de/intevation/lada/util/data/ReadOnlyRepository.java src/main/java/de/intevation/lada/util/data/Repository.java src/test/java/de/intevation/lada/LadaTest.java src/test/java/de/intevation/lada/test/ProbeServiceTest.java |
diffstat | 7 files changed, 185 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/rest/ProbeService.java Wed Feb 11 13:01:55 2015 +0100 +++ b/src/main/java/de/intevation/lada/rest/ProbeService.java Wed Feb 11 13:03:07 2015 +0100 @@ -38,25 +38,40 @@ import de.intevation.lada.util.data.RepositoryType; import de.intevation.lada.util.rest.Response; + +/** + * This class produces a RESTful service to interact with probe objects. + * + * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> + */ @Path("probe") @RequestScoped public class ProbeService { + /* The logger used in this class.*/ @Inject private Logger logger; + /* The data repository granting read/write access.*/ @Inject @RepositoryConfig(type=RepositoryType.RW) private Repository defaultRepo; + /* The authentication module.*/ @Inject @AuthenticationConfig(type=AuthenticationType.NONE) private Authentication authentication; + /* The authorization module.*/ @Inject @AuthorizationConfig(type=AuthorizationType.NONE) private Authorization authorization; + /** + * Get all probe objects. + * + * @return Response object containing all probe objects. + */ @GET @Path("/") @Produces("application/json") @@ -65,10 +80,14 @@ logger.debug("User is not authenticated!"); return new Response(false, 699, null); } - defaultRepo.setDataSource("land"); - return defaultRepo.getAll(LProbe.class); + return defaultRepo.getAll(LProbe.class, "land"); } + /** + * Get a probe object by id. + * + * @return Response object containing a single probe. + */ @GET @Path("/{id}") @Produces(MediaType.APPLICATION_JSON) @@ -80,10 +99,14 @@ logger.debug("User is not authenticated!"); return new Response(false, 699, null); } - defaultRepo.setDataSource("land"); - return defaultRepo.getById(LProbe.class, Integer.valueOf(id)); + return defaultRepo.getById(LProbe.class, Integer.valueOf(id), "land"); } + /** + * Create a new probe object. + * + * @return Response object containing the new probe object. + */ @POST @Path("/") @Produces(MediaType.APPLICATION_JSON)
--- a/src/main/java/de/intevation/lada/util/data/AbstractRepository.java Wed Feb 11 13:01:55 2015 +0100 +++ b/src/main/java/de/intevation/lada/util/data/AbstractRepository.java Wed Feb 11 13:03:07 2015 +0100 @@ -32,10 +32,10 @@ protected String jndiPath; /** - * Create or update an object in the database. + * Create object in the database. * This operation can not be undone. * - * @param object The object to create or update + * @param object The object to create * * @throws EntityExistsException * @throws IllegalArgumentException @@ -52,6 +52,17 @@ emp.entityManager(dataSource).persist(object); } + /** + * Create object in the database. + * This operation can not be undone. + * + * @param object The object to create + * + * @throws EntityExistsException + * @throws IllegalArgumentException + * @throws EJBTransactionRolledbackException + * @throws TransactionRequiredException + */ @TransactionAttribute(TransactionAttributeType.REQUIRED) protected void updateInDatabase(Object object, String dataSource) throws EntityExistsException,
--- a/src/main/java/de/intevation/lada/util/data/DefaultRepository.java Wed Feb 11 13:01:55 2015 +0100 +++ b/src/main/java/de/intevation/lada/util/data/DefaultRepository.java Wed Feb 11 13:03:07 2015 +0100 @@ -19,7 +19,9 @@ /** - * @author rrenkert + * Repository providing read and write access. + * + * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> */ @Stateless public class DefaultRepository extends ReadOnlyRepository { @@ -68,6 +70,14 @@ return response; } + /** + * Update an existing object in the database. + * + * @param object The object. + * @param dataSource The datasource. + * + * @return Response object containing the upadted object. + */ @Override public Response update(Object object, String dataSource) { Response response = new Response(true, 200, object); @@ -89,6 +99,14 @@ return response; } + /** + * Delete an object from the database. + * + * @param object The object. + * @param dataSource The datasource. + * + * @return Response object. + */ @Override public Response delete(Object object, String dataSource) { Response response = new Response(true, 200, null);
--- a/src/main/java/de/intevation/lada/util/data/ReadOnlyRepository.java Wed Feb 11 13:01:55 2015 +0100 +++ b/src/main/java/de/intevation/lada/util/data/ReadOnlyRepository.java Wed Feb 11 13:03:07 2015 +0100 @@ -20,7 +20,9 @@ /** - * @author rrenkert + * Repository providing read access. + * + * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> */ @Stateless public class ReadOnlyRepository extends AbstractRepository { @@ -31,21 +33,44 @@ public ReadOnlyRepository() { } + /** + * NOT SUPPORTED. + * + * @return null + */ @Override public Response create(Object object, String dataSource) { return null; } + /** + * NOT SUPPORTED. + * + * @return null + */ @Override public Response update(Object object, String dataSource) { return null; } + /** + * NOT SUPPORTED. + * + * @return null + */ @Override public Response delete(Object object, String dataSource) { return null; } + /** + * Get objects from database using the given filter. + * + * @param filter Filter used to request objects. + * @param datasource The datasource. + * + * @return Response object containing the filtered list of objects. + */ @Override public <T> Response filter(CriteriaQuery<T> filter, String dataSource) { List<T> result = @@ -53,6 +78,17 @@ return new Response(true, 200, result); } + + /** + * Get objects from database using the given filter. + * + * @param filter Filter used to request objects. + * @param size The maximum amount of objects. + * @param start The start index. + * @param datasource The datasource. + * + * @return Response object containing the filtered list of objects. + */ @Override public <T> Response filter( CriteriaQuery<T> filter, @@ -69,7 +105,16 @@ return new Response(true, 200, result); } + /** + * Get all objects. + * + * @param clazz The type of the objects. + * @param dataSource The datasource. + * + * @return Response object containg all requested objects. + */ public <T> Response getAll(Class<T> clazz, String dataSource) { + logger.warn("ich bin ein logger"); EntityManager manager = emp.entityManager(dataSource); QueryBuilder<T> builder = new QueryBuilder<T>(manager, clazz); @@ -78,6 +123,15 @@ return new Response(true, 200, result); } + /** + * Get an object by its id. + * + * @param clazz The type of the object. + * @param id The id of the object. + * @param dataSource The datasource. + * + * @return Response object containg the requested object. + */ @Override public <T> Response getById(Class<T> clazz, Object id, String dataSource) { T item = emp.entityManager(dataSource).find(clazz, id);
--- a/src/main/java/de/intevation/lada/util/data/Repository.java Wed Feb 11 13:01:55 2015 +0100 +++ b/src/main/java/de/intevation/lada/util/data/Repository.java Wed Feb 11 13:03:07 2015 +0100 @@ -15,7 +15,7 @@ /** * This generic Container is an interface to request and select Data - * obejcts from the connected database. + * objects from the connected database. * * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> */
--- a/src/test/java/de/intevation/lada/LadaTest.java Wed Feb 11 13:01:55 2015 +0100 +++ b/src/test/java/de/intevation/lada/LadaTest.java Wed Feb 11 13:03:07 2015 +0100 @@ -1,3 +1,10 @@ +/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz + * Software engineering by Intevation GmbH + * + * This file is Free Software under the GNU GPL (v>=3) + * and comes with ABSOLUTELY NO WARRANTY! Check out + * the documentation coming with IMIS-Labordaten-Application for details. + */ package de.intevation.lada; import java.net.URL; @@ -15,9 +22,18 @@ import de.intevation.lada.test.ProbeServiceTest; import de.intevation.lada.test.QueryServiceTest; + +/** + * Class to test the Lada server. + * + * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> + */ @RunWith(Arquillian.class) public class LadaTest { + /** + * Create a deployable WAR archive. + * */ @Deployment public static WebArchive createDeployment() throws Exception { return ShrinkWrap.create(WebArchive.class, "lada-basis-test.war") @@ -46,8 +62,7 @@ */ @Test @RunAsClient - public final void testProbeService( - @ArquillianResource URL baseUrl) + public final void testProbeService(@ArquillianResource URL baseUrl) throws Exception { Assert.assertNotNull(baseUrl); ProbeServiceTest test = new ProbeServiceTest();
--- a/src/test/java/de/intevation/lada/test/ProbeServiceTest.java Wed Feb 11 13:01:55 2015 +0100 +++ b/src/test/java/de/intevation/lada/test/ProbeServiceTest.java Wed Feb 11 13:03:07 2015 +0100 @@ -1,3 +1,10 @@ +/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz + * Software engineering by Intevation GmbH + * + * This file is Free Software under the GNU GPL (v>=3) + * and comes with ABSOLUTELY NO WARRANTY! Check out + * the documentation coming with IMIS-Labordaten-Application for details. + */ package de.intevation.lada.test; import java.io.StringReader; @@ -16,7 +23,11 @@ import org.junit.Assert; - +/** + * Class to test the Lada probe REST service. + * + * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> + */ public class ProbeServiceTest { private static final String COMPARE_PROBE = @@ -46,6 +57,11 @@ private Integer createdProbeId; + /** + * Main entry point in this class to start the tests. + * + * @param baseUrl The url pointing to the test deployment. + */ public final void test(URL baseUrl) throws Exception { System.out.println("\nStarting test (2) on Probe-Service:"); probeGetAllService(baseUrl); @@ -55,38 +71,58 @@ probeDelete(baseUrl); } + /** + * Test the GET Service by requesting all probe objects. + * + * @param baseUrl The url pointing to the test deployment. + */ private final void probeGetAllService(URL baseUrl) throws Exception { System.out.println("Testing get: "); + /* Create a client*/ Client client = ClientBuilder.newClient(); WebTarget target = client.target(baseUrl + "probe"); + /* Request all probe objects*/ Response response = target.request().get(); String entity = response.readEntity(String.class); try{ + /* Try to parse the response*/ JsonReader reader = Json.createReader(new StringReader(entity)); JsonObject content = reader.readObject(); + /* Verify the response*/ Assert.assertTrue(content.getBoolean("success")); Assert.assertEquals("200", content.getString("message")); } catch(JsonException je) { Assert.fail(je.getMessage()); } + System.out.println("passed"); } + /** + * Test the GET Service by requesting a single probe object by id. + * + * @param baseUrl The url pointing to the test deployment. + */ private final void probeGetByIdService(URL baseUrl) throws Exception { System.out.println("Testing getById: "); try { + /* Create a json object from static probe string*/ JsonReader fromStringRreader = Json.createReader(new StringReader(COMPARE_PROBE)); JsonObject staticProbe = fromStringRreader.readObject(); + /* Create a client*/ Client client = ClientBuilder.newClient(); WebTarget target = client.target(baseUrl + "probe/1"); + /* Request a probe object by id*/ Response response = target.request().get(); String entity = response.readEntity(String.class); + /* Try to parse the response*/ JsonReader fromServiceReader = Json.createReader(new StringReader(entity)); JsonObject content = fromServiceReader.readObject(); + /* Verify the response*/ Assert.assertTrue(content.getBoolean("success")); Assert.assertEquals("200", content.getString("message")); Assert.assertEquals(staticProbe, @@ -97,6 +133,12 @@ } System.out.println("passed"); } + + /** + * Test the CREATE Service. + * + * @param baseUrl The url pointing to the test deployment. + */ private final void probeCreate(URL baseUrl) throws Exception { System.out.println("Testing create: "); @@ -125,6 +167,11 @@ System.out.println("passed"); } + /** + * Test the UPDATE Service. + * + * @param baseUrl The url pointing to the test deployment. + */ private final void probeUpdate(URL baseUrl) throws Exception { System.out.println("Testing update: "); @@ -162,6 +209,11 @@ System.out.println("passed"); } + /** + * Test the DELETE Service. + * + * @param baseUrl The url pointing to the test deployment. + */ private final void probeDelete(URL baseUrl) { System.out.println("Testing delete: "); try {