Add instructions to invoke the linter and code formatter plugins to the README and...
[aai/schema-service.git] / aai-schema-service / src / test / java / org / onap / aai / schemaservice / SchemaServiceTestConfiguration.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.schemaservice;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.security.KeyStore;
28
29 import javax.net.ssl.SSLContext;
30
31 import org.apache.http.client.HttpClient;
32 import org.apache.http.impl.client.HttpClients;
33 import org.apache.http.ssl.SSLContextBuilder;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.boot.test.context.TestConfiguration;
38 import org.springframework.boot.web.client.RestTemplateBuilder;
39 import org.springframework.context.annotation.Bean;
40 import org.springframework.core.env.Environment;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.client.ClientHttpResponse;
43 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
44 import org.springframework.util.ResourceUtils;
45 import org.springframework.web.client.ResponseErrorHandler;
46 import org.springframework.web.client.RestTemplate;
47
48 @TestConfiguration
49 public class SchemaServiceTestConfiguration {
50
51     private static final Logger logger =
52         LoggerFactory.getLogger(SchemaServiceTestConfiguration.class);
53
54     @Autowired
55     private Environment env;
56
57     /**
58      * Create a RestTemplate bean, using the RestTemplateBuilder provided
59      * by the auto-configuration.
60      */
61     @Bean
62     RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception {
63
64         RestTemplate restTemplate = null;
65
66         if (env.acceptsProfiles("one-way-ssl", "two-way-ssl")) {
67             char[] trustStorePassword =
68                 env.getProperty("server.ssl.trust-store-password").toCharArray();
69             char[] keyStorePassword =
70                 env.getProperty("server.ssl.key-store-password").toCharArray();
71
72             String keyStore = env.getProperty("server.ssl.key-store");
73             String trustStore = env.getProperty("server.ssl.trust-store");
74             SSLContextBuilder sslContextBuilder = SSLContextBuilder.create();
75
76             if (env.acceptsProfiles("two-way-ssl")) {
77                 sslContextBuilder = sslContextBuilder
78                     .loadKeyMaterial(loadPfx(keyStore, keyStorePassword), keyStorePassword);
79             }
80
81             SSLContext sslContext = sslContextBuilder
82                 .loadTrustMaterial(ResourceUtils.getFile(trustStore), trustStorePassword).build();
83
84             HttpClient client = HttpClients.custom().setSSLContext(sslContext)
85                 .setSSLHostnameVerifier((s, sslSession) -> true).build();
86
87             restTemplate = builder
88                 .requestFactory(() -> new HttpComponentsClientHttpRequestFactory(client)).build();
89         } else {
90             restTemplate = builder.build();
91         }
92
93         restTemplate.setErrorHandler(new ResponseErrorHandler() {
94             @Override
95             public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
96                 if (clientHttpResponse.getStatusCode() != HttpStatus.OK) {
97
98                     logger.debug("Status code: " + clientHttpResponse.getStatusCode());
99
100                     if (clientHttpResponse.getStatusCode() == HttpStatus.FORBIDDEN) {
101                         logger.debug("Call returned a error 403 forbidden resposne ");
102                         return true;
103                     }
104
105                     if (clientHttpResponse.getRawStatusCode() % 100 == 5) {
106                         logger.debug("Call returned a error " + clientHttpResponse.getStatusText());
107                         return true;
108                     }
109                 }
110
111                 return false;
112             }
113
114             @Override
115             public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {
116             }
117         });
118
119         return restTemplate;
120     }
121
122     private KeyStore loadPfx(String file, char[] password) throws Exception {
123         KeyStore keyStore = KeyStore.getInstance("PKCS12");
124         File key = ResourceUtils.getFile(file);
125         try (InputStream in = new FileInputStream(key)) {
126             keyStore.load(in, password);
127         }
128         return keyStore;
129     }
130 }