Replaced all tabs with spaces in java and pom.xml
[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.data.repository.AllottedResourceRepository;
46 import org.onap.so.db.catalog.data.repository.NetworkResourceRepository;
47 import org.onap.so.db.catalog.data.repository.ServiceRepository;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.boot.test.web.client.TestRestTemplate;
50 import org.springframework.boot.web.server.LocalServerPort;
51 import org.springframework.http.HttpEntity;
52 import org.springframework.http.HttpHeaders;
53 import org.springframework.http.HttpMethod;
54 import org.springframework.http.ResponseEntity;
55 import com.fasterxml.jackson.databind.ObjectMapper;
56
57 public class ASDCRestInterfaceTest extends BaseTest {
58
59     @Autowired
60     private AllottedResourceRepository allottedRepo;
61
62     @Autowired
63     private ServiceRepository serviceRepo;
64
65     @Autowired
66     private NetworkResourceRepository networkRepo;
67
68     @Autowired
69     private ASDCRestInterface asdcRestInterface;
70
71     private TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
72
73     private HttpHeaders headers = new HttpHeaders();
74
75     @Spy
76     DistributionClientEmulator spyClient = new DistributionClientEmulator();
77
78     @LocalServerPort
79     private int port;
80
81
82     @Rule
83     public TemporaryFolder folder = new TemporaryFolder();
84
85
86     @Before
87     public void setUp() {
88         // ASDC Controller writes to this path
89         System.setProperty("mso.config.path", folder.getRoot().toString());
90     }
91
92     @Test
93     @Transactional
94     public void testAllottedResourceService() throws Exception {
95
96         wireMockServer.stubFor(post(urlPathMatching("/aai/.*"))
97                 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")));
98
99         ObjectMapper mapper = new ObjectMapper();
100         NotificationDataImpl request =
101                 mapper.readValue(new File("src/test/resources/resource-examples/allottedresource/notif-portm.json"),
102                         NotificationDataImpl.class);
103         headers.add("resource-location", "src/test/resources/resource-examples/allottedresource/");
104         HttpEntity<NotificationDataImpl> entity = new HttpEntity<NotificationDataImpl>(request, headers);
105
106         ResponseEntity<String> response = restTemplate.exchange(createURLWithPort("test/treatNotification/v1"),
107                 HttpMethod.POST, entity, String.class);
108
109         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
110
111         AllottedResource expectedService = new AllottedResource();
112         expectedService.setDescription("rege1802pnf");
113         expectedService.setModelInvariantUUID("b8f83c3f-077c-4e2c-b489-c66382060436");
114         expectedService.setModelName("rege1802pnf");
115         expectedService.setModelUUID("5b18c75e-2d08-4bf2-ad58-4ea704ec648d");
116         expectedService.setModelVersion("1.0");
117         expectedService.setSubcategory("Contrail Route");
118         expectedService.setToscaNodeType("org.openecomp.resource.pnf.Rege1802pnf");
119         Set<AllottedResourceCustomization> arCustomizationSet = new HashSet<AllottedResourceCustomization>();
120         AllottedResourceCustomization arCustomization = new AllottedResourceCustomization();
121         arCustomization.setModelCustomizationUUID("f62bb612-c5d4-4406-865c-0abec30631ba");
122         arCustomization.setModelInstanceName("rege1802pnf 0");
123         arCustomizationSet.add(arCustomization);
124
125         arCustomization.setAllottedResource(expectedService);
126
127
128         expectedService.setAllotedResourceCustomization(arCustomizationSet);
129
130         AllottedResource actualResponse = allottedRepo.findResourceByModelUUID("5b18c75e-2d08-4bf2-ad58-4ea704ec648d");
131
132
133         if (actualResponse == null)
134             throw new Exception("No Allotted Resource Written to database");
135
136
137         assertThat(actualResponse, sameBeanAs(expectedService).ignoring("0x1.created")
138                 .ignoring("0x1.allotedResourceCustomization.created"));
139     }
140
141     @Test
142     public void invokeASDCStatusDataNullTest() {
143         String request = "";
144         Response response = asdcRestInterface.invokeASDCStatusData(request);
145         assertNull(response);
146
147     }
148
149
150
151     protected String createURLWithPort(String uri) {
152         return "http://localhost:" + port + uri;
153     }
154 }