Reduce amount of explicit object mapping in babel 36/139236/1
authorFiete Ostkamp <Fiete.Ostkamp@telekom.de>
Mon, 21 Oct 2024 14:24:14 +0000 (16:24 +0200)
committerFiete Ostkamp <Fiete.Ostkamp@telekom.de>
Mon, 21 Oct 2024 14:24:14 +0000 (16:24 +0200)
- let the framework do the object mapping to BabelRequest

Issue-ID: AAI-4022
Change-Id: Ia657c5ce6974b25b85364b2deff3aab34f27d10a
Signed-off-by: Fiete Ostkamp <Fiete.Ostkamp@telekom.de>
src/main/java/org/onap/aai/babel/service/GenerateArtifactsController.java
src/main/java/org/onap/aai/babel/service/GenerateArtifactsControllerImpl.java
src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java
src/test/java/org/onap/aai/babel/testdata/CsarTest.java

index 67fc865..29ebbb2 100644 (file)
@@ -31,6 +31,7 @@ import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 import org.onap.aai.auth.AAIAuthException;
+import org.onap.aai.babel.service.data.BabelRequest;
 
 /** Generate artifacts from the specified request content */
 @Path("/app")
@@ -41,6 +42,5 @@ public interface GenerateArtifactsController {
 
     @POST
     @Path("/generateArtifacts")
-    Response generateArtifacts(@Context UriInfo uriInfo, @Context HttpHeaders headers,
-            @Context HttpServletRequest servletRequest, String request) throws AAIAuthException;
+    Response generateArtifacts(BabelRequest babelRequest) throws AAIAuthException;
 }
index 189aaf3..4b56798 100644 (file)
@@ -22,7 +22,6 @@
 package org.onap.aai.babel.service;
 
 import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
 import com.google.gson.JsonSyntaxException;
 
 import lombok.RequiredArgsConstructor;
@@ -34,9 +33,6 @@ import javax.ws.rs.core.*;
 import javax.ws.rs.core.Response.Status;
 
 import org.apache.commons.lang3.time.StopWatch;
-import org.onap.aai.auth.AAIAuthException;
-import org.onap.aai.auth.AAIMicroServiceAuth;
-import org.onap.aai.auth.AAIMicroServiceAuthCore;
 import org.onap.aai.babel.csar.CsarConverterException;
 import org.onap.aai.babel.csar.CsarToXmlConverter;
 import org.onap.aai.babel.csar.vnfcatalog.ToscaToCatalogException;
@@ -63,26 +59,10 @@ public class GenerateArtifactsControllerImpl implements GenerateArtifactsControl
     private final Gson gson;
 
     @Override
