Update babel dependency in model-loader
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / restclient / TestBabelServiceClient.java
index 8da5f89..d82bff0 100644 (file)
  */
 package org.onap.aai.modelloader.restclient;
 
-import static javax.servlet.http.HttpServletResponse.SC_OK;
-import static org.apache.commons.io.IOUtils.write;
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
+import static org.hamcrest.MatcherAssert.assertThat;
 
-import com.google.gson.Gson;
 import java.io.IOException;
 import java.net.URISyntaxException;
 import java.nio.file.Files;
 import java.nio.file.Paths;
-import java.util.ArrayList;
+import java.util.Base64;
 import java.util.List;
-import java.util.Properties;
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import org.eclipse.jetty.server.Handler;
-import org.eclipse.jetty.server.Request;
-import org.eclipse.jetty.server.Server;
-import org.eclipse.jetty.server.handler.AbstractHandler;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
+
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
 import org.onap.aai.babel.service.data.BabelArtifact;
-import org.onap.aai.modelloader.config.ModelLoaderConfig;
-import org.onap.aai.modelloader.service.HttpsBabelServiceClientFactory;
+import org.onap.aai.babel.service.data.BabelRequest;
+import org.onap.aai.modelloader.BabelClientTestConfiguration;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
+import org.springframework.context.annotation.Import;
+import org.springframework.test.annotation.DirtiesContext;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.github.tomakehurst.wiremock.client.WireMock;
 
 /**
  * Local testing of the Babel service client.
  *
  */
+@SpringBootTest
+@DirtiesContext
+@AutoConfigureWireMock(port = 0)
+@Import(BabelClientTestConfiguration.class)
 public class TestBabelServiceClient {
 
-    private Server server;
-    private String responseBody;
+    @Value("${wiremock.server.port}")
+    private int wiremockPort;
 
-    @Before
-    public void startJetty() throws Exception {
-        List<BabelArtifact> response = new ArrayList<>();
-        response.add(new BabelArtifact("", null, ""));
-        response.add(new BabelArtifact("", null, ""));
-        response.add(new BabelArtifact("", null, ""));
-        responseBody = new Gson().toJson(response);
+    @Autowired BabelServiceClient client;
 
-        server = new Server(8080);
-        server.setHandler(getMockHandler());
-        server.start();
-    }
-
-    @After
-    public void stopJetty() throws Exception {
-        server.stop();
+    @BeforeAll
+    public static void setup() throws JsonProcessingException {
+        ObjectMapper objectMapper = new ObjectMapper();
+        List<BabelArtifact> artifacts = List.of(
+            new BabelArtifact("art1", null, ""),
+            new BabelArtifact("art2", null, ""),
+            new BabelArtifact("art3", null, ""));
+        WireMock.stubFor(
+            WireMock.post(WireMock.urlEqualTo("/services/babel-service/v1/app/generateArtifacts"))
+                .withHeader("X-TransactionId", WireMock.equalTo("Test-Transaction-ID-BabelClient"))
+                .withHeader("X-FromAppId", WireMock.equalTo("ModelLoader"))
+                .withRequestBody(WireMock.matchingJsonPath("$.artifactName", WireMock.equalTo("service-Vscpass-Test")))
+                .withRequestBody(WireMock.matchingJsonPath("$.artifactVersion", WireMock.equalTo("1.0")))
+                .withRequestBody(WireMock.matchingJsonPath("$.csar", WireMock.matching(".*")))
+                .willReturn(
+                    WireMock.aResponse()
+                        .withHeader("Content-Type", "application/json")
+                        .withBody(objectMapper.writeValueAsString(artifacts))));
     }
 
     @Test
     public void testRestClient() throws BabelServiceClientException, IOException, URISyntaxException {
-        Properties configProperties = new Properties();
-        configProperties.put("ml.babel.KEYSTORE_PASSWORD", "OBF:1vn21ugu1saj1v9i1v941sar1ugw1vo0");
-        configProperties.put("ml.babel.KEYSTORE_FILE", "src/test/resources/auth/aai-client-dummy.p12");
-        configProperties.put("ml.babel.TRUSTSTORE_PASSWORD", "OBF:1vn21ugu1saj1v9i1v941sar1ugw1vo0");
-        // In a real deployment this would be a different file (to the client keystore)
-        configProperties.put("ml.babel.TRUSTSTORE_FILE", "src/test/resources/auth/aai-client-dummy.p12");
-        configProperties.put("ml.babel.BASE_URL", "http://localhost:8080/");
-        configProperties.put("ml.babel.GENERATE_ARTIFACTS_URL", "generate");
-        BabelServiceClient client =
-                new HttpsBabelServiceClientFactory().create(new ModelLoaderConfig(configProperties, "."));
-        List<BabelArtifact> result =
-                client.postArtifact(readBytesFromFile("compressedArtifacts/service-VscpaasTest-csar.csar"),
-                        "service-Vscpass-Test", "1.0", "Test-Transaction-ID-BabelClient");
-        assertThat(result.size(), is(equalTo(3)));
-    }
+        BabelRequest babelRequest = new BabelRequest();
+        babelRequest.setArtifactName("service-Vscpass-Test");
+        babelRequest.setCsar(Base64.getEncoder().encodeToString(readBytesFromFile("compressedArtifacts/service-VscpaasTest-csar.csar")));
+        babelRequest.setArtifactVersion("1.0");
 
-    @Test
-    public void testRestClientHttp() throws BabelServiceClientException, IOException, URISyntaxException {
-        Properties configProperties = new Properties();
-        configProperties.put("ml.babel.USE_HTTPS", "false");
-        configProperties.put("ml.babel.BASE_URL", "http://localhost:8080/");
-        configProperties.put("ml.babel.GENERATE_ARTIFACTS_URL", "generate");
-        BabelServiceClient client =
-                new HttpsBabelServiceClientFactory().create(new ModelLoaderConfig(configProperties, "."));
         List<BabelArtifact> result =
-                client.postArtifact(readBytesFromFile("compressedArtifacts/service-VscpaasTest-csar.csar"),
-                        "service-Vscpass-Test", "1.0", "Test-Transaction-ID-BabelClient");
+                client.postArtifact(babelRequest, "Test-Transaction-ID-BabelClient");
         assertThat(result.size(), is(equalTo(3)));
     }
 
@@ -111,23 +98,4 @@ public class TestBabelServiceClient {
     private byte[] readBytesFromFile(String resourceFile) throws IOException, URISyntaxException {
         return Files.readAllBytes(Paths.get(ClassLoader.getSystemResource(resourceFile).toURI()));
     }
-
-    /**
-     * Creates an {@link AbstractHandler handler} returning an arbitrary String as a response.
-     * 
-     * @return never <code>null</code>.
-     */
-    private Handler getMockHandler() {
-        Handler handler = new AbstractHandler() {
-            @Override
-            public void handle(String target, Request request, HttpServletRequest servletRequest,
-                    HttpServletResponse response) throws IOException, ServletException {
-                response.setStatus(SC_OK);
-                response.setContentType("text/xml;charset=utf-8");
-                write(responseBody, response.getOutputStream());
-                request.setHandled(true);
-            }
-        };
-        return handler;
-    }
 }