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