Model distribution fails with model-loader 1.13.5
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / restclient / TestBabelServiceClient.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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 package org.onap.aai.modelloader.restclient;
22
23 import static javax.servlet.http.HttpServletResponse.SC_OK;
24 import static org.apache.commons.io.IOUtils.write;
25 import static org.hamcrest.CoreMatchers.equalTo;
26 import static org.hamcrest.CoreMatchers.is;
27 import static org.hamcrest.MatcherAssert.assertThat;
28
29 import java.io.IOException;
30 import java.net.URISyntaxException;
31 import java.nio.charset.Charset;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Properties;
37
38 import javax.servlet.ServletException;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41
42 import org.eclipse.jetty.server.Handler;
43 import org.eclipse.jetty.server.Request;
44 import org.eclipse.jetty.server.Server;
45 import org.eclipse.jetty.server.handler.AbstractHandler;
46 import org.junit.jupiter.api.AfterEach;
47 import org.junit.jupiter.api.BeforeEach;
48 import org.junit.jupiter.api.Test;
49 import org.onap.aai.babel.service.data.BabelArtifact;
50 import org.onap.aai.modelloader.config.ModelLoaderConfig;
51 import org.onap.aai.modelloader.service.HttpsBabelServiceClientFactory;
52
53 import com.google.gson.Gson;
54
55 /**
56  * Local testing of the Babel service client.
57  *
58  */
59 public class TestBabelServiceClient {
60
61     private Server server;
62     private String responseBody;
63
64     @BeforeEach
65     public void startJetty() throws Exception {
66         List<BabelArtifact> response = new ArrayList<>();
67         response.add(new BabelArtifact("", null, ""));
68         response.add(new BabelArtifact("", null, ""));
69         response.add(new BabelArtifact("", null, ""));
70         responseBody = new Gson().toJson(response);
71
72         server = new Server(0);
73         server.setHandler(getMockHandler());
74         server.start();
75     }
76
77     @AfterEach
78     public void stopJetty() throws Exception {
79         server.stop();
80     }
81
82     @Test
83     public void testRestClient() throws BabelServiceClientException, IOException, URISyntaxException {
84         String url = server.getURI().toString();
85         Properties configProperties = new Properties();
86         configProperties.put("ml.babel.KEYSTORE_PASSWORD", "OBF:1vn21ugu1saj1v9i1v941sar1ugw1vo0");
87         configProperties.put("ml.babel.KEYSTORE_FILE", "src/test/resources/auth/aai-client-dummy.p12");
88         configProperties.put("ml.babel.TRUSTSTORE_PASSWORD", "OBF:1vn21ugu1saj1v9i1v941sar1ugw1vo0");
89         // In a real deployment this would be a different file (to the client keystore)
90         configProperties.put("ml.babel.TRUSTSTORE_FILE", "src/test/resources/auth/aai-client-dummy.p12");
91         configProperties.put("ml.babel.BASE_URL", url);
92         configProperties.put("ml.babel.GENERATE_ARTIFACTS_URL", "generate");
93         configProperties.put("ml.aai.RESTCLIENT_CONNECT_TIMEOUT", "12000");
94         configProperties.put("ml.aai.RESTCLIENT_READ_TIMEOUT", "12000");
95         BabelServiceClient client =
96                 new HttpsBabelServiceClientFactory().create(new ModelLoaderConfig(configProperties, "."));
97         List<BabelArtifact> result =
98                 client.postArtifact(readBytesFromFile("compressedArtifacts/service-VscpaasTest-csar.csar"),
99                         "service-Vscpass-Test", "1.0", "Test-Transaction-ID-BabelClient");
100         assertThat(result.size(), is(equalTo(3)));
101     }
102
103     @Test
104     public void testRestClientHttp() throws BabelServiceClientException, IOException, URISyntaxException {
105         String url = server.getURI().toString();
106         Properties configProperties = new Properties();
107         configProperties.put("ml.babel.USE_HTTPS", "false");
108         configProperties.put("ml.babel.BASE_URL", url);
109         configProperties.put("ml.babel.GENERATE_ARTIFACTS_URL", "generate");
110         configProperties.put("ml.aai.RESTCLIENT_CONNECT_TIMEOUT", "3000");
111         configProperties.put("ml.aai.RESTCLIENT_READ_TIMEOUT", "3000");
112         BabelServiceClient client =
113                 new HttpsBabelServiceClientFactory().create(new ModelLoaderConfig(configProperties, "."));
114         List<BabelArtifact> result =
115                 client.postArtifact(readBytesFromFile("compressedArtifacts/service-VscpaasTest-csar.csar"),
116                         "service-Vscpass-Test", "1.0", "Test-Transaction-ID-BabelClient");
117         assertThat(result.size(), is(equalTo(3)));
118     }
119
120
121     private byte[] readBytesFromFile(String resourceFile) throws IOException, URISyntaxException {
122         return Files.readAllBytes(Paths.get(ClassLoader.getSystemResource(resourceFile).toURI()));
123     }
124
125     /**
126      * Creates an {@link AbstractHandler handler} returning an arbitrary String as a response.
127      * 
128      * @return never <code>null</code>.
129      */
130     private Handler getMockHandler() {
131         Handler handler = new AbstractHandler() {
132             @Override
133             public void handle(String target, Request request, HttpServletRequest servletRequest,
134                     HttpServletResponse response) throws IOException, ServletException {
135                 response.setStatus(SC_OK);
136                 response.setContentType("text/xml;charset=utf-8");
137                 write(responseBody, response.getOutputStream(), Charset.defaultCharset());
138                 request.setHandled(true);
139             }
140         };
141         return handler;
142     }
143 }