Add the queries endpoint and files
[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 package org.onap.aai.schemaservice;
21
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.apache.http.client.HttpClient;
25 import org.apache.http.impl.client.HttpClients;
26 import org.apache.http.ssl.SSLContextBuilder;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.boot.test.context.TestConfiguration;
29 import org.springframework.boot.web.client.RestTemplateBuilder;
30 import org.springframework.context.annotation.Bean;
31 import org.springframework.core.env.Environment;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.client.ClientHttpResponse;
34 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
35 import org.springframework.util.ResourceUtils;
36 import org.springframework.web.client.ResponseErrorHandler;
37 import org.springframework.web.client.RestTemplate;
38
39 import javax.net.ssl.SSLContext;
40 import java.io.File;
41 import java.io.FileInputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.security.KeyStore;
45
46 @TestConfiguration
47 public class SchemaServiceTestConfiguration {
48
49     private static final EELFLogger logger = EELFManager.getInstance().getLogger(SchemaServiceTestConfiguration.class);
50
51     @Autowired
52     private Environment env;
53
54     /**
55      * Create a RestTemplate bean, using the RestTemplateBuilder provided
56      * by the auto-configuration.
57      */
58     @Bean
59     RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception {
60
61         char[] trustStorePassword = env.getProperty("server.ssl.trust-store-password").toCharArray();
62         char[] keyStorePassword   = env.getProperty("server.ssl.key-store-password").toCharArray();
63
64         String keyStore = env.getProperty("server.ssl.key-store");
65         String trustStore = env.getProperty("server.ssl.trust-store");
66
67         SSLContextBuilder sslContextBuilder = SSLContextBuilder.create();
68
69         if(env.acceptsProfiles("two-way-ssl")){
70             sslContextBuilder = sslContextBuilder.loadKeyMaterial(loadPfx(keyStore, keyStorePassword), keyStorePassword);
71         }
72
73         SSLContext sslContext = sslContextBuilder
74                 .loadTrustMaterial(ResourceUtils.getFile(trustStore), trustStorePassword)
75                 .build();
76
77         HttpClient client = HttpClients.custom()
78                 .setSSLContext(sslContext)
79                 .setSSLHostnameVerifier((s, sslSession) -> true)
80                 .build();
81
82         RestTemplate restTemplate =  builder
83                 .requestFactory(new HttpComponentsClientHttpRequestFactory(client))
84                 .build();
85
86         restTemplate.setErrorHandler(new ResponseErrorHandler() {
87             @Override
88             public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
89                 if (clientHttpResponse.getStatusCode() != HttpStatus.OK) {
90
91                     logger.debug("Status code: " + clientHttpResponse.getStatusCode());
92
93                     if (clientHttpResponse.getStatusCode() == HttpStatus.FORBIDDEN) {
94                         logger.debug("Call returned a error 403 forbidden resposne ");
95                         return true;
96                     }
97
98                     if(clientHttpResponse.getRawStatusCode() % 100 == 5){
99                         logger.debug("Call returned a error " + clientHttpResponse.getStatusText());
100                         return true;
101                     }
102                 }
103
104                 return false;
105             }
106
107             @Override
108             public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {
109             }
110         });
111
112         return restTemplate;
113     }
114
115     private KeyStore loadPfx(String file, char[] password) throws Exception {
116         KeyStore keyStore = KeyStore.getInstance("PKCS12");
117         File key = ResourceUtils.getFile(file);
118         try (InputStream in = new FileInputStream(key)) {
119             keyStore.load(in, password);
120         }
121         return keyStore;
122     }
123 }