Addressed vCPE-Infra processing issue ASDC Controller
[so.git] / asdc-controller / src / test / java / org / onap / so / asdc / client / test / rest / ASDCRestInterfaceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.asdc.client.test.rest;
22
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.assertNotNull;
31 import static org.junit.Assert.assertNull;
32 import static org.junit.Assert.assertTrue;
33 import java.io.File;
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
36 import java.util.HashSet;
37 import java.util.List;
38 import java.util.Optional;
39 import java.util.Set;
40 import javax.transaction.Transactional;
41 import javax.ws.rs.core.Response;
42 import org.junit.Before;
43 import org.junit.Rule;
44 import org.junit.Test;
45 import org.junit.rules.TemporaryFolder;
46 import org.mockito.Spy;
47 import org.onap.so.asdc.BaseTest;
48 import org.onap.so.asdc.client.test.emulators.DistributionClientEmulator;
49 import org.onap.so.asdc.client.test.emulators.NotificationDataImpl;
50 import org.onap.so.db.catalog.beans.AllottedResource;
51 import org.onap.so.db.catalog.beans.AllottedResourceCustomization;
52 import org.onap.so.db.catalog.beans.NetworkResource;
53 import org.onap.so.db.catalog.beans.NetworkResourceCustomization;
54 import org.onap.so.db.catalog.beans.Service;
55 import org.onap.so.db.catalog.beans.ToscaCsar;
56 import org.onap.so.db.catalog.beans.Workflow;
57 import org.onap.so.db.catalog.data.repository.AllottedResourceRepository;
58 import org.onap.so.db.catalog.data.repository.NetworkResourceRepository;
59 import org.onap.so.db.catalog.data.repository.ServiceRepository;
60 import org.onap.so.db.catalog.data.repository.ToscaCsarRepository;
61 import org.onap.so.db.catalog.data.repository.WorkflowRepository;
62 import org.springframework.beans.factory.annotation.Autowired;
63 import org.springframework.boot.test.web.client.TestRestTemplate;
64 import org.springframework.boot.web.server.LocalServerPort;
65 import org.springframework.http.HttpEntity;
66 import org.springframework.http.HttpHeaders;
67 import org.springframework.http.HttpMethod;
68 import org.springframework.http.ResponseEntity;
69 import com.fasterxml.jackson.databind.ObjectMapper;
70
71 public class ASDCRestInterfaceTest extends BaseTest {
72
73     @Autowired
74     private AllottedResourceRepository allottedRepo;
75
76     @Autowired
77     private ServiceRepository serviceRepo;
78
79     @Autowired
80     private NetworkResourceRepository networkRepo;
81
82     @Autowired
83     private WorkflowRepository workflowRepo;
84
85     @Autowired
86     private ToscaCsarRepository toscaCsarRepo;
87
88     @Autowired
89     private ASDCRestInterface asdcRestInterface;
90
91     private TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
92
93     private HttpHeaders headers = new HttpHeaders();
94
95     @Spy
96     DistributionClientEmulator spyClient = new DistributionClientEmulator();
97
98     @LocalServerPort
99     private int port;
100
101
102     @Rule
103     public TemporaryFolder folder = new TemporaryFolder();
104
105
106     @Before
107     public void setUp() {
108         // ASDC Controller writes to this path
109         System.setProperty("mso.config.path", folder.getRoot().toString());
110     }
111
112     @Test
113     @Transactional
114     public void testAllottedResourceService() throws Exception {
115
116         wireMockServer.stubFor(post(urlPathMatching("/aai/.*"))
117                 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")));
118
119         wireMockServer.stubFor(post(urlPathMatching("/v1.0/activity-spec"))
120                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
121                         .withStatus(org.springframework.http.HttpStatus.ACCEPTED.value())));
122
123         ObjectMapper mapper = new ObjectMapper();
124         NotificationDataImpl request =
125                 mapper.readValue(new File("src/test/resources/resource-examples/allottedresource/notif-portm.json"),
126                         NotificationDataImpl.class);
127         headers.add("resource-location", "src/test/resources/resource-examples/allottedresource/");
128         HttpEntity<NotificationDataImpl> entity = new HttpEntity<NotificationDataImpl>(request, headers);
129
130         ResponseEntity<String> response = restTemplate.exchange(createURLWithPort("test/treatNotification/v1"),
131                 HttpMethod.POST, entity, String.class);
132
133         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
134
135         AllottedResource expectedService = new AllottedResource();
136         expectedService.setDescription("rege1802pnf");
137         expectedService.setModelInvariantUUID("b8f83c3f-077c-4e2c-b489-c66382060436");
138         expectedService.setModelName("rege1802pnf");
139         expectedService.setModelUUID("5b18c75e-2d08-4bf2-ad58-4ea704ec648d");
140         expectedService.setModelVersion("1.0");
141         expectedService.setSubcategory("Contrail Route");
142         expectedService.setToscaNodeType("org.openecomp.resource.pnf.Rege1802pnf");
143         Set<AllottedResourceCustomization> arCustomizationSet = new HashSet<AllottedResourceCustomization>();
144         AllottedResourceCustomization arCustomization = new AllottedResourceCustomization();
145         arCustomization.setModelCustomizationUUID("f62bb612-c5d4-4406-865c-0abec30631ba");
146         arCustomization.setModelInstanceName("rege1802pnf 0");
147         arCustomizationSet.add(arCustomization);
148
149         arCustomization.setAllottedResource(expectedService);
150
151
152         expectedService.setAllotedResourceCustomization(arCustomizationSet);
153
154         AllottedResource actualResponse = allottedRepo.findResourceByModelUUID("5b18c75e-2d08-4bf2-ad58-4ea704ec648d");
155
156
157         if (actualResponse == null)
158             throw new Exception("No Allotted Resource Written to database");
159
160
161         assertThat(actualResponse, sameBeanAs(expectedService).ignoring("0x1.created")
162                 .ignoring("0x1.allotedResourceCustomization.created"));
163     }
164
165     @Test
166     @Transactional
167     public void test_VFW_Distrobution() throws Exception {
168
169         wireMockServer.stubFor(post(urlPathMatching("/aai/.*"))
170                 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")));
171
172         wireMockServer.stubFor(post(urlPathMatching("/v1.0/activity-spec"))
173                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
174                         .withStatus(org.springframework.http.HttpStatus.ACCEPTED.value())));
175
176         ObjectMapper mapper = new ObjectMapper();
177         NotificationDataImpl request = mapper.readValue(
178                 new File("src/test/resources/resource-examples/vFW/notification.json"), NotificationDataImpl.class);
179         headers.add("resource-location", "src/test/resources/resource-examples/vFW/");
180         HttpEntity<NotificationDataImpl> entity = new HttpEntity<NotificationDataImpl>(request, headers);
181
182         ResponseEntity<String> response = restTemplate.exchange(createURLWithPort("test/treatNotification/v1"),
183                 HttpMethod.POST, entity, String.class);
184
185         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
186
187         Service expectedService = new Service();
188         expectedService.setDescription("catalog service description");
189         expectedService.setModelInvariantUUID("3164f9ff-d7e7-4813-ab32-6be7e1cacb18");
190         expectedService.setModelName("vFW 2019-04-10 21:53:05");
191         expectedService.setModelUUID("e16e4ed9-3429-423a-bc3c-1389ae91491c");
192         expectedService.setModelVersion("1.0");
193
194
195
196         Service actualService = serviceRepo.findOneByModelUUID("e16e4ed9-3429-423a-bc3c-1389ae91491c");
197
198
199         if (actualService == null)
200             throw new Exception("No Allotted Resource Written to database");
201
202         assertEquals(expectedService.getModelName(), actualService.getModelName());
203     }
204
205     @Test
206     @Transactional
207     public void testWorkflowDistribution() throws Exception {
208
209         wireMockServer.stubFor(post(urlPathMatching("/aai/.*"))
210                 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")));
211
212         wireMockServer.stubFor(post(urlPathMatching("/v1.0/activity-spec"))
213                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
214                         .withStatus(org.springframework.http.HttpStatus.ACCEPTED.value())));
215
216         wireMockServer.stubFor(
217                 post(urlPathEqualTo("/sobpmnengine/deployment/create")).willReturn(aResponse().withStatus(200)));
218
219         ObjectMapper mapper = new ObjectMapper();
220         NotificationDataImpl request = mapper.readValue(
221                 new File("src/test/resources/resource-examples/WorkflowBpmn/workflow-distribution.json"),
222                 NotificationDataImpl.class);
223         headers.add("resource-location", "src/test/resources/resource-examples/WorkflowBpmn/");
224         HttpEntity<NotificationDataImpl> entity = new HttpEntity<NotificationDataImpl>(request, headers);
225
226         ResponseEntity<String> response = restTemplate.exchange(createURLWithPort("test/treatNotification/v1"),
227                 HttpMethod.POST, entity, String.class);
228
229         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
230
231         Workflow actualResponse = workflowRepo.findByArtifactUUID("a90f8eaa-7c20-422f-8c81-aacbca6fb9e7");
232
233         if (actualResponse == null)
234             throw new Exception("No Workflow Written to database");
235
236         String expectedBody = new String(
237                 Files.readAllBytes(Paths.get("src/test/resources/resource-examples/WorkflowBpmn/TestWF2-1_0.bpmn")));
238         assertEquals(actualResponse.getArtifactChecksum(), "ZjUzNjg1NDMyMTc4MWJmZjFlNDcyOGQ0Zjc1YWQwYzQ\u003d");
239         assertEquals(actualResponse.getArtifactName(), "TestWF2-1_0.bpmn");
240         assertEquals(actualResponse.getDescription(), "Workflow Artifact Description");
241         assertEquals(actualResponse.getBody(), expectedBody);
242
243         Workflow shouldNotBeFound = workflowRepo.findByArtifactUUID("f27066a1-c3a7-4672-b02e-1251b74b7b71");
244         assertNull(shouldNotBeFound);
245     }
246
247     @Test
248     public void invokeASDCStatusDataNullTest() {
249         String request = "";
250         wireMockServer.stubFor(post(urlPathMatching("/v1.0/activity-spec"))
251                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
252                         .withStatus(org.springframework.http.HttpStatus.ACCEPTED.value())));
253         Response response = asdcRestInterface.invokeASDCStatusData(request);
254         assertNull(response);
255
256     }
257
258
259     @Test
260     public void test_Vcpe_Infra_Distribution() throws Exception {
261         wireMockServer.stubFor(post(urlPathMatching("/aai/.*"))
262                 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")));
263
264         wireMockServer.stubFor(post(urlPathMatching("/v1.0/activity-spec"))
265                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
266                         .withStatus(org.springframework.http.HttpStatus.ACCEPTED.value())));
267
268         String resourceLocation = "src/test/resources/resource-examples/vcpe-infra/";
269
270         ObjectMapper mapper = new ObjectMapper();
271         NotificationDataImpl request = mapper.readValue(new File(resourceLocation + "demovcpeinfra-notification.json"),
272                 NotificationDataImpl.class);
273         headers.add("resource-location", resourceLocation);
274         HttpEntity<NotificationDataImpl> entity = new HttpEntity<NotificationDataImpl>(request, headers);
275
276         ResponseEntity<String> response = restTemplate.exchange(createURLWithPort("test/treatNotification/v1"),
277                 HttpMethod.POST, entity, String.class);
278         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
279
280         Optional<ToscaCsar> toscaCsar = toscaCsarRepo.findById("144606d8-a505-4ba0-90a9-6d1c6219fc6b");
281         assertTrue(toscaCsar.isPresent());
282         assertEquals("service-Demovcpeinfra-csar.csar", toscaCsar.get().getName());
283
284         Optional<Service> service = serviceRepo.findById("8a77cbbb-9850-40bb-a42f-7aec8e3e6ab7");
285         assertTrue(service.isPresent());
286         assertEquals("demoVCPEInfra", service.get().getModelName());
287
288         Optional<NetworkResource> networkResource = networkRepo.findById("89789b26-a46b-4cee-aed0-d46e21f93a5e");
289         assertTrue(networkResource.isPresent());
290         assertEquals("Generic NeutronNet", networkResource.get().getModelName());
291     }
292
293     protected String createURLWithPort(String uri) {
294         return "http://localhost:" + port + uri;
295     }
296 }