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.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 org.springframework.web.util.UriComponentsBuilder;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
34 * @author waqas.ikram@ericsson.com
37 public class TestUtils {
39 private static final String PASSWORD = "aai.onap.org:demo123456!";
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;
48 public static File getFile(final String file) throws IOException {
49 return new ClassPathResource(file).getFile();
52 public static String getJsonString(final String file) throws IOException {
53 return new String(Files.readAllBytes(getFile(file).toPath()));
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());
60 return mapper.readValue(file, clazz);
63 public static String getBasicAuth(final String username) {
64 return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes()));
67 public static String getBaseUrl(final int port) {
68 return "https://localhost:" + port;
71 public static String getCustomer() throws Exception, IOException {
72 return getJsonString("test-data/business-customer.json");
75 public static String getServiceSubscription() throws IOException {
76 return getJsonString("test-data/service-subscription.json");
79 public static String getServiceInstance() throws IOException {
80 return getJsonString("test-data/service-instance.json");
83 public static String getGenericVnf() throws IOException {
84 return getJsonString("test-data/generic-vnf.json");
87 public static String getPnf() throws IOException {
88 return getJsonString("test-data/pnf.json");
91 public static String getRelationShip() throws IOException {
92 return getJsonString("test-data/relation-ship.json");
95 public static String getPlatformRelatedLink() throws IOException {
96 return getJsonString("test-data/platform-related-link.json");
99 public static String getLineOfBusinessRelatedLink() throws IOException {
100 return getJsonString("test-data/line-of-business-related-link.json");
103 public static String getPlatform() throws IOException {
104 return getJsonString("test-data/platform.json");
107 public static String getGenericVnfRelationShip() throws IOException {
108 return getJsonString("test-data/generic-vnf-relationship.json");
111 public static String getLineOfBusiness() throws IOException {
112 return getJsonString("test-data/line-of-business.json");
115 public static String getBusinessProject() throws IOException {
116 return getJsonString("test-data/business-project.json");
119 public static String getBusinessProjectRelationship() throws IOException {
120 return getJsonString("test-data/business-project-relation-ship.json");
123 public static String getOwningEntityRelationship() throws IOException {
124 return getJsonString("test-data/owning-entity-relation-ship.json");
127 public static String getOwningEntity() throws IOException {
128 return getJsonString("test-data/owning-entity.json");
131 public static String getOrchStatuUpdateServiceInstance() throws IOException {
132 return getJsonString("test-data/service-instance-orch-status-update.json");
135 public static String getRelationShipJsonObject() throws IOException {
136 return getJsonString("test-data/service-Instance-relationShip.json");
139 public static String getCloudRegion() throws IOException {
140 return getJsonString("test-data/cloud-region.json");
143 public static String getTenant() throws IOException {
144 return getJsonString("test-data/tenant.json");
147 public static String getCloudRegionRelatedLink() throws IOException {
148 return getJsonString("test-data/cloud-region-related-link.json");
151 public static String getGenericVnfRelatedLink() throws IOException {
152 return getJsonString("test-data/generic-vnf-related-link.json");
155 public static String getTenantRelationShip() throws IOException {
156 return getJsonString("test-data/tenant-relationship.json");
159 public static String getGenericVnfOrchStatuUpdate() throws IOException {
160 return getJsonString("test-data/generic-vnf-orch-status-update.json");
163 public static String getEsrVnfm() throws IOException {
164 return getJsonString("test-data/esr-vnfm.json");
167 public static String getEsrSystemInfo() throws IOException {
168 return getJsonString("test-data/esr-system-info.json");
171 public static String getVserver() throws IOException {
172 return getJsonString("test-data/vServer.json");
176 public static String getUrl(final int port, final String... urls) {
177 final UriComponentsBuilder baseUri = UriComponentsBuilder.fromUriString("https://localhost:" + port);
178 for (final String url : urls) {
181 return baseUri.toUriString();
184 private TestUtils() {}