From e9d2e4ffb09b1196b702db8d218aea160869a251 Mon Sep 17 00:00:00 2001 From: Imran M Yousuf Date: Sun, 14 Dec 2008 17:12:18 +0600 Subject: [PATCH] Add jax-rs annotations and test case for search operation Signed-off-by: Imran M Yousuf --- .../ws/server/PublicationHouseResource.java | 19 +++++++++---- .../ws/server/PublicationHouseResourceTest.java | 32 ++++++++++++++++++++-- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/WebServiceResource/src/main/java/com/smartitengineering/bookstore/ws/server/PublicationHouseResource.java b/WebServiceResource/src/main/java/com/smartitengineering/bookstore/ws/server/PublicationHouseResource.java index 0bbf0df..160c8ef 100644 --- a/WebServiceResource/src/main/java/com/smartitengineering/bookstore/ws/server/PublicationHouseResource.java +++ b/WebServiceResource/src/main/java/com/smartitengineering/bookstore/ws/server/PublicationHouseResource.java @@ -4,12 +4,15 @@ */ package com.smartitengineering.bookstore.ws.server; +import com.smartitengineering.bookstore.domain.filter.PublicationHouseFilter; import com.smartitengineering.bookstore.service.api.PublicationHouseService; import com.smartitengineering.bookstore.ws.element.PublicationHouseElement; import com.smartitengineering.bookstore.ws.element.PublicationHouseElements; import com.smartitengineering.bookstore.ws.element.PublicationHouseFilterElement; import javax.annotation.Resource; +import javax.ws.rs.Consumes; import javax.ws.rs.GET; +import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; @@ -22,11 +25,10 @@ import org.springframework.stereotype.Component; */ @Path("/pub-house") @Component -@Scope(value="singleton") -@Produces("application/xml") +@Scope(value = "singleton") public class PublicationHouseResource { - @Resource(name="publicationHouseService") + @Resource(name = "publicationHouseService") private PublicationHouseService publicationHouseService; public void create(PublicationHouseElement publicationHouseElement) { @@ -44,16 +46,23 @@ public class PublicationHouseResource { getPublicationHouse()); } + @POST + @Path("search") + @Consumes("application/xml") + @Produces("application/xml") public PublicationHouseElements search( PublicationHouseFilterElement filterElement) { PublicationHouseElements elements = new PublicationHouseElements(); - elements.setPublicationHouses(publicationHouseService.search(filterElement. - getPublicationHouseFilter())); + elements.setPublicationHouses( + publicationHouseService.search(filterElement != null && + filterElement.getPublicationHouseFilter() != null ? filterElement. + getPublicationHouseFilter() : new PublicationHouseFilter())); return elements; } @GET @Path("/short/{uniqueShortName}") + @Produces("application/xml") public PublicationHouseElement getByUniqueShortName( @PathParam("uniqueShortName") String uniqueShortName) { PublicationHouseElement element = new PublicationHouseElement(); diff --git a/WebServiceServer/src/test/java/com/smartitengineering/bookstore/ws/server/PublicationHouseResourceTest.java b/WebServiceServer/src/test/java/com/smartitengineering/bookstore/ws/server/PublicationHouseResourceTest.java index 9bea53e..a4e00ae 100644 --- a/WebServiceServer/src/test/java/com/smartitengineering/bookstore/ws/server/PublicationHouseResourceTest.java +++ b/WebServiceServer/src/test/java/com/smartitengineering/bookstore/ws/server/PublicationHouseResourceTest.java @@ -5,9 +5,13 @@ package com.smartitengineering.bookstore.ws.server; import com.smartitengineering.bookstore.domain.PublicationHouse; +import com.smartitengineering.bookstore.domain.filter.PublicationHouseFilter; import com.smartitengineering.bookstore.ws.element.PublicationHouseElement; +import com.smartitengineering.bookstore.ws.element.PublicationHouseElements; +import com.smartitengineering.bookstore.ws.element.PublicationHouseFilterElement; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.WebResource.Builder; import java.io.File; import java.net.URI; import java.util.Collections; @@ -67,11 +71,15 @@ public class PublicationHouseResourceTest protected void tearDown() throws Exception { super.tearDown(); - glassfish.stop(); } - public void testGetByUniqueShortName() { + public void testResources() { + doTestGetByUniqueShortName(); + doTestSearch(); + } + + public void doTestGetByUniqueShortName() { String uniqueShortName = "testA"; WebResource resource = webResource.path("pub-house/short/" + uniqueShortName); @@ -85,4 +93,24 @@ public class PublicationHouseResourceTest assertEquals("No Name", publicationHouse.getName()); assertEquals("NONE@NONE", publicationHouse.getContactEmailAddress()); } + + public void doTestSearch() { + PublicationHouseFilterElement filter = + new PublicationHouseFilterElement(); + PublicationHouseFilter publicationHouseFilter = new PublicationHouseFilter(); + filter.setPublicationHouseFilter(publicationHouseFilter); + final String name = "Sheba Prokashoni"; + publicationHouseFilter.setName(name); + final String uniqueName = "sheba-prok"; + publicationHouseFilter.setUniqueShortString(uniqueName); + final Builder type = + webResource.path("pub-house/search").type("application/xml"); + final PublicationHouseElements elements = type.post( + PublicationHouseElements.class, filter); + assertTrue(elements.getPublicationHouses().size() > 0); + for(PublicationHouse house : elements.getPublicationHouses()) { + assertTrue(house.getName().startsWith(name)); + assertTrue(house.getUniqueShortName().startsWith(uniqueName)); + } + } } -- 2.11.4.GIT