-    public Response generateArtifacts(UriInfo uriInfo, HttpHeaders headers, HttpServletRequest servletRequest,
-            String requestBody) {
+    public Response generateArtifacts(BabelRequest babelRequest) {
         Response response;
-        // try {
-            // Get last URI path segment to use for authentication
-            // List<PathSegment> pathSegments = uriInfo.getPathSegments();
-            // String lastPathSegment = pathSegments.isEmpty() ? "" : pathSegments.get(pathSegments.size() - 1).getPath();
-
-            // boolean authorized = aaiMicroServiceAuth.validateRequest(headers, servletRequest,
-            //         AAIMicroServiceAuthCore.HTTP_METHODS.POST, lastPathSegment);
-
-        response = generateArtifacts(requestBody);
-            // response = authorized ? generateArtifacts(requestBody)
-                    // : buildResponse(Status.UNAUTHORIZED, "User not authorized to perform the operation.");
-        // } catch (AAIAuthException e) {
-        //     applicationLogger.error(ApplicationMsgs.PROCESS_REQUEST_ERROR, e);
-        //     applicationLogger.logAuditError(e);
-        //     return buildResponse(Status.INTERNAL_SERVER_ERROR,
-        //             "Error while processing request. Please check the Babel service logs for more details.\n");
-        // }
+
+        response = generateArtifactsImpl(babelRequest);
 
         StatusCode statusDescription;
         int statusCode = response.getStatus();
@@ -104,14 +84,13 @@ public class GenerateArtifactsControllerImpl implements GenerateArtifactsControl
      *            the request body in JSON format
      * @return response object containing the generated XML models
      */
-    protected Response generateArtifacts(String requestBody) {
+    protected Response generateArtifactsImpl(BabelRequest babelRequest) {
         StopWatch stopwatch = new StopWatch();
         stopwatch.start();
 
         Response response;
 
         try {
-            BabelRequest babelRequest = gson.fromJson(requestBody, BabelRequest.class);
             new RequestValidator().validateRequest(babelRequest);
             byte[] csarFile = Base64.getDecoder().decode(babelRequest.getCsar());
 
index 3070566..27eeb51 100644 (file)
@@ -96,7 +96,7 @@ public class TestGenerateArtifactsServiceImpl {
      */
     @Test
     public void testGenerateArtifactsWithoutRequestId() throws URISyntaxException, IOException {
-        Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getJsonRequest(), Optional.empty());
+        Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getBabelRequest(), Optional.empty());
         assertThat(response.toString(), response.getStatus(), is(Response.Status.OK.getStatusCode()));
         assertThat(response.getEntity(), is(getResponseJson("response.json")));
     }
@@ -111,7 +111,7 @@ public class TestGenerateArtifactsServiceImpl {
      */
     @Test
     public void testGenerateArtifactsWithoutMinorArtifactVersion() throws URISyntaxException, IOException {
-        Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getJsonRequestWithArtifactVersion("1"),
+        Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getBabelRequestWithArtifactVersion("1"),
                        Optional.of("transaction-id"));
         assertThat(response.toString(), response.getStatus(), is(Response.Status.OK.getStatusCode()));
         assertThat(response.getEntity(), is(getResponseJson("response.json")));
@@ -127,7 +127,7 @@ public class TestGenerateArtifactsServiceImpl {
      */
     @Test
     public void testGenerateArtifactsWithInvalidArtifactVersion() throws URISyntaxException, IOException {
-        Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getJsonRequestWithArtifactVersion("a"),
+        Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getBabelRequestWithArtifactVersion("a"),
                        Optional.of("transaction-id"));
         assertThat(response.toString(), response.getStatus(), is(Response.Status.OK.getStatusCode()));
         assertThat(response.getEntity(), is(getResponseJson("response.json")));
@@ -144,7 +144,7 @@ public class TestGenerateArtifactsServiceImpl {
      */
     @Test
     public void testGenerateArtifactsWithArtifactVerLessThan1() throws URISyntaxException, IOException {
-        Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getJsonRequestWithArtifactVersion("0.1"),
+        Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getBabelRequestWithArtifactVersion("0.1"),
                        Optional.of("transaction-id"));
         assertThat(response.toString(), response.getStatus(), is(Response.Status.OK.getStatusCode()));
         assertThat(response.getEntity(), is(getResponseJson("responseWithVersionLessThan1.json")));
@@ -187,24 +187,17 @@ public class TestGenerateArtifactsServiceImpl {
         request.setArtifactName("hello");
         request.setArtifactVersion("1.0");
         request.setCsar("xxxx");
-        Response response = invokeService(new Gson().toJson(request));
+        Response response = invokeService(request);
         assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
         assertThat(response.getEntity(), is("Error converting CSAR artifact to XML model."));
     }
 
-    @Test
-    public void testInvalidJsonFile() throws URISyntaxException, IOException {
-        Response response = invokeService("{\"csar:\"xxxx\"");
-        assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
-        assertThat(response.getEntity(), is("Malformed request."));
-    }
-
     @Test
     public void testMissingArtifactName() throws Exception {
         BabelRequest request = new BabelRequest();
         request.setArtifactVersion("1.0");
         request.setCsar("");
-        Response response = invokeService(new Gson().toJson(request));
+        Response response = invokeService(request);
         assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
         assertThat(response.getEntity(), is("No artifact name attribute found in the request body."));
     }
@@ -214,7 +207,7 @@ public class TestGenerateArtifactsServiceImpl {
         BabelRequest request = new BabelRequest();
         request.setArtifactName("hello");
         request.setCsar("");
-        Response response = invokeService(new Gson().toJson(request));
+        Response response = invokeService(request);
         assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
         assertThat(response.getEntity(), is("No artifact version attribute found in the request body."));
     }
@@ -224,7 +217,7 @@ public class TestGenerateArtifactsServiceImpl {
         BabelRequest request = new BabelRequest();
         request.setArtifactName("test-name");
         request.setArtifactVersion("1.0");
-        Response response = invokeService(new Gson().toJson(request));
+        Response response = invokeService(request);
         assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
         assertThat(response.getEntity(), is("No csar attribute found in the request body."));
     }
@@ -244,7 +237,7 @@ public class TestGenerateArtifactsServiceImpl {
      */
     private Response processJsonRequest(CsarTest csar)
             throws URISyntaxException, IOException {
-        return invokeService(csar.getJsonRequest(), Optional.of("transaction-id"));
+        return invokeService(csar.getBabelRequest(), Optional.of("transaction-id"));
     }
 
     /**
@@ -256,8 +249,8 @@ public class TestGenerateArtifactsServiceImpl {
      * @throws URISyntaxException
      *             if the URI cannot be created
      */
-    private Response invokeService(String jsonRequest) throws URISyntaxException {
-        return invokeService(jsonRequest, Optional.of("transaction-id"));
+    private Response invokeService(BabelRequest babelRequest) throws URISyntaxException {
+        return invokeService(babelRequest, Optional.of("transaction-id"));
     }
 
     /**
@@ -273,7 +266,7 @@ public class TestGenerateArtifactsServiceImpl {
      * @throws URISyntaxException
      *             if the URI cannot be created
      */
-    private Response invokeService(String jsonString, Optional<String> transactionId)
+    private Response invokeService(BabelRequest babelRequest, Optional<String> transactionId)
             throws URISyntaxException {
         UriInfo mockUriInfo = Mockito.mock(UriInfo.class);
         Mockito.when(mockUriInfo.getRequestUri()).thenReturn(new URI("/validate")); // NOSONAR (mocked)
@@ -310,7 +303,7 @@ public class TestGenerateArtifactsServiceImpl {
         servletRequest.setAttribute("javax.servlet.request.cipher_suite", "");
 
         GenerateArtifactsControllerImpl service = new GenerateArtifactsControllerImpl(gson);
-        return service.generateArtifacts(mockUriInfo, headers, servletRequest, jsonString);
+        return service.generateArtifacts(babelRequest);
     }
 
     private String getResponseJson(String jsonResponse) throws IOException, URISyntaxException {
index 461e75c..c20e554 100644 (file)
@@ -96,31 +96,39 @@ public enum CsarTest {
 
     /**
      * Create a BabelRequest containing the encoded CSAR content.
-     * 
+     *
      * @return a new Babel request for this CSAR
      * @throws IOException
      *             if an I/O exception occurs
      */
     public String getJsonRequest() throws IOException {
+        return new Gson().toJson(getBabelRequest());
+    }
+
+    public BabelRequest getBabelRequest() throws IOException {
         BabelRequest request = new BabelRequest();
         request.setArtifactName(getName());
         request.setArtifactVersion("1.0");
         request.setCsar(new String(GeneratorUtil.encode(getContent())));
-        return new Gson().toJson(request);
+        return request;
     }
-    
+
     /**
      * Create a BabelRequest containing the encoded CSAR content by passing in the artifact version.
-     * 
+     *
      * @return a new Babel request for this CSAR
      * @throws IOException
      *             if an I/O exception occurs
      */
     public String getJsonRequestWithArtifactVersion(String artifactVersion) throws IOException {
+        BabelRequest babelRequest = getBabelRequestWithArtifactVersion(artifactVersion);
+        return new Gson().toJson(babelRequest);
+    }
+    public BabelRequest getBabelRequestWithArtifactVersion(String artifactVersion) throws IOException {
         BabelRequest request = new BabelRequest();
         request.setArtifactName(getName());
         request.setArtifactVersion(artifactVersion);
         request.setCsar(new String(GeneratorUtil.encode(getContent())));
-        return new Gson().toJson(request);
+        return request;
     }
 }