2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.adapters.vnf;
23 import com.fasterxml.jackson.core.JsonParseException;
24 import com.fasterxml.jackson.databind.JsonMappingException;
25 import com.fasterxml.jackson.databind.JsonNode;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import org.apache.http.HttpStatus;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.onap.so.adapters.openstack.MsoOpenstackAdaptersApplication;
32 import org.onap.so.db.catalog.beans.AuthenticationType;
33 import org.onap.so.db.catalog.beans.CloudIdentity;
34 import org.onap.so.db.catalog.beans.CloudSite;
35 import org.onap.so.db.catalog.beans.ServerType;
36 import org.onap.so.db.catalog.data.repository.CloudIdentityRepository;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.beans.factory.annotation.Qualifier;
39 import org.springframework.beans.factory.annotation.Value;
40 import org.springframework.boot.context.embedded.LocalServerPort;
41 import org.springframework.boot.test.context.SpringBootTest;
42 import org.springframework.boot.test.web.client.TestRestTemplate;
43 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
44 import org.springframework.http.HttpHeaders;
45 import org.springframework.test.context.ActiveProfiles;
46 import org.springframework.test.context.junit4.SpringRunner;
48 import javax.ws.rs.core.MediaType;
49 import java.io.BufferedReader;
51 import java.io.FileReader;
52 import java.io.IOException;
54 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
55 import static com.github.tomakehurst.wiremock.client.WireMock.get;
56 import static com.github.tomakehurst.wiremock.client.WireMock.reset;
57 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
58 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
60 @RunWith(SpringRunner.class)
61 @SpringBootTest(classes = MsoOpenstackAdaptersApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
62 @ActiveProfiles("test")
63 @AutoConfigureWireMock(port = 0)
64 public class BaseRestTestUtils {
65 @Value("${wiremock.server.port}")
66 protected int wireMockPort;
69 @Qualifier("JettisonStyle")
70 protected TestRestTemplate restTemplate;
72 protected HttpHeaders headers = new HttpHeaders();
77 public ObjectMapper mapper;
80 private CloudIdentityRepository cloudIdentityRepository;
82 protected String readJsonFileAsString(String fileLocation) throws JsonParseException, JsonMappingException, IOException{
83 ObjectMapper mapper = new ObjectMapper();
84 JsonNode jsonNode = mapper.readTree(new File(fileLocation));
85 return jsonNode.asText();
88 protected String createURLWithPort(String uri) {
89 return "http://localhost:" + port + uri;
92 protected String readFile(String fileName) throws IOException {
93 try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
94 StringBuilder sb = new StringBuilder();
95 String line = br.readLine();
97 while (line != null) {
100 line = br.readLine();
102 return sb.toString();
107 * Before each test execution, updating IdentityUrl port value to the ramdom wireMockPort
108 * Since URL will be used as a rest call and required to be mocked in unit tests
111 public void setUp() throws Exception {
113 mapper = new ObjectMapper();
115 CloudIdentity identity = new CloudIdentity();
116 identity.setId("MTN13");
117 identity.setMsoId("m93945");
118 identity.setMsoPass("93937EA01B94A10A49279D4572B48369");
119 identity.setAdminTenant("admin");
120 identity.setMemberRole("admin");
121 identity.setTenantMetadata(new Boolean(true));
122 identity.setIdentityUrl("http://localhost:"+wireMockPort+"/v2.0");
123 identity.setIdentityAuthenticationType(AuthenticationType.USERNAME_PASSWORD);
125 CloudSite cloudSite = new CloudSite();
126 cloudSite.setId("MTN13");
127 cloudSite.setCloudVersion("3.0");
128 cloudSite.setClli("MDT13");
129 cloudSite.setRegionId("mtn13");
130 cloudSite.setOrchestrator("orchestrator" +
132 identity.setIdentityServerType(ServerType.KEYSTONE);
133 cloudSite.setIdentityService(identity);
136 stubFor(get(urlPathEqualTo("/cloudSite/MTN13")).willReturn(aResponse()
137 .withBody(getBody(mapper.writeValueAsString(cloudSite),wireMockPort, ""))
138 .withHeader(org.apache.http.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
139 .withStatus(HttpStatus.SC_OK)));
140 stubFor(get(urlPathEqualTo("/cloudSite/default")).willReturn(aResponse()
141 .withBody(getBody(mapper.writeValueAsString(cloudSite),wireMockPort, ""))
142 .withHeader(org.apache.http.HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON)
143 .withStatus(HttpStatus.SC_OK)));
144 stubFor(get(urlPathEqualTo("/cloudIdentity/MTN13")).willReturn(aResponse()
145 .withBody(getBody(mapper.writeValueAsString(identity),wireMockPort, ""))
146 .withHeader(org.apache.http.HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON)
147 .withStatus(HttpStatus.SC_OK)));
150 protected static String getBody(String body, int port, String urlPath) throws IOException {
151 return body.replaceAll("port", "http://localhost:" + port + urlPath);
155 public void testNothing(){