X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Forg%2Fonap%2Faai%2Fbabel%2Fservice%2FTestGenerateArtifactsServiceImpl.java;h=599b3ff32dce1dea7f1e26fcafad37841fbeda33;hb=49a1e7a2d020b9413e08ba34d2af83c1c6604d54;hp=b5063dda4cf31ad9e07411d63ea788a381792ca7;hpb=dc94e09008ba0eb9dce7541d2c898ddc5c500814;p=aai%2Fbabel.git diff --git a/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java b/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java index b5063dd..599b3ff 100644 --- a/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java +++ b/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java @@ -23,6 +23,7 @@ package org.onap.aai.babel.service; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; +import com.google.gson.Gson; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; @@ -42,13 +43,15 @@ import org.junit.runner.RunWith; import org.mockito.Mockito; import org.onap.aai.auth.AAIMicroServiceAuth; import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser; +import org.onap.aai.babel.service.data.BabelRequest; +import org.onap.aai.babel.testdata.CsarTest; import org.onap.aai.babel.util.ArtifactTestUtils; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - * Direct invocation of the generate artifacts service implementation + * Direct invocation of the generate artifacts service implementation. * */ @RunWith(SpringJUnit4ClassRunner.class) @@ -63,6 +66,7 @@ public class TestGenerateArtifactsServiceImpl { } private static final String ARTIFACT_GENERATOR_CONFIG = "artifact-generator.properties"; + @Inject private AAIMicroServiceAuth auth; @@ -72,50 +76,94 @@ public class TestGenerateArtifactsServiceImpl { new ArtifactTestUtils().getResourcePath(ARTIFACT_GENERATOR_CONFIG)); } + @Test + public void testGenerateArtifacts() throws Exception { + Response response = processJsonRequest(CsarTest.VNF_VENDOR_CSAR); + assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode())); + assertThat(response.getEntity(), is(getResponseJson("response.json"))); + } + + /** + * No VNF Configuration exists. + * + * @throws Exception + */ + @Test + public void testGenerateArtifactsWithoutVnfConfiguration() throws Exception { + Response response = processJsonRequest(CsarTest.NO_VNF_CONFIG_CSAR); + assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode())); + assertThat(response.getEntity(), is(getResponseJson("validNoVnfConfigurationResponse.json"))); + } + @Test public void testInvalidCsarFile() throws URISyntaxException, IOException { - Response response = processJsonRequest("invalid_csar_request.json"); + BabelRequest request = new BabelRequest(); + request.setArtifactName("hello"); + request.setArtifactVersion("1.0"); + request.setCsar("xxxx"); + Response response = invokeService(new Gson().toJson(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 = processJsonRequest("invalid_json_request.json"); + 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 { - Response response = processJsonRequest("missing_artifact_name_request.json"); + BabelRequest request = new BabelRequest(); + request.setArtifactVersion("1.0"); + request.setCsar(""); + Response response = invokeService(new Gson().toJson(request)); assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode())); assertThat(response.getEntity(), is("No artifact name attribute found in the request body.")); } @Test public void testMissingArtifactVersion() throws Exception { - Response response = processJsonRequest("missing_artifact_version_request.json"); + BabelRequest request = new BabelRequest(); + request.setArtifactName("hello"); + request.setCsar(""); + Response response = invokeService(new Gson().toJson(request)); assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode())); assertThat(response.getEntity(), is("No artifact version attribute found in the request body.")); } @Test public void testMissingCsarFile() throws Exception { - Response response = processJsonRequest("missing_csar_request.json"); + BabelRequest request = new BabelRequest(); + request.setArtifactName("test-name"); + request.setArtifactVersion("1.0"); + Response response = invokeService(new Gson().toJson(request)); assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode())); assertThat(response.getEntity(), is("No csar attribute found in the request body.")); } /** - * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API + * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API. + * + * @param csar + * @return the Response from the HTTP API + * @throws URISyntaxException if the URI cannot be created + * @throws IOException if the resource cannot be loaded + */ + private Response processJsonRequest(CsarTest csar) throws IOException, URISyntaxException { + String jsonString = csar.getJsonRequest(); + return invokeService(jsonString); + } + + /** + * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API. * - * @param resource path to the incoming JSON request + * @param jsonString the JSON request * @return the Response from the HTTP API - * @throws URISyntaxException - * @throws IOException + * @throws URISyntaxException if the URI cannot be created */ - private Response processJsonRequest(String resource) throws URISyntaxException, IOException { + private Response invokeService(String jsonString) throws URISyntaxException { UriInfo mockUriInfo = Mockito.mock(UriInfo.class); Mockito.when(mockUriInfo.getRequestUri()).thenReturn(new URI("/validate")); // NOSONAR (mocked) Mockito.when(mockUriInfo.getPath(false)).thenReturn("validate"); // URI prefix is stripped by AJSC routing @@ -148,12 +196,11 @@ public class TestGenerateArtifactsServiceImpl { servletRequest.setAttribute("javax.servlet.request.cipher_suite", ""); GenerateArtifactsServiceImpl service = new GenerateArtifactsServiceImpl(auth); - String jsonString = getRequestJson(resource); return service.generateArtifacts(mockUriInfo, headers, servletRequest, jsonString); } - private String getRequestJson(String resource) throws IOException, URISyntaxException { - return new ArtifactTestUtils().getRequestJson(resource); + private String getResponseJson(String jsonResponse) throws IOException, URISyntaxException { + return new ArtifactTestUtils().getResponseJson(jsonResponse); } private List createSingletonList(String listItem) {