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.asdc.client.test.rest;
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.post;
25 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
26 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
27 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
28 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertNull;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import java.util.HashSet;
36 import javax.transaction.Transactional;
37 import javax.ws.rs.core.Response;
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.TemporaryFolder;
42 import org.mockito.Spy;
43 import org.onap.so.asdc.BaseTest;
44 import org.onap.so.asdc.client.test.emulators.DistributionClientEmulator;
45 import org.onap.so.asdc.client.test.emulators.NotificationDataImpl;
46 import org.onap.so.db.catalog.beans.AllottedResource;
47 import org.onap.so.db.catalog.beans.AllottedResourceCustomization;
48 import org.onap.so.db.catalog.beans.Service;
49 import org.onap.so.db.catalog.beans.Workflow;
50 import org.onap.so.db.catalog.data.repository.AllottedResourceRepository;
51 import org.onap.so.db.catalog.data.repository.NetworkResourceRepository;
52 import org.onap.so.db.catalog.data.repository.ServiceRepository;
53 import org.onap.so.db.catalog.data.repository.WorkflowRepository;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.boot.test.web.client.TestRestTemplate;
56 import org.springframework.boot.web.server.LocalServerPort;
57 import org.springframework.http.HttpEntity;
58 import org.springframework.http.HttpHeaders;
59 import org.springframework.http.HttpMethod;
60 import org.springframework.http.ResponseEntity;
61 import com.fasterxml.jackson.databind.ObjectMapper;
63 public class ASDCRestInterfaceTest extends BaseTest {
66 private AllottedResourceRepository allottedRepo;
69 private ServiceRepository serviceRepo;
72 private NetworkResourceRepository networkRepo;
75 private WorkflowRepository workflowRepo;
78 private ASDCRestInterface asdcRestInterface;
80 private TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
82 private HttpHeaders headers = new HttpHeaders();
85 DistributionClientEmulator spyClient = new DistributionClientEmulator();
92 public TemporaryFolder folder = new TemporaryFolder();
97 // ASDC Controller writes to this path
98 System.setProperty("mso.config.path", folder.getRoot().toString());
103 public void testAllottedResourceService() throws Exception {
105 wireMockServer.stubFor(post(urlPathMatching("/aai/.*"))
106 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")));
108 wireMockServer.stubFor(post(urlPathMatching("/v1.0/activity-spec"))
109 .willReturn(aResponse().withHeader("Content-Type", "application/json")
110 .withStatus(org.springframework.http.HttpStatus.ACCEPTED.value())));
112 ObjectMapper mapper = new ObjectMapper();
113 NotificationDataImpl request =
114 mapper.readValue(new File("src/test/resources/resource-examples/allottedresource/notif-portm.json"),
115 NotificationDataImpl.class);
116 headers.add("resource-location", "src/test/resources/resource-examples/allottedresource/");
117 HttpEntity<NotificationDataImpl> entity = new HttpEntity<NotificationDataImpl>(request, headers);
119 ResponseEntity<String> response = restTemplate.exchange(createURLWithPort("test/treatNotification/v1"),
120 HttpMethod.POST, entity, String.class);
122 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
124 AllottedResource expectedService = new AllottedResource();
125 expectedService.setDescription("rege1802pnf");
126 expectedService.setModelInvariantUUID("b8f83c3f-077c-4e2c-b489-c66382060436");
127 expectedService.setModelName("rege1802pnf");
128 expectedService.setModelUUID("5b18c75e-2d08-4bf2-ad58-4ea704ec648d");
129 expectedService.setModelVersion("1.0");
130 expectedService.setSubcategory("Contrail Route");
131 expectedService.setToscaNodeType("org.openecomp.resource.pnf.Rege1802pnf");
132 Set<AllottedResourceCustomization> arCustomizationSet = new HashSet<AllottedResourceCustomization>();
133 AllottedResourceCustomization arCustomization = new AllottedResourceCustomization();
134 arCustomization.setModelCustomizationUUID("f62bb612-c5d4-4406-865c-0abec30631ba");
135 arCustomization.setModelInstanceName("rege1802pnf 0");
136 arCustomizationSet.add(arCustomization);
138 arCustomization.setAllottedResource(expectedService);
141 expectedService.setAllotedResourceCustomization(arCustomizationSet);
143 AllottedResource actualResponse = allottedRepo.findResourceByModelUUID("5b18c75e-2d08-4bf2-ad58-4ea704ec648d");
146 if (actualResponse == null)
147 throw new Exception("No Allotted Resource Written to database");
150 assertThat(actualResponse, sameBeanAs(expectedService).ignoring("0x1.created")
151 .ignoring("0x1.allotedResourceCustomization.created"));
156 public void test_VFW_Distrobution() throws Exception {
158 wireMockServer.stubFor(post(urlPathMatching("/aai/.*"))
159 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")));
161 wireMockServer.stubFor(post(urlPathMatching("/v1.0/activity-spec"))
162 .willReturn(aResponse().withHeader("Content-Type", "application/json")
163 .withStatus(org.springframework.http.HttpStatus.ACCEPTED.value())));
165 ObjectMapper mapper = new ObjectMapper();
166 NotificationDataImpl request = mapper.readValue(
167 new File("src/test/resources/resource-examples/vFW/notification.json"), NotificationDataImpl.class);
168 headers.add("resource-location", "src/test/resources/resource-examples/vFW/");
169 HttpEntity<NotificationDataImpl> entity = new HttpEntity<NotificationDataImpl>(request, headers);
171 ResponseEntity<String> response = restTemplate.exchange(createURLWithPort("test/treatNotification/v1"),
172 HttpMethod.POST, entity, String.class);
174 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
176 Service expectedService = new Service();
177 expectedService.setDescription("catalog service description");
178 expectedService.setModelInvariantUUID("3164f9ff-d7e7-4813-ab32-6be7e1cacb18");
179 expectedService.setModelName("vFW 2019-04-10 21:53:05");
180 expectedService.setModelUUID("e16e4ed9-3429-423a-bc3c-1389ae91491c");
181 expectedService.setModelVersion("1.0");
185 Service actualService = serviceRepo.findOneByModelUUID("e16e4ed9-3429-423a-bc3c-1389ae91491c");
188 if (actualService == null)
189 throw new Exception("No Allotted Resource Written to database");
191 assertEquals(expectedService.getModelName(), actualService.getModelName());
196 public void testWorkflowDistribution() throws Exception {
198 wireMockServer.stubFor(post(urlPathMatching("/aai/.*"))
199 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")));
201 wireMockServer.stubFor(post(urlPathMatching("/v1.0/activity-spec"))
202 .willReturn(aResponse().withHeader("Content-Type", "application/json")
203 .withStatus(org.springframework.http.HttpStatus.ACCEPTED.value())));
205 wireMockServer.stubFor(
206 post(urlPathEqualTo("/sobpmnengine/deployment/create")).willReturn(aResponse().withStatus(200)));
208 ObjectMapper mapper = new ObjectMapper();
209 NotificationDataImpl request = mapper.readValue(
210 new File("src/test/resources/resource-examples/WorkflowBpmn/workflow-distribution.json"),
211 NotificationDataImpl.class);
212 headers.add("resource-location", "src/test/resources/resource-examples/WorkflowBpmn/");
213 HttpEntity<NotificationDataImpl> entity = new HttpEntity<NotificationDataImpl>(request, headers);
215 ResponseEntity<String> response = restTemplate.exchange(createURLWithPort("test/treatNotification/v1"),
216 HttpMethod.POST, entity, String.class);
218 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
220 Workflow actualResponse = workflowRepo.findByArtifactUUID("a90f8eaa-7c20-422f-8c81-aacbca6fb9e7");
222 if (actualResponse == null)
223 throw new Exception("No Workflow Written to database");
225 String expectedBody = new String(
226 Files.readAllBytes(Paths.get("src/test/resources/resource-examples/WorkflowBpmn/TestWF2-1_0.bpmn")));
227 assertEquals(actualResponse.getArtifactChecksum(), "ZjUzNjg1NDMyMTc4MWJmZjFlNDcyOGQ0Zjc1YWQwYzQ\u003d");
228 assertEquals(actualResponse.getArtifactName(), "TestWF2-1_0.bpmn");
229 assertEquals(actualResponse.getDescription(), "Workflow Artifact Description");
230 assertEquals(actualResponse.getBody(), expectedBody);
232 Workflow shouldNotBeFound = workflowRepo.findByArtifactUUID("f27066a1-c3a7-4672-b02e-1251b74b7b71");
233 assertNull(shouldNotBeFound);
237 public void invokeASDCStatusDataNullTest() {
239 wireMockServer.stubFor(post(urlPathMatching("/v1.0/activity-spec"))
240 .willReturn(aResponse().withHeader("Content-Type", "application/json")
241 .withStatus(org.springframework.http.HttpStatus.ACCEPTED.value())));
242 Response response = asdcRestInterface.invokeASDCStatusData(request);
243 assertNull(response);
247 protected String createURLWithPort(String uri) {
248 return "http://localhost:" + port + uri;