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