c03f5ceb3819a8cd0b03990c57c7d2a02066b6bb
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / test / java / org / onap / so / aai / simulator / controller / 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.aai.simulator.controller;
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
37 public class TestUtils {
38
39     private static final String PASSWORD = "aai.onap.org:demo123456!";
40
41     public static HttpHeaders getHttpHeaders(final String username) {
42         final HttpHeaders requestHeaders = new HttpHeaders();
43         requestHeaders.add("Authorization", getBasicAuth(username));
44         requestHeaders.setContentType(MediaType.APPLICATION_JSON);
45         return requestHeaders;
46     }
47
48     public static File getFile(final String file) throws IOException {
49         return new ClassPathResource(file).getFile();
50     }
51
52     public static String getJsonString(final String file) throws IOException {
53         return new String(Files.readAllBytes(getFile(file).toPath()));
54     }
55
56     public static <T> T getObjectFromFile(final File file, final Class<T> clazz) throws Exception {
57         final ObjectMapper mapper = new ObjectMapper();
58         mapper.registerModule(new JaxbAnnotationModule());
59
60         return mapper.readValue(file, clazz);
61     }
62
63     public static String getBasicAuth(final String username) {
64         return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes()));
65     }
66
67     public static String getBaseUrl(final int port) {
68         return "https://localhost:" + port;
69     }
70
71     private TestUtils() {}
72
73 }