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