X-Git-Url: https://gerrit.onap.org/r/gitweb?p=aai%2Fmodel-loader.git;a=blobdiff_plain;f=src%2Ftest%2Fjava%2Forg%2Fonap%2Faai%2Fmodelloader%2Fentity%2Fmodel%2FTestModelArtifactHandler.java;h=fac50cb17b9ad99c3fd8e38a3926f48038f1b1b2;hp=b7a8cf265cd705c5ce4a9b9e4488742e264eb3d6;hb=HEAD;hpb=f1dc7620477e26db40a91c43ded1dbfa8fe3ccc0 diff --git a/src/test/java/org/onap/aai/modelloader/entity/model/TestModelArtifactHandler.java b/src/test/java/org/onap/aai/modelloader/entity/model/TestModelArtifactHandler.java index b7a8cf2..990a83f 100644 --- a/src/test/java/org/onap/aai/modelloader/entity/model/TestModelArtifactHandler.java +++ b/src/test/java/org/onap/aai/modelloader/entity/model/TestModelArtifactHandler.java @@ -21,23 +21,27 @@ package org.onap.aai.modelloader.entity.model; import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Matchers.any; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import javax.ws.rs.core.Response; -import org.junit.Before; -import org.junit.Test; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.onap.aai.modelloader.config.AaiProperties; import org.onap.aai.modelloader.config.ModelLoaderConfig; import org.onap.aai.modelloader.entity.Artifact; import org.onap.aai.modelloader.restclient.AaiRestClient; -import org.onap.aai.restclient.client.OperationResult; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; /** * Test the Model Artifact Handler using Mocks @@ -51,61 +55,54 @@ public class TestModelArtifactHandler { @Mock private AaiRestClient aaiClient; - @Before + private static AaiProperties aaiProperties = new AaiProperties(); + + @BeforeAll + public static void setup() { + aaiProperties.setBaseUrl("http://aai.onap:80"); + aaiProperties.setModelUrl("/aai/%s/service-design-and-creation/models/model/"); + aaiProperties.setNamedQueryUrl("/aai/%s/service-design-and-creation/named-queries/named-query/"); + aaiProperties.setVnfImageUrl("/aai/%s/service-design-and-creation/vnf-images"); + } + + @BeforeEach public void setupMocks() { - MockitoAnnotations.initMocks(this); + MockitoAnnotations.openMocks(this); } @Test public void testEmptyLists() { - ModelArtifactHandler handler = new ModelArtifactHandler(config); + ModelArtifactHandler handler = new ModelArtifactHandler(aaiProperties); handler.pushArtifacts(Collections.emptyList(), "", Collections.emptyList(), aaiClient); handler.rollback(Collections.emptyList(), "", aaiClient); - } - - @Test - public void testSingleItemList() { - when(config.getAaiBaseUrl()).thenReturn(""); - when(config.getAaiModelUrl(any())).thenReturn(""); - - ModelArtifactHandler handler = new ModelArtifactHandler(config); - List artifacts = Collections.singletonList(new ModelArtifact()); - handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient); - handler.rollback(Collections.emptyList(), "", aaiClient); + assertTrue(true); } @Test public void testPushExistingModelsWithRollback() { - when(config.getAaiBaseUrl()).thenReturn(""); - when(config.getAaiModelUrl(any())).thenReturn(""); - - OperationResult operationResult = mock(OperationResult.class); - when(aaiClient.getResource(any(), any(), any())).thenReturn(operationResult); - when(operationResult.getResultCode()).thenReturn(Response.Status.OK.getStatusCode()); + ResponseEntity operationResult = mock(ResponseEntity.class); + when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(operationResult); + when(operationResult.getStatusCode()).thenReturn(HttpStatus.OK); List artifacts = new ArrayList<>(); Artifact artifact = new ModelArtifact(); artifacts.add(artifact); - ModelArtifactHandler handler = new ModelArtifactHandler(config); + ModelArtifactHandler handler = new ModelArtifactHandler(aaiProperties); boolean pushed = handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient); - assertThat(pushed, is(true)); + assertTrue(pushed); handler.rollback(artifacts, "", aaiClient); } @Test public void testPushNewModelsWithRollback() { - when(config.getAaiBaseUrl()).thenReturn(""); - when(config.getAaiModelUrl(any())).thenReturn(""); - when(config.getAaiNamedQueryUrl(any())).thenReturn(""); - - OperationResult getResult = mock(OperationResult.class); - when(aaiClient.getResource(any(), any(), any())).thenReturn(getResult); - when(getResult.getResultCode()).thenReturn(Response.Status.NOT_FOUND.getStatusCode()); + ResponseEntity getResult = mock(ResponseEntity.class); + when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(getResult); + when(getResult.getStatusCode()).thenReturn(HttpStatus.NOT_FOUND); - OperationResult putResult = mock(OperationResult.class); - when(aaiClient.putResource(any(), any(), any(), any())).thenReturn(putResult); - when(putResult.getResultCode()).thenReturn(Response.Status.CREATED.getStatusCode()); + ResponseEntity putResult = mock(ResponseEntity.class); + when(aaiClient.putResource(any(), any(), any(), any(), any())).thenReturn(putResult); + when(putResult.getStatusCode()).thenReturn(HttpStatus.CREATED); List artifacts = new ArrayList<>(); artifacts.add(new ModelArtifact()); @@ -115,7 +112,7 @@ public class TestModelArtifactHandler { artifacts.add(namedQueryArtifact); List completedArtifacts = new ArrayList<>(); - ModelArtifactHandler handler = new ModelArtifactHandler(config); + ModelArtifactHandler handler = new ModelArtifactHandler(aaiProperties); boolean pushed = handler.pushArtifacts(artifacts, "", completedArtifacts, aaiClient); assertThat(pushed, is(true)); handler.rollback(artifacts, "", aaiClient); @@ -123,47 +120,30 @@ public class TestModelArtifactHandler { @Test public void testPushNewModelsBadRequest() { - when(config.getAaiBaseUrl()).thenReturn(""); - when(config.getAaiModelUrl(any())).thenReturn(""); - when(config.getAaiNamedQueryUrl(any())).thenReturn(""); - - OperationResult getResult = mock(OperationResult.class); - when(aaiClient.getResource(any(), any(), any())).thenReturn(getResult); - when(getResult.getResultCode()).thenReturn(Response.Status.NOT_FOUND.getStatusCode()); + ResponseEntity getResult = mock(ResponseEntity.class); + when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(getResult); + when(getResult.getStatusCode()).thenReturn(HttpStatus.NOT_FOUND); - OperationResult putResult = mock(OperationResult.class); - when(aaiClient.putResource(any(), any(), any(), any())).thenReturn(putResult); - when(putResult.getResultCode()).thenReturn(Response.Status.BAD_REQUEST.getStatusCode()); + ResponseEntity putResult = mock(ResponseEntity.class); + when(aaiClient.putResource(any(), any(), any(), any(), any())).thenReturn(putResult); + when(putResult.getStatusCode()).thenReturn(HttpStatus.BAD_REQUEST); checkRollback(Collections.singletonList(new ModelArtifact())); } @Test public void testBadRequestResourceModelResult() { - when(config.getAaiBaseUrl()).thenReturn(""); - when(config.getAaiModelUrl(any())).thenReturn(""); - - OperationResult operationResult = mock(OperationResult.class); - when(aaiClient.getResource(any(), any(), any())).thenReturn(operationResult); - when(operationResult.getResultCode()).thenReturn(Response.Status.BAD_REQUEST.getStatusCode()); - - checkRollback(Collections.singletonList(new ModelArtifact())); - } - - @Test - public void testNullResourceModelResult() { - when(config.getAaiBaseUrl()).thenReturn(""); - when(config.getAaiModelUrl(any())).thenReturn(""); - when(aaiClient.getResource(any(), any(), any())).thenReturn(null); + ResponseEntity operationResult = mock(ResponseEntity.class); + when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(operationResult); + when(operationResult.getStatusCode()).thenReturn(HttpStatus.BAD_REQUEST); checkRollback(Collections.singletonList(new ModelArtifact())); } private void checkRollback(List artifacts) { - ModelArtifactHandler handler = new ModelArtifactHandler(config); + ModelArtifactHandler handler = new ModelArtifactHandler(aaiProperties); boolean pushed = handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient); assertThat(pushed, is(false)); handler.rollback(artifacts, "", aaiClient); } } -