← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10006: FRED-API: added more tests. changed to new code/message responses.

 

------------------------------------------------------------
revno: 10006
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2013-03-06 12:21:16 +0300
message:
  FRED-API: added more tests. changed to new code/message responses.
added:
  dhis-2/dhis-web/dhis-web-api-fred/src/test/java/org/hisp/dhis/web/webapi/v1/utils/OrganisationUnitToFacilityConverterTest.java
modified:
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityServiceController.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/MessageResponse.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/MessageResponseUtils.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/webapi/v1/controller/FacilityController.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java	2013-03-05 09:09:02 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java	2013-03-06 09:21:16 +0000
@@ -90,9 +90,9 @@
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
-@Controller( value = "facility-controller-" + FredController.PREFIX )
-@RequestMapping( FacilityController.RESOURCE_PATH )
-@PreAuthorize( "hasRole('M_dhis-web-api-fred') or hasRole('ALL')" )
+@Controller(value = "facility-controller-" + FredController.PREFIX)
+@RequestMapping(FacilityController.RESOURCE_PATH)
+@PreAuthorize("hasRole('M_dhis-web-api-fred') or hasRole('ALL')")
 public class FacilityController
 {
     public static final String RESOURCE_PATH = "/" + FredController.PREFIX + "/facilities";
@@ -238,13 +238,13 @@
         return facility;
     }
 
-    @RequestMapping( value = "", method = RequestMethod.GET )
-    public String readFacilities( Model model, @RequestParam( required = false ) Boolean active,
-        @RequestParam( value = "updatedSince", required = false ) Date lastUpdated,
-        @RequestParam( value = "allProperties", required = false, defaultValue = "true" ) Boolean allProperties,
-        @RequestParam( value = "fields", required = false ) String fields,
-        @RequestParam( value = "limit", required = false, defaultValue = "25" ) String limit,
-        @RequestParam( value = "offset", required = false, defaultValue = "0" ) Integer offset,
+    @RequestMapping(value = "", method = RequestMethod.GET)
+    public String readFacilities( Model model, @RequestParam(required = false) Boolean active,
+        @RequestParam(value = "updatedSince", required = false) Date lastUpdated,
+        @RequestParam(value = "allProperties", required = false, defaultValue = "true") Boolean allProperties,
+        @RequestParam(value = "fields", required = false) String fields,
+        @RequestParam(value = "limit", required = false, defaultValue = "25") String limit,
+        @RequestParam(value = "offset", required = false, defaultValue = "0") Integer offset,
         HttpServletRequest request )
     {
         Facilities facilities = new Facilities();
@@ -338,10 +338,10 @@
         return FredController.PREFIX + "/layout";
     }
 
-    @RequestMapping( value = "/{id}", method = RequestMethod.GET )
+    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
     public String readFacility( Model model, @PathVariable String id,
-        @RequestParam( value = "allProperties", required = false, defaultValue = "true" ) Boolean allProperties,
-        @RequestParam( value = "fields", required = false ) String fields,
+        @RequestParam(value = "allProperties", required = false, defaultValue = "true") Boolean allProperties,
+        @RequestParam(value = "fields", required = false) String fields,
         HttpServletRequest request )
     {
         OrganisationUnit organisationUnit;
@@ -426,8 +426,8 @@
     // POST JSON
     //--------------------------------------------------------------------------
 
-    @RequestMapping( value = "", method = RequestMethod.POST )
-    @PreAuthorize( "hasRole('F_FRED_CREATE') or hasRole('ALL')" )
+    @RequestMapping(value = "", method = RequestMethod.POST)
+    @PreAuthorize("hasRole('F_FRED_CREATE') or hasRole('ALL')")
     public ResponseEntity<String> createFacility( @RequestBody Facility facility ) throws Exception
     {
         if ( facility.getId() == null )
@@ -448,15 +448,18 @@
 
             if ( organisationUnitService.getOrganisationUnit( organisationUnit.getUuid() ) != null )
             {
-                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( "An object with that UUID already exists." ), headers, HttpStatus.CONFLICT );
+                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( HttpStatus.CONFLICT.toString(),
+                    "An object with that UUID already exists." ), headers, HttpStatus.CONFLICT );
             }
             if ( organisationUnitService.getOrganisationUnit( organisationUnit.getUid() ) != null )
             {
-                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( "An object with that UID already exists." ), headers, HttpStatus.CONFLICT );
+                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( HttpStatus.CONFLICT.toString(),
+                    "An object with that UID already exists." ), headers, HttpStatus.CONFLICT );
             }
             else if ( organisationUnit.getCode() != null && organisationUnitService.getOrganisationUnitByCode( organisationUnit.getCode() ) != null )
             {
-                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( "An object with that code already exists." ), headers, HttpStatus.CONFLICT );
+                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( HttpStatus.CONFLICT.toString(),
+                    "An object with that code already exists." ), headers, HttpStatus.CONFLICT );
             }
 
             organisationUnitService.addOrganisationUnit( organisationUnit );
