Update model-loader to spring-boot 2.4
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / entity / model / TestModelArtifactHandler.java
index 2a951a8..fac50cb 100644 (file)
 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.hamcrest.Matchers.hasSize;
+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.BeforeEach;
+import org.junit.jupiter.api.Test;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.onap.aai.modelloader.config.ModelLoaderConfig;
@@ -51,7 +55,7 @@ public class TestModelArtifactHandler {
     @Mock
     private AaiRestClient aaiClient;
 
-    @Before
+    @BeforeEach
     public void setupMocks() {
         MockitoAnnotations.initMocks(this);
     }
@@ -61,6 +65,7 @@ public class TestModelArtifactHandler {
         ModelArtifactHandler handler = new ModelArtifactHandler(config);
         handler.pushArtifacts(Collections.emptyList(), "", Collections.emptyList(), aaiClient);
         handler.rollback(Collections.emptyList(), "", aaiClient);
+        assertTrue(true);
     }
 
     @Test
@@ -72,6 +77,7 @@ public class TestModelArtifactHandler {
         List<Artifact> artifacts = Collections.singletonList(new ModelArtifact());
         handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient);
         handler.rollback(Collections.emptyList(), "", aaiClient);
+        assertThat(artifacts, hasSize(1));
     }
 
     @Test
@@ -89,7 +95,7 @@ public class TestModelArtifactHandler {
 
         ModelArtifactHandler handler = new ModelArtifactHandler(config);
         boolean pushed = handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient);
-        assertThat(pushed, is(true));
+        assertTrue(pushed);
         handler.rollback(artifacts, "", aaiClient);
     }
 
@@ -120,5 +126,50 @@ public class TestModelArtifactHandler {
         assertThat(pushed, is(true));
         handler.rollback(artifacts, "", aaiClient);
     }
+
+    @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());
+
+        OperationResult putResult = mock(OperationResult.class);
+        when(aaiClient.putResource(any(), any(), any(), any())).thenReturn(putResult);
+        when(putResult.getResultCode()).thenReturn(Response.Status.BAD_REQUEST.getStatusCode());
+
+        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);
+
+        checkRollback(Collections.singletonList(new ModelArtifact()));
+    }
+
+    private void checkRollback(List<Artifact> artifacts) {
+        ModelArtifactHandler handler = new ModelArtifactHandler(config);
+        boolean pushed = handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient);
+        assertThat(pushed, is(false));
+        handler.rollback(artifacts, "", aaiClient);
+    }
 }