2e5ad13c216ce314b39a01d33d097af4d691aa2c
[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.urlPathMatching;
26 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
27 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNull;
30 import java.io.File;
31 import java.util.HashSet;
32 import java.util.Set;
33 import javax.transaction.Transactional;
34 import javax.ws.rs.core.Response;
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.TemporaryFolder;
39 import org.mockito.Spy;
40 import org.onap.so.asdc.BaseTest;
41 import org.onap.so.asdc.client.test.emulators.DistributionClientEmulator;
42 import org.onap.so.asdc.client.test.emulators.NotificationDataImpl;
43 import org.onap.so.db.catalog.beans.AllottedResource;
44 import org.onap.so.db.catalog.beans.AllottedResourceCustomization;
45 import org.onap.so.db.catalog.beans.Service;
46 import org.onap.so.db.catalog.data.repository.AllottedResourceRepository;
47 import org.onap.so.db.catalog.data.repository.NetworkResourceRepository;
48 import org.onap.so.db.catalog.data.repository.ServiceRepository;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.boot.test.web.client.TestRestTemplate;
51 import org.springframework.boot.web.server.LocalServerPort;
52 import org.springframework.http.HttpEntity;
53 import org.springframework.http.HttpHeaders;
54 import org.springframework.http.HttpMethod;
55 import org.springframework.http.ResponseEntity;
56 import com.fasterxml.jackson.databind.ObjectMapper;
57
58 public class ASDCRestInterfaceTest extends BaseTest {
59
60     @Autowired
61     private AllottedResourceRepository allottedRepo;
62
63     @Autowired
64     private ServiceRepository serviceRepo;
65
66     @Autowired
67     private NetworkResourceRepository networkRepo;
68
69     @Autowired
70     private ASDCRestInterface asdcRestInterface;
71
72     private TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
73
74     private HttpHeaders headers = new HttpHeaders();
75
76     @Spy
77     DistributionClientEmulator spyClient = new DistributionClientEmulator();
78
79     @LocalServerPort
80     private int port;
81
82
83     @Rule
84     public TemporaryFolder folder = new TemporaryFolder();
85
86
87     @Before
88     public void setUp() {
89         // ASDC Controller writes to this path
90         System.setProperty("mso.config.path", folder.getRoot().toString());
91     }
92
93     @Test
94     @Transactional
95     public void testAllottedResourceService() throws Exception {
96
97         wireMockServer.stubFor(post(urlPathMatching("/aai/.*"))
98                 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")));
99
100         ObjectMapper mapper = new ObjectMapper();
101         NotificationDataImpl request =
102                 mapper.readValue(new File("src/test/resources/resource-examples/allottedresource/notif-portm.json"),
103                         NotificationDataImpl.class);
104         headers.add("resource-location", "src/test/resources/resource-examples/allottedresource/");
105         HttpEntity<NotificationDataImpl> entity = new HttpEntity<NotificationDataImpl>(request, headers);
106
107         ResponseEntity<String> response = restTemplate.exchange(createURLWithPort("test/treatNotification/v1"),
108                 HttpMethod.POST, entity, String.class);
109
110         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
111
112         AllottedResource expectedService = new AllottedResource();
113         expectedService.setDescription("rege1802pnf");
114         expectedService.setModelInvariantUUID("b8f83c3f-077c-4e2c-b489-c66382060436");
115         expectedService.setModelName("rege1802pnf");
116         expectedService.setModelUUID("5b18c75e-2d08-4bf2-ad58-4ea704ec648d");
117         expectedService.setModelVersion("1.0");
118         expectedService.setSubcategory("Contrail Route");
119         expectedService.setToscaNodeType("org.openecomp.resource.pnf.Rege1802pnf");
120         Set<AllottedResourceCustomization> arCustomizationSet = new HashSet<AllottedResourceCustomization>();
121         AllottedResourceCustomization arCustomization = new AllottedResourceCustomization();
122         arCustomization.setModelCustomizationUUID("f62bb612-c5d4-4406-865c-0abec30631ba");
123         arCustomization.setModelInstanceName("rege1802pnf 0");
124         arCustomizationSet.add(arCustomization);
125
126         arCustomization.setAllottedResource(expectedService);
127
128
129         expectedService.setAllotedResourceCustomization(arCustomizationSet);
130
131         AllottedResource actualResponse = allottedRepo.findResourceByModelUUID("5b18c75e-2d08-4bf2-ad58-4ea704ec648d");
132
133
134         if (actualResponse == null)
135             throw new Exception("No Allotted Resource Written to database");
136
137
138         assertThat(actualResponse, sameBeanAs(expectedService).ignoring("0x1.created")
139                 .ignoring("0x1.allotedResourceCustomization.created"));
140     }
141
142     @Test
143     @Transactional
144     public void test_VFW_Distrobution() throws Exception {
145
146         wireMockServer.stubFor(post(urlPathMatching("/aai/.*"))
147                 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")));
148
149         ObjectMapper mapper = new ObjectMapper();
150         NotificationDataImpl request = mapper.readValue(
151                 new File("src/test/resources/resource-examples/vFW/notification.json"), NotificationDataImpl.class);
152         headers.add("resource-location", "src/test/resources/resource-examples/vFW/");
153         HttpEntity<NotificationDataImpl> entity = new HttpEntity<NotificationDataImpl>(request, headers);
154
155         ResponseEntity<String> response = restTemplate.exchange(createURLWithPort("test/treatNotification/v1"),
156                 HttpMethod.POST, entity, String.class);
157
158         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
159
160         Service expectedService = new Service();
161         expectedService.setDescription("catalog service description");
162         expectedService.setModelInvariantUUID("3164f9ff-d7e7-4813-ab32-6be7e1cacb18");
163         expectedService.setModelName("vFW 2019-04-10 21:53:05");
164         expectedService.setModelUUID("e16e4ed9-3429-423a-bc3c-1389ae91491c");
165         expectedService.setModelVersion("1.0");
166
167
168
169         Service actualService = serviceRepo.findOneByModelUUID("e16e4ed9-3429-423a-bc3c-1389ae91491c");
170
171
172         if (actualService == null)
173             throw new Exception("No Allotted Resource Written to database");
174
175         assertEquals(expectedService.getModelName(), actualService.getModelName());
176     }
177
178     @Test
179     public void invokeASDCStatusDataNullTest() {
180         String request = "";
181         Response response = asdcRestInterface.invokeASDCStatusData(request);
182         assertNull(response);
183
184     }
185
186
187
188     protected String createURLWithPort(String uri) {
189         return "http://localhost:" + port + uri;
190     }
191 }