package org.onap.aai.babel.service;
import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import lombok.RequiredArgsConstructor;
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;
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();
* 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());
*/
@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")));
}
*/
@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")));
*/
@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")));
*/
@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")));
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."));
}
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."));
}
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."));
}
*/
private Response processJsonRequest(CsarTest csar)
throws URISyntaxException, IOException {
- return invokeService(csar.getJsonRequest(), Optional.of("transaction-id"));
+ return invokeService(csar.getBabelRequest(), Optional.of("transaction-id"));
}
/**
* @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"));
}
/**
* @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)
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 {
/**
* 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;
}
}