Merge "INT:1183 fix csit for sdnc_netconf_tls_post_deploy"
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / test / java / org / onap / so / aaisimulator / utils / TestUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.aaisimulator.utils;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.util.Base64;
26 import org.springframework.core.io.ClassPathResource;
27 import org.springframework.http.HttpHeaders;
28 import org.springframework.http.MediaType;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
31
32 /**
33  * @author waqas.ikram@ericsson.com
34  *
35  */
36 public class TestUtils {
37
38     private static final String PASSWORD = "aai.onap.org:demo123456!";
39
40     public static HttpHeaders getHttpHeaders(final String username) {
41         final HttpHeaders requestHeaders = new HttpHeaders();
42         requestHeaders.add("Authorization", getBasicAuth(username));
43         requestHeaders.setContentType(MediaType.APPLICATION_JSON);
44         return requestHeaders;
45     }
46
47     public static File getFile(final String file) throws IOException {
48         return new ClassPathResource(file).getFile();
49     }
50
51     public static String getJsonString(final String file) throws IOException {
52         return new String(Files.readAllBytes(getFile(file).toPath()));
53     }
54
55     public static <T> T getObjectFromFile(final File file, final Class<T> clazz) throws Exception {
56         final ObjectMapper mapper = new ObjectMapper();
57         mapper.registerModule(new JaxbAnnotationModule());
58
59         return mapper.readValue(file, clazz);
60     }
61
62     public static String getBasicAuth(final String username) {
63         return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes()));
64     }
65
66     public static String getBaseUrl(final int port) {
67         return "https://localhost:" + port;
68     }
69
70     private TestUtils() {}
71
72 }