@@ -526,8 +529,8 @@
         }
     }
 
-    @RequestMapping( value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE )
-    @PreAuthorize( "hasRole('F_FRED_UPDATE') or hasRole('ALL')" )
+    @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
+    @PreAuthorize("hasRole('F_FRED_UPDATE') or hasRole('ALL')")
     public ResponseEntity<String> updateFacility( @PathVariable String id, @RequestBody Facility facility, HttpServletRequest request ) throws Exception
     {
         HttpHeaders headers = new HttpHeaders();
@@ -544,8 +547,8 @@
             }
             catch ( IllegalArgumentException ignored )
             {
-                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( "Bad id: (does not match expected UUID string format)" ),
-                    headers, HttpStatus.PRECONDITION_FAILED );
+                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( HttpStatus.PRECONDITION_FAILED.toString(),
+                    "Bad id: (does not match expected UUID string format)" ), headers, HttpStatus.PRECONDITION_FAILED );
             }
         }
 
@@ -570,15 +573,15 @@
 
                 if ( !ETag.equals( request.getHeader( "If-Match" ) ) )
                 {
-                    return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( "ETag provided does not match current ETag of facility" ),
-                        headers, HttpStatus.PRECONDITION_FAILED );
+                    return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( HttpStatus.PRECONDITION_FAILED.toString(),
+                        "ETag provided does not match current ETag of facility" ), headers, HttpStatus.PRECONDITION_FAILED );
                 }
             }
 
             if ( organisationUnit == null )
             {
-                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( "No object with that identifier exists." ),
-                    headers, HttpStatus.NOT_FOUND );
+                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( HttpStatus.NOT_FOUND.toString(),
+                    "No object with that identifier exists." ), headers, HttpStatus.NOT_FOUND );
             }
             else if ( organisationUnitUpdate.getCode() != null )
             {
@@ -586,8 +589,8 @@
 
                 if ( ouByCode != null && !organisationUnit.getUid().equals( ouByCode.getUid() ) )
                 {
-                    return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( "Another object with the same code already exists." ),
-                        headers, HttpStatus.CONFLICT );
+                    return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( HttpStatus.CONFLICT.toString(),
+                        "Another object with the same code already exists." ), headers, HttpStatus.CONFLICT );
                 }
             }
 
@@ -625,8 +628,8 @@
     // DELETE JSON
     //--------------------------------------------------------------------------
 
