1cdfeb911cec552234a6a15390d49c4f80871a1a
[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 org.springframework.web.util.UriComponentsBuilder;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
32
33 /**
34  * @author waqas.ikram@ericsson.com
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     public static String getCustomer() throws Exception, IOException {
72         return getJsonString("test-data/business-customer.json");
73     }
74
75     public static String getServiceSubscription() throws IOException {
76         return getJsonString("test-data/service-subscription.json");
77     }
78
79     public static String getServiceInstance() throws IOException {
80         return getJsonString("test-data/service-instance.json");
81     }
82
83     public static String getGenericVnf() throws IOException {
84         return getJsonString("test-data/generic-vnf.json");
85     }
86
87     public static String getRelationShip() throws IOException {
88         return getJsonString("test-data/relation-ship.json");
89     }
90
91     public static String getPlatformRelatedLink() throws IOException {
92         return getJsonString("test-data/platform-related-link.json");
93     }
94
95     public static String getLineOfBusinessRelatedLink() throws IOException {
96         return getJsonString("test-data/line-of-business-related-link.json");
97     }
98
99     public static String getPlatform() throws IOException {
100         return getJsonString("test-data/platform.json");
101     }
102
103     public static String getGenericVnfRelationShip() throws IOException {
104         return getJsonString("test-data/generic-vnf-relationship.json");
105     }
106
107     public static String getLineOfBusiness() throws IOException {
108         return getJsonString("test-data/line-of-business.json");
109     }
110
111     public static String getBusinessProject() throws IOException {
112         return getJsonString("test-data/business-project.json");
113     }
114
115     public static String getBusinessProjectRelationship() throws IOException {
116         return getJsonString("test-data/business-project-relation-ship.json");
117     }
118
119     public static String getOwningEntityRelationship() throws IOException {
120         return getJsonString("test-data/owning-entity-relation-ship.json");
121     }
122
123     public static String getOwningEntity() throws IOException {
124         return getJsonString("test-data/owning-entity.json");
125     }
126
127     public static String getOrchStatuUpdateServiceInstance() throws IOException {
128         return getJsonString("test-data/service-instance-orch-status-update.json");
129     }
130
131     public static String getRelationShipJsonObject() throws IOException {
132         return getJsonString("test-data/service-Instance-relationShip.json");
133     }
134
135     public static String getCloudRegion() throws IOException {
136         return getJsonString("test-data/cloud-region.json");
137     }
138
139     public static String getTenant() throws IOException {
140         return getJsonString("test-data/tenant.json");
141     }
142
143     public static String getCloudRegionRelatedLink() throws IOException {
144         return getJsonString("test-data/cloud-region-related-link.json");
145     }
146
147     public static String getGenericVnfRelatedLink() throws IOException {
148         return getJsonString("test-data/generic-vnf-related-link.json");
149     }
150
151     public static String getTenantRelationShip() throws IOException {
152         return getJsonString("test-data/tenant-relationship.json");
153     }
154
155     public static String getGenericVnfOrchStatuUpdate() throws IOException {
156         return getJsonString("test-data/generic-vnf-orch-status-update.json");
157     }
158
159     public static String getUrl(final int port, final String... urls) {
160         final UriComponentsBuilder baseUri = UriComponentsBuilder.fromUriString("https://localhost:" + port);
161         for (final String url : urls) {
162             baseUri.path(url);
163         }
164         return baseUri.toUriString();
165     }
166
167     private TestUtils() {}
168
169 }