diff --git a/pom.xml b/pom.xml index df2d547..37ea15f 100644 --- a/pom.xml +++ b/pom.xml @@ -68,7 +68,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M5 + 3.0.0-M6 false diff --git a/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/AbstractResourceOperation.java b/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/AbstractResourceOperation.java index d6f4e40..da7fc4e 100644 --- a/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/AbstractResourceOperation.java +++ b/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/AbstractResourceOperation.java @@ -27,8 +27,10 @@ */ package org.hisp.dhis.integration.sdk.internal.operation; +import okhttp3.HttpUrl; import okhttp3.OkHttpClient; - +import okhttp3.Request; +import org.hisp.dhis.integration.sdk.api.Dhis2Response; import org.hisp.dhis.integration.sdk.api.converter.ConverterFactory; import org.hisp.dhis.integration.sdk.api.operation.ResourceOperation; @@ -42,6 +44,34 @@ public AbstractResourceOperation( String baseApiUrl, String path, OkHttpClient h super( baseApiUrl, path, httpClient, converterFactory, pathParams ); } + @Override + public Dhis2Response doTransfer( HttpUrl httpUrl ) { + HttpUrl.Builder httpUrlBuilder = httpUrl.newBuilder(); + Request.Builder requestBuilder = new Request.Builder().url( httpUrlBuilder.build() ) + .addHeader( "Content-Type", "application/json" ); + final byte[] bytes; + if ( resource != null ) + { + if ( resource instanceof String ) + { + bytes = ((String) resource).getBytes(); + } + else + { + bytes = converterFactory.createRequestConverter( requestBuilder.build() ).convert( resource ) + .getBytes(); + } + } + else + { + bytes = new byte[] {}; + } + + return doResourceTransfer(bytes, requestBuilder); + } + + protected abstract Dhis2Response doResourceTransfer( byte[] resourceAsBytes, Request.Builder requestBuilder ); + @Override public ResourceOperation withResource( Object resource ) { diff --git a/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/DefaultDeleteOperation.java b/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/DefaultDeleteOperation.java index e0d5a51..9e1f940 100644 --- a/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/DefaultDeleteOperation.java +++ b/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/DefaultDeleteOperation.java @@ -27,9 +27,8 @@ */ package org.hisp.dhis.integration.sdk.internal.operation; -import okhttp3.HttpUrl; import okhttp3.OkHttpClient; - +import okhttp3.Request; import org.hisp.dhis.integration.sdk.api.Dhis2Response; import org.hisp.dhis.integration.sdk.api.converter.ConverterFactory; import org.hisp.dhis.integration.sdk.api.operation.DeleteOperation; @@ -44,13 +43,7 @@ public DefaultDeleteOperation( String baseApiUrl, String path, OkHttpClient http } @Override - public Dhis2Response transfer() - { - return null; - } - - @Override - public Dhis2Response doTransfer( HttpUrl httpUrl ) + protected Dhis2Response doResourceTransfer( byte[] resourceAsBytes, Request.Builder requestBuilder ) { throw new UnsupportedOperationException(); } diff --git a/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/DefaultPatchOperation.java b/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/DefaultPatchOperation.java index 782efab..f85bca0 100644 --- a/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/DefaultPatchOperation.java +++ b/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/DefaultPatchOperation.java @@ -27,9 +27,8 @@ */ package org.hisp.dhis.integration.sdk.internal.operation; -import okhttp3.HttpUrl; import okhttp3.OkHttpClient; - +import okhttp3.Request; import org.hisp.dhis.integration.sdk.api.Dhis2Response; import org.hisp.dhis.integration.sdk.api.converter.ConverterFactory; import org.hisp.dhis.integration.sdk.api.operation.PatchOperation; @@ -43,13 +42,7 @@ public DefaultPatchOperation( String baseApiUrl, String path, OkHttpClient httpC } @Override - public Dhis2Response transfer() - { - return null; - } - - @Override - public Dhis2Response doTransfer( HttpUrl httpUrl ) + protected Dhis2Response doResourceTransfer( byte[] resourceAsBytes, Request.Builder requestBuilder ) { throw new UnsupportedOperationException(); } diff --git a/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/DefaultPostOperation.java b/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/DefaultPostOperation.java index 696eaa8..d8344ba 100644 --- a/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/DefaultPostOperation.java +++ b/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/DefaultPostOperation.java @@ -47,25 +47,13 @@ public DefaultPostOperation( String baseApiUrl, String path, OkHttpClient httpCl } @Override - public Dhis2Response doTransfer( HttpUrl httpUrl ) + protected Dhis2Response doResourceTransfer( byte[] resourceAsBytes, Request.Builder requestBuilder ) { - HttpUrl.Builder httpUrlBuilder = httpUrl.newBuilder(); - Request.Builder requestBuilder = new Request.Builder().url( httpUrlBuilder.build() ); - Request request; - converterFactory.createRequestConverter( requestBuilder.build() ).convert( resource ); - if ( resource != null ) - { - request = requestBuilder.post( RequestBody.create( - converterFactory.createRequestConverter( requestBuilder.build() ).convert( resource ).getBytes(), - MediaType.parse( "application/json" ) ) ).build(); - } - else - { - request = requestBuilder.post( RequestBody.create( new byte[] {}, MediaType.parse( "application/json" ) ) ) - .build(); - } + Request request = requestBuilder.post( RequestBody.create( resourceAsBytes ) ) + .build(); okhttp3.Response response = onHttpResponse( () -> httpClient.newCall( request ).execute() ); return new DefaultDhis2Response( response, converterFactory ); } + } diff --git a/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/DefaultPutOperation.java b/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/DefaultPutOperation.java index 1315e83..118f477 100644 --- a/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/DefaultPutOperation.java +++ b/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/DefaultPutOperation.java @@ -47,25 +47,14 @@ public DefaultPutOperation( String baseApiUrl, String path, OkHttpClient httpCli } @Override - public Dhis2Response doTransfer( HttpUrl httpUrl ) + protected Dhis2Response doResourceTransfer( byte[] resourceAsBytes, Request.Builder requestBuilder ) { - Request.Builder requestBuilder = new Request.Builder().url( httpUrl ); - Request request; - converterFactory.createRequestConverter( requestBuilder.build() ).convert( resource ); - if ( resource != null ) - { - request = requestBuilder.put( RequestBody.create( - converterFactory.createRequestConverter( requestBuilder.build() ).convert( resource ).getBytes(), - MediaType.parse( "application/json" ) ) ).build(); - } - else - { - request = requestBuilder.put( RequestBody.create( new byte[] {}, MediaType.parse( "application/json" ) ) ) - .build(); - } + Request request = requestBuilder.put( RequestBody.create( resourceAsBytes ) ) + .build(); okhttp3.Response response = onHttpResponse( () -> httpClient.newCall( request ).execute() ); return new DefaultDhis2Response( response, converterFactory ); } + } diff --git a/src/test/java/org/hisp/dhis/integration/sdk/Environment.java b/src/test/java/org/hisp/dhis/integration/sdk/Environment.java index 874ce86..90b70cc 100644 --- a/src/test/java/org/hisp/dhis/integration/sdk/Environment.java +++ b/src/test/java/org/hisp/dhis/integration/sdk/Environment.java @@ -33,6 +33,7 @@ import java.util.Date; import java.util.Map; +import com.fasterxml.jackson.databind.ObjectMapper; import org.hisp.dhis.api.v2_37_6.model.OrganisationUnit; import org.hisp.dhis.api.v2_37_6.model.OrganisationUnitLevel; import org.hisp.dhis.api.v2_37_6.model.WebMessage; @@ -98,10 +99,11 @@ private static void importMetaData() String metaData; try { - metaData = new String( + + metaData = new ObjectMapper().writeValueAsString( new String( Thread.currentThread().getContextClassLoader().getResourceAsStream( "MLAG00_1.2.1_DHIS2.37.json" ) .readAllBytes(), - Charset.defaultCharset() ); + Charset.defaultCharset() ) ); } catch ( IOException e ) { diff --git a/src/test/java/org/hisp/dhis/integration/sdk/internal/operation/AbstractResourceOperationTestCase.java b/src/test/java/org/hisp/dhis/integration/sdk/internal/operation/AbstractResourceOperationTestCase.java new file mode 100644 index 0000000..6b51fdb --- /dev/null +++ b/src/test/java/org/hisp/dhis/integration/sdk/internal/operation/AbstractResourceOperationTestCase.java @@ -0,0 +1,57 @@ +package org.hisp.dhis.integration.sdk.internal.operation; + +import okhttp3.Request; +import org.hisp.dhis.integration.sdk.api.Dhis2Response; +import org.hisp.dhis.integration.sdk.api.operation.ResourceOperation; +import org.hisp.dhis.integration.sdk.internal.converter.JacksonConverterFactory; +import org.junit.jupiter.api.Test; + +import java.util.Map; +import java.util.concurrent.CountDownLatch; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class AbstractResourceOperationTestCase +{ + @Test + public void testTransferGivenStringResource() + { + + CountDownLatch countDownLatch = new CountDownLatch( 1 ); + ResourceOperation resourceOperation = new AbstractResourceOperation( "http://example", "", null, + new JacksonConverterFactory() ) + { + @Override + protected Dhis2Response doResourceTransfer( byte[] resourceAsBytes, + Request.Builder requestBuilder ) + { + assertEquals( "{\"foo\":\"acme\"}", new String( resourceAsBytes ) ); + countDownLatch.countDown(); + return null; + } + }.withResource( "{\"foo\":\"acme\"}" ); + resourceOperation.transfer(); + assertEquals( 0, countDownLatch.getCount() ); + } + + @Test + public void testTransferGivenNonStringResource() + { + + CountDownLatch countDownLatch = new CountDownLatch( 1 ); + ResourceOperation resourceOperation = new AbstractResourceOperation( "http://example", "", null, + new JacksonConverterFactory() ) + { + @Override + protected Dhis2Response doResourceTransfer( byte[] resourceAsBytes, + Request.Builder requestBuilder ) + { + assertEquals( "{\"foo\":\"acme\"}", new String( resourceAsBytes ) ); + countDownLatch.countDown(); + return null; + } + }.withResource( Map.of( "foo", "acme" ) ); + resourceOperation.transfer(); + assertEquals( 0, countDownLatch.getCount() ); + } +}