2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 European Software Marketing Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.babel.service;
25 import static org.hamcrest.Matchers.is;
26 import static org.junit.Assert.assertThat;
28 import java.io.IOException;
29 import java.net.URISyntaxException;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
33 import java.util.stream.Collectors;
34 import javax.ws.rs.core.Response;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
39 * Direct invocation of the generate artifacts service implementation
42 public class TestGenerateArtifactsServiceImpl {
45 public static void setup() {
46 URL url = TestGenerateArtifactsServiceImpl.class.getClassLoader().getResource("artifact-generator.properties");
47 System.setProperty("artifactgenerator.config", url.getPath());
51 public void testGenerateArtifacts() throws Exception {
52 String jsonRequest = readstringFromFile("jsonFiles/success_request.json");
53 Response response = processJsonRequest(jsonRequest);
54 assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
55 assertThat(response.getEntity(), is(readstringFromFile("response/response.json")));
60 public void testInvalidCsarFile() throws URISyntaxException, IOException{
61 String jsonRequest = readstringFromFile("jsonFiles/invalid_csar_request.json");
62 Response response = processJsonRequest(jsonRequest);
63 assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
64 assertThat(response.getEntity(), is("Error converting CSAR artifact to XML model."));
68 public void testInvalidJsonFile() throws URISyntaxException, IOException{
69 String jsonRequest = readstringFromFile("jsonFiles/invalid_json_request.json");
70 Response response = processJsonRequest(jsonRequest);
71 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
72 assertThat(response.getEntity(), is("Malformed request."));
76 public void testMissingArtifactName() throws Exception {
77 String jsonRequest = readstringFromFile("jsonFiles/missing_artifact_name_request.json");
78 Response response = processJsonRequest(jsonRequest);
79 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
80 assertThat(response.getEntity(), is("No artifact name attribute found in the request body." ));
84 public void testMissingArtifactVersion() throws Exception {
85 String jsonRequest = readstringFromFile("jsonFiles/missing_artifact_version_request.json");
86 Response response = processJsonRequest(jsonRequest);
87 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
88 assertThat(response.getEntity(), is("No artifact version attribute found in the request body."));
92 public void testMissingCsarFile() throws Exception {
93 String jsonRequest = readstringFromFile("jsonFiles/missing_csar_request.json");
94 Response response = processJsonRequest(jsonRequest);
95 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
96 assertThat(response.getEntity(), is("No csar attribute found in the request body."));
100 private Response processJsonRequest(String jsonRequest) {
101 GenerateArtifactsServiceImpl service = new GenerateArtifactsServiceImpl(/* No authentiction required */ null);
102 return service.generateArtifacts(jsonRequest);
105 private String readstringFromFile(String resourceFile) throws IOException, URISyntaxException {
106 return Files.lines(Paths.get(ClassLoader.getSystemResource(resourceFile).toURI()))
107 .collect(Collectors.joining());