← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10060: FRED-API: tests for DELETE

 

------------------------------------------------------------
revno: 10060
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-03-08 07:56:18 +0300
message:
  FRED-API: tests for DELETE
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/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/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-07 18:52:55 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java	2013-03-08 04:56:18 +0000
@@ -663,18 +663,28 @@
 
     @RequestMapping( value = "/{id}", method = RequestMethod.DELETE )
     @PreAuthorize( "hasRole('F_FRED_DELETE') or hasRole('ALL')" )
-    public ResponseEntity<Void> deleteFacility( @PathVariable String id ) throws HierarchyViolationException
+    public ResponseEntity<String> deleteFacility( @PathVariable String id ) throws HierarchyViolationException, IOException
     {
-        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( id );
-
-        if ( organisationUnit != null )
-        {
-            organisationUnitService.deleteOrganisationUnit( organisationUnit );
-
-            return new ResponseEntity<Void>( HttpStatus.OK );
-        }
-
-        return new ResponseEntity<Void>( HttpStatus.NOT_FOUND );
+        OrganisationUnit organisationUnit;
+
+        if ( id.length() == 11 )
+        {
+            organisationUnit = organisationUnitService.getOrganisationUnit( id );
+        }
+        else
+        {
+            organisationUnit = organisationUnitService.getOrganisationUnitByUuid( id );
+        }
+
+        if ( organisationUnit == null )
+        {
+            return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( HttpStatus.NOT_FOUND.toString(),
+                "Facility with that ID not found" ), HttpStatus.NOT_FOUND );
+        }
+
+        organisationUnitService.deleteOrganisationUnit( organisationUnit );
+
+        return new ResponseEntity<String>( HttpStatus.OK );
     }
 
     //--------------------------------------------------------------------------

=== 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 15:50:24 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/test/java/org/hisp/dhis/web/webapi/v1/controller/FacilityControllerTest.java	2013-03-08 04:56:18 +0000
@@ -57,6 +57,10 @@
         mvc.perform( get( "/api-fred/" ).session( session ) ).andExpect( redirectedUrl( "/api-fred/v1" ) );
     }
 
+    //---------------------------------------------------------------------------------------------
+    // Test GET
+    //---------------------------------------------------------------------------------------------
+
     @Test
     public void testGetFacilitiesWithALL() throws Exception
     {
@@ -154,6 +158,10 @@
             .andExpect( status().isOk() );
     }
 
+    //---------------------------------------------------------------------------------------------
+    // Test PUT
+    //---------------------------------------------------------------------------------------------
+
     @Test
     public void testPutFacility404() throws Exception
     {
@@ -163,6 +171,10 @@
             .andExpect( status().isNotFound() );
     }
 
+    //---------------------------------------------------------------------------------------------
+    // Test POST
+    //---------------------------------------------------------------------------------------------
+
     @Test
     public void testPostName() throws Exception
     {
@@ -211,4 +223,41 @@
             .andExpect( jsonPath( "$.name" ).value( "FacilityA" ) )
             .andExpect( status().isCreated() );
     }
+
+    //---------------------------------------------------------------------------------------------
+    // Test DELETE
+    //---------------------------------------------------------------------------------------------
+
+    @Test
+    public void testDeleteFacility404() throws Exception
+    {
+        MockHttpSession session = getSession( "ALL" );
+
+        mvc.perform( delete( "/v1/facilities/abc123" ).session( session ) )
+            .andExpect( status().isNotFound() );
+    }
+
+    @Test
+    public void testDeleteFacilityUid() throws Exception
+    {
+        OrganisationUnit organisationUnit = createOrganisationUnit( 'A' );
+        manager.save( organisationUnit );
+
+        MockHttpSession session = getSession( "ALL" );
+
+        mvc.perform( delete( "/v1/facilities/" + organisationUnit.getUid() ).session( session ) )
+            .andExpect( status().isOk() );
+    }
+
+    @Test
+    public void testDeleteFacilityUuid() throws Exception
+    {
+        OrganisationUnit organisationUnit = createOrganisationUnit( 'A' );
+        manager.save( organisationUnit );
+
+        MockHttpSession session = getSession( "ALL" );
+
+        mvc.perform( delete( "/v1/facilities/" + organisationUnit.getUuid() ).session( session ) )
+            .andExpect( status().isOk() );
+    }
 }