Use distribution json for workflow install
[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.assertNull;
31 import java.io.File;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import java.util.HashSet;
35 import java.util.Set;
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;
62
63 public class ASDCRestInterfaceTest extends BaseTest {
64
65     @Autowired
66     private AllottedResourceRepository allottedRepo;
67
68     @Autowired
69     private ServiceRepository serviceRepo;
70
71     @Autowired
72     private NetworkResourceRepository networkRepo;
73
74     @Autowired
75     private WorkflowRepository workflowRepo;
76
77     @Autowired
78     private ASDCRestInterface asdcRestInterface;
79
80     private TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
81
82     private HttpHeaders headers = new HttpHeaders();
83
84     @Spy
85     DistributionClientEmulator spyClient = new DistributionClientEmulator();
86
87     @LocalServerPort
88     private int port;
89
90
91     @Rule
92     public TemporaryFolder folder = new TemporaryFolder();
93
94
95     @Before
96     public void setUp() {
97         // ASDC Controller writes to this path
98         System.setProperty("mso.config.path", folder.getRoot().toString());
99     }
100
101     @Test
102     @Transactional
103     public void testAllottedResourceService() throws Exception {
104
105         wireMockServer.stubFor(post(urlPathMatching("/aai/.*"))
106                 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")));
107
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())));
111
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);
118
119         ResponseEntity<String> response = restTemplate.exchange(createURLWithPort("test/treatNotification/v1"),
120                 HttpMethod.POST, entity, String.class);
121
122         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
123
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);
137
138         arCustomization.setAllottedResource(expectedService);
139
140
141         expectedService.setAllotedResourceCustomization(arCustomizationSet);
142
143         AllottedResource actualResponse = allottedRepo.findResourceByModelUUID("5b18c75e-2d08-4bf2-ad58-4ea704ec648d");
144
145
146         if (actualResponse == null)
147             throw new Exception("No Allotted Resource Written to database");
148
149
150         assertThat(actualResponse, sameBeanAs(expectedService).ignoring("0x1.created")
151                 .ignoring("0x1.allotedResourceCustomization.created"));
152     }
153
154     @Test
155     @Transactional
156     public void test_VFW_Distrobution() throws Exception {
157
158         wireMockServer.stubFor(post(urlPathMatching("/aai/.*"))
159                 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")));
160
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())));
164
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);
170
171         ResponseEntity<String> response = restTemplate.exchange(createURLWithPort("test/treatNotification/v1"),
172                 HttpMethod.POST, entity, String.class);
173
174         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
175
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");
182
183
184
185         Service actualService = serviceRepo.findOneByModelUUID("e16e4ed9-3429-423a-bc3c-1389ae91491c");
186
187
188         if (actualService == null)
189             throw new Exception("No Allotted Resource Written to database");
190
191         assertEquals(expectedService.getModelName(), actualService.getModelName());
192     }
193
194     @Test
195     @Transactional
196     public void testWorkflowDistribution() throws Exception {
197
198         wireMockServer.stubFor(post(urlPathMatching("/aai/.*"))
199                 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")));
200
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())));
204
205         wireMockServer.stubFor(
206                 post(urlPathEqualTo("/sobpmnengine/deployment/create")).willReturn(aResponse().withStatus(200)));
207
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);
214
215         ResponseEntity<String> response = restTemplate.exchange(createURLWithPort("test/treatNotification/v1"),
216                 HttpMethod.POST, entity, String.class);
217
218         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
219
220         Workflow actualResponse = workflowRepo.findByArtifactUUID("a90f8eaa-7c20-422f-8c81-aacbca6fb9e7");
221
222         if (actualResponse == null)
223             throw new Exception("No Workflow Written to database");
224
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);
231
232         Workflow shouldNotBeFound = workflowRepo.findByArtifactUUID("f27066a1-c3a7-4672-b02e-1251b74b7b71");
233         assertNull(shouldNotBeFound);
234     }
235
236     @Test
237     public void invokeASDCStatusDataNullTest() {
238         String request = "";
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);
244
245     }
246
247     protected String createURLWithPort(String uri) {
248         return "http://localhost:" + port + uri;
249     }
250 }