5d2309f8a8f27ab11370e6225aca1764dee02274
[aai/babel.git] / src / test / java / org / onap / aai / babel / service / TestGenerateArtifactsServiceImpl.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.babel.service;
24
25 import static org.hamcrest.Matchers.is;
26 import static org.junit.Assert.assertThat;
27
28 import java.io.IOException;
29 import java.net.URISyntaxException;
30 import java.net.URL;
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;
37
38 /**
39  * Direct invocation of the generate artifacts service implementation
40  *
41  */
42 public class TestGenerateArtifactsServiceImpl {
43     
44     @BeforeClass
45     public static void setup() {
46         URL url = TestGenerateArtifactsServiceImpl.class.getClassLoader().getResource("artifact-generator.properties");
47         System.setProperty("artifactgenerator.config", url.getPath());
48     }
49     
50     @Test
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")));
56     }
57
58     
59     @Test
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."));
65     }
66     
67     @Test
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."));
73     }
74     
75     @Test
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." ));
81     }
82     
83     @Test
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."));
89     }
90     
91     @Test
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."));
97     }
98     
99
100     private Response processJsonRequest(String jsonRequest) {
101         GenerateArtifactsServiceImpl service = new GenerateArtifactsServiceImpl(/* No authentiction required */ null);
102         return service.generateArtifacts(jsonRequest);
103     }
104
105     private String readstringFromFile(String resourceFile) throws IOException, URISyntaxException {
106         return Files.lines(Paths.get(ClassLoader.getSystemResource(resourceFile).toURI()))
107                 .collect(Collectors.joining());
108     }
109     
110     
111 }