-    @RequestMapping( value = "/{id}", method = RequestMethod.DELETE )
-    @PreAuthorize( "hasRole('F_FRED_DELETE') or hasRole('ALL')" )
+    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
+    @PreAuthorize("hasRole('F_FRED_DELETE') or hasRole('ALL')")
     public ResponseEntity<Void> deleteFacility( @PathVariable String id ) throws HierarchyViolationException
     {
         OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( id );
@@ -645,7 +648,7 @@
     // EXCEPTION HANDLERS
     //--------------------------------------------------------------------------
 
-    @ExceptionHandler( { DeleteNotAllowedException.class, HierarchyViolationException.class } )
+    @ExceptionHandler({ DeleteNotAllowedException.class, HierarchyViolationException.class })
     public ResponseEntity<String> exceptionHandler( Exception ex )
     {
         return new ResponseEntity<String>( ex.getMessage(), HttpStatus.FORBIDDEN );

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityServiceController.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityServiceController.java	2013-02-04 14:57:16 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityServiceController.java	2013-03-06 09:21:16 +0000
@@ -140,15 +140,18 @@
 
             if ( organisationUnitService.getOrganisationUnit( organisationUnit.getUid() ) != null )
             {
-                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( "An object with that ID already exists." ), headers, HttpStatus.CONFLICT );
+                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( HttpStatus.CONFLICT.toString(),
+                    "An object with that ID already exists." ), headers, HttpStatus.CONFLICT );
             }
             else if ( organisationUnitService.getOrganisationUnitByName( organisationUnit.getName() ) != null )
             {
-                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( "An object with that name already exists." ), headers, HttpStatus.CONFLICT );
+                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( HttpStatus.CONFLICT.toString(),
+                    "An object with that name already exists." ), headers, HttpStatus.CONFLICT );
             }
             else if ( organisationUnit.getCode() != null && organisationUnitService.getOrganisationUnitByCode( organisationUnit.getCode() ) != null )
             {
-                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( "An object with that code already exists." ), headers, HttpStatus.CONFLICT );
+                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( HttpStatus.CONFLICT.toString(),
+                    "An object with that code already exists." ), headers, HttpStatus.CONFLICT );
             }
 
             return new ResponseEntity<String>( json, headers, HttpStatus.OK );
