← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10052: FRED-API: testing creation of facilities (with name, and name+active)

 

------------------------------------------------------------
revno: 10052
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2013-03-07 18:41:40 +0300
message:
  FRED-API: testing creation of facilities (with name, and name+active)
modified:
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/FredSpringWebTest.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/Facility.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/GeoUtils.java
  dhis-2/dhis-web/dhis-web-api-fred/src/test/java/org/hisp/dhis/web/webapi/v1/controller/FacilityControllerTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/FredSpringWebTest.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/FredSpringWebTest.java	2013-03-07 09:24:35 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/FredSpringWebTest.java	2013-03-07 15:41:40 +0000
@@ -27,7 +27,9 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.codehaus.jackson.map.ObjectMapper;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.web.webapi.v1.utils.ObjectMapperFactoryBean;
 import org.junit.Before;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -46,6 +48,8 @@
 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.filter.CharacterEncodingFilter;
+import org.springframework.web.filter.ShallowEtagHeaderFilter;
 
 import java.lang.reflect.Method;
 import java.util.ArrayList;
@@ -72,6 +76,8 @@
 
     protected MockMvc mvc;
 
+    protected ObjectMapper objectMapper;
+
     public MockHttpSession getSession( String... authorities )
     {
         SecurityContextHolder.getContext().setAuthentication( getPrincipal( authorities ) );
@@ -107,8 +113,14 @@
     @Before
     public void setup() throws Exception
     {
+        objectMapper = new ObjectMapperFactoryBean().getObject();
+
+        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
+        characterEncodingFilter.setEncoding( "UTF-8" );
+        characterEncodingFilter.setForceEncoding( true );
+
         mvc = MockMvcBuilders.webAppContextSetup( wac )
-            .addFilter( filterChainProxy )
+            .addFilters( characterEncodingFilter, new ShallowEtagHeaderFilter(), filterChainProxy )
             .build();
 
         executeStartupRoutines();

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/Facility.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/Facility.java	2013-03-04 12:07:19 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/Facility.java	2013-03-07 15:41:40 +0000
@@ -54,8 +54,7 @@
     private String name;
 
     // Active = true/false indicates whether the facility is active or not
-    @NotNull
-    private Boolean active;
+    private Boolean active = true;
 
     // URL link to the unique ID API resource for the facility
     private String url;
@@ -81,6 +80,24 @@
     {
     }
 
+    public Facility( String name )
+    {
+        this.name = name;
+    }
+
+    public Facility( String name, Boolean active )
+    {
+        this.name = name;
+        this.active = active;
+    }
+
+    public Facility( String name, Boolean active, List<Double> coordinates )
+    {
+        this.name = name;
+        this.active = active;
+        this.coordinates = coordinates;
+    }
+
     public String getId()
     {
         return id;

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/GeoUtils.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/GeoUtils.java	2013-03-05 05:20:25 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/GeoUtils.java	2013-03-07 15:41:40 +0000
@@ -75,12 +75,12 @@
         {
             List<?> list = new ObjectMapper().readValue( coordinatesString, List.class );
 
-            if ( from == CoordinateOrder.COORDINATE_LATLNG )
+            if ( !list.isEmpty() && from == CoordinateOrder.COORDINATE_LATLNG )
             {
                 coordinates.lat = convertToDouble( list.get( 0 ) );
                 coordinates.lng = convertToDouble( list.get( 1 ) );
             }
-            else if ( from == CoordinateOrder.COORDINATE_LNGLAT )
+            else if ( !list.isEmpty() && from == CoordinateOrder.COORDINATE_LNGLAT )
             {
                 coordinates.lat = convertToDouble( list.get( 1 ) );
                 coordinates.lng = convertToDouble( list.get( 0 ) );

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/test/java/org/hisp/dhis/web/webapi/v1/controller/FacilityControllerTest.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/test/java/org/hisp/dhis/web/webapi/v1/controller/FacilityControllerTest.java	2013-03-07 12:59:06 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/test/java/org/hisp/dhis/web/webapi/v1/controller/FacilityControllerTest.java	2013-03-07 15:41:40 +0000
@@ -27,9 +27,11 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hamcrest.Matchers;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.web.FredSpringWebTest;
+import org.hisp.dhis.web.webapi.v1.domain.Facility;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.MediaType;
@@ -121,6 +123,20 @@
         mvc.perform( get( "/v1/facilities/" + organisationUnit.getUid() ).session( session ).accept( MediaType.APPLICATION_JSON ) )
             .andExpect( content().contentType( MediaType.APPLICATION_JSON ) )
             .andExpect( jsonPath( "$.name" ).value( "OrgUnitA" ) )
+            .andExpect( header().string( "ETag", Matchers.notNullValue() ) )
+            .andExpect( status().isOk() );
+    }
+
+    @Test
+    public void testGetFacilityVerifyPresenceOfETag() throws Exception
+    {
+        OrganisationUnit organisationUnit = createOrganisationUnit( 'A' );
+        manager.save( organisationUnit );
+
+        MockHttpSession session = getSession( "ALL" );
+
+        mvc.perform( get( "/v1/facilities/" + organisationUnit.getUid() ).session( session ).accept( MediaType.APPLICATION_JSON ) )
+            .andExpect( header().string( "ETag", Matchers.notNullValue() ) )
             .andExpect( status().isOk() );
     }
 
@@ -146,4 +162,33 @@
         mvc.perform( put( "/v1/facilities/abc123" ).content( "{}" ).session( session ).contentType( MediaType.APPLICATION_JSON ) )
             .andExpect( status().isNotFound() );
     }
+
+    @Test
+    public void testPostName() throws Exception
+    {
+        MockHttpSession session = getSession( "ALL" );
+
+        Facility facility = new Facility( "FacilityA" );
+
+        mvc.perform( post( "/v1/facilities" ).content( objectMapper.writeValueAsString( facility ) )
+            .session( session ).contentType( MediaType.APPLICATION_JSON ) )
+            .andExpect( content().contentType( MediaType.APPLICATION_JSON ) )
+            .andExpect( jsonPath( "$.name" ).value( "FacilityA" ) )
+            .andExpect( status().isCreated() );
+    }
+
+    @Test
+    public void testPostNameActive() throws Exception
+    {
+        MockHttpSession session = getSession( "ALL" );
+
+        Facility facility = new Facility( "FacilityA", false );
+
+        mvc.perform( post( "/v1/facilities" ).content( objectMapper.writeValueAsString( facility ) )
+            .session( session ).contentType( MediaType.APPLICATION_JSON ) )
+            .andExpect( content().contentType( MediaType.APPLICATION_JSON ) )
+            .andExpect( jsonPath( "$.name" ).value( "FacilityA" ) )
+            .andExpect( jsonPath( "$.active" ).value( false ) )
+            .andExpect( status().isCreated() );
+    }
 }