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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.aaisimulator.utils;
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;
33 * @author waqas.ikram@ericsson.com
36 public class TestUtils {
38 private static final String PASSWORD = "aai.onap.org:demo123456!";
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;
47 public static File getFile(final String file) throws IOException {
48 return new ClassPathResource(file).getFile();
51 public static String getJsonString(final String file) throws IOException {
52 return new String(Files.readAllBytes(getFile(file).toPath()));
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());
59 return mapper.readValue(file, clazz);
62 public static String getBasicAuth(final String username) {
63 return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes()));
66 public static String getBaseUrl(final int port) {
67 return "https://localhost:" + port;
70 private TestUtils() {}