@@ -176,8 +179,8 @@
 
             if ( ou == null )
             {
-                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( "No object with that identifier exists." ),
-                    headers, HttpStatus.NOT_FOUND );
+                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( HttpStatus.NOT_FOUND.toString(),
+                    "No object with that identifier exists." ), headers, HttpStatus.NOT_FOUND );
             }
             else if ( organisationUnit.getCode() != null )
             {
@@ -185,8 +188,8 @@
 
                 if ( ouByCode != null && !ou.getUid().equals( ouByCode.getUid() ) )
                 {
-                    return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( "Another object with the same code already exists." ),
-                        headers, HttpStatus.CONFLICT );
+                    return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( HttpStatus.CONFLICT.toString(),
+                        "Another object with the same code already exists." ), headers, HttpStatus.CONFLICT );
                 }
             }
 

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/MessageResponse.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/MessageResponse.java	2012-12-11 10:59:00 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/MessageResponse.java	2013-03-06 09:21:16 +0000
@@ -32,21 +32,31 @@
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
-@JsonPropertyOrder( value = { "message", "moreInfo" } )
+@JsonPropertyOrder( value = { "code", "message" } )
 public class MessageResponse
 {
+    private String code;
+
     private String message;
 
-    private String moreInfo;
-
     public MessageResponse()
     {
     }
 
-    public MessageResponse( String message, String moreInfo )
+    public MessageResponse( String code, String message )
     {
+        this.code = code;
         this.message = message;
-        this.moreInfo = moreInfo;
+    }
+
+    public String getCode()
+    {
+        return code;
+    }
+
+    public void setCode( String code )
+    {
+        this.code = code;
     }
 
     public String getMessage()
@@ -58,14 +68,4 @@
     {
         this.message = message;
     }
-
-    public String getMoreInfo()
-    {
-        return moreInfo;
-    }
-
-    public void setMoreInfo( String moreInfo )
-    {
-        this.moreInfo = moreInfo;
-    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/MessageResponseUtils.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/MessageResponseUtils.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/MessageResponseUtils.java	2013-03-06 09:21:16 +0000
@@ -50,12 +50,12 @@
 
     public static String jsonMessage( String message ) throws IOException
     {
-        return messageToJson( new MessageResponse( message, null ) );
+        return messageToJson( new MessageResponse( null, message ) );
     }
 
-    public static String jsonMessage( String message, String moreInfo ) throws IOException
+    public static String jsonMessage( String code, String message ) throws IOException
     {
-        return messageToJson( new MessageResponse( message, moreInfo ) );
+        return messageToJson( new MessageResponse( code, message ) );
     }
 
     public static String messageToJson( MessageResponse messageResponse ) throws IOException

=== added file 'dhis-2/dhis-web/dhis-web-api-fred/src/test/java/org/hisp/dhis/web/webapi/v1/utils/OrganisationUnitToFacilityConverterTest.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/test/java/org/hisp/dhis/web/webapi/v1/utils/OrganisationUnitToFacilityConverterTest.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/test/java/org/hisp/dhis/web/webapi/v1/utils/OrganisationUnitToFacilityConverterTest.java	2013-03-06 09:21:16 +0000
@@ -0,0 +1,147 @@
+package org.hisp.dhis.web.webapi.v1.utils;
+
+/*
+ * Copyright (c) 2004-2012, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import org.hisp.dhis.dataset.DataSet;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.web.webapi.v1.domain.Facility;
+import org.hisp.dhis.web.webapi.v1.domain.Identifier;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.List;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class OrganisationUnitToFacilityConverterTest
+{
+    private OrganisationUnitToFacilityConverter converter;
+
+    @Before
+    public void setup()
+    {
+        converter = new OrganisationUnitToFacilityConverter();
+    }
+
+    @Test
+    public void testName()
+    {
+        OrganisationUnit organisationUnit = new OrganisationUnit( "OrgUnit1" );
+        Facility facility = converter.convert( organisationUnit );
+
+        Assert.assertEquals( facility.getName(), organisationUnit.getName() );
+    }
+
+    @Test
+    public void testParent()
+    {
+        OrganisationUnit organisationUnit = new OrganisationUnit( "OrgUnit1" );
+        OrganisationUnit parent = new OrganisationUnit( "parent" );
+        organisationUnit.setParent( parent );
+
+        organisationUnit.setAutoFields();
+        parent.setAutoFields();
+
+        Facility facility = converter.convert( organisationUnit );
+
+        String parent1 = (String) facility.getProperties().get( "parent" );
+        Assert.assertEquals( parent.getUid(), parent1 );
+    }
+
+    @Test
+    public void testIdentifiers()
+    {
+        OrganisationUnit organisationUnit = new OrganisationUnit( "OrgUnit1" );
+        organisationUnit.setAutoFields();
+        organisationUnit.setCode( "code" );
+
+        Facility facility = converter.convert( organisationUnit );
+
+        boolean foundUid = false;
+        boolean foundCode = false;
+
+        for ( Identifier identifier : facility.getIdentifiers() )
+        {
+            if ( identifier.getAgency().equalsIgnoreCase( Identifier.DHIS2_AGENCY ) )
+            {
+                if ( identifier.getContext().equalsIgnoreCase( Identifier.DHIS2_UID_CONTEXT ) )
+                {
+                    Assert.assertEquals( identifier.getId(), organisationUnit.getUid() );
+                    foundUid = true;
+                }
+                else if ( identifier.getContext().equalsIgnoreCase( Identifier.DHIS2_CODE_CONTEXT ) )
+                {
+                    Assert.assertEquals( identifier.getId(), organisationUnit.getCode() );
+                    foundCode = true;
+                }
+            }
+        }
+
+        Assert.assertTrue( foundUid );
+        Assert.assertTrue( foundCode );
+    }
+
+    @Test
+    public void testLevel()
+    {
+        OrganisationUnit organisationUnit = new OrganisationUnit( "OrgUnit1" );
+
+        Facility facility = converter.convert( organisationUnit );
+
+        Integer level = (Integer) facility.getProperties().get( "level" );
+
+        Assert.assertTrue( level.equals( organisationUnit.getOrganisationUnitLevel() ) );
+    }
+
+    @Test
+    public void testDataSets()
+    {
+        OrganisationUnit organisationUnit = new OrganisationUnit( "OrgUnit1" );
+        DataSet dataSet1 = new DataSet( "dataSet1" );
+        DataSet dataSet2 = new DataSet( "dataSet1" );
+        DataSet dataSet3 = new DataSet( "dataSet1" );
+
+        dataSet1.setAutoFields();
+        dataSet2.setAutoFields();
+        dataSet3.setAutoFields();
+
+        organisationUnit.getDataSets().add( dataSet1 );
+        organisationUnit.getDataSets().add( dataSet2 );
+        organisationUnit.getDataSets().add( dataSet3 );
+
+        Facility facility = converter.convert( organisationUnit );
+
+        List<String> dataSets = (List<String>) facility.getProperties().get( "dataSets" );
+
+        Assert.assertTrue( dataSets.contains( dataSet1.getUid() ) );
+        Assert.assertTrue( dataSets.contains( dataSet2.getUid() ) );
+        Assert.assertTrue( dataSets.contains( dataSet3.getUid() ) );
+    }
+}