60dc811427997a75a094ef59683803c9f553a94f
[sdnc/apps.git] /
1 package org.onap.sdnc.apps.ms.gra.controllers;
2
3 import static org.junit.Assert.assertEquals;
4
5 import java.io.IOException;
6 import java.nio.file.Files;
7 import java.nio.file.Paths;
8 import java.util.ArrayList;
9
10 import com.fasterxml.jackson.databind.ObjectMapper;
11
12 import org.junit.BeforeClass;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.onap.sdnc.apps.ms.gra.GenericResourceMsApp;
16 import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadDataRepository;
17 import org.onap.sdnc.apps.ms.gra.data.ConfigServices;
18 import org.onap.sdnc.apps.ms.gra.data.ConfigServicesRepository;
19 import org.onap.sdnc.apps.ms.gra.data.OperationalServicesRepository;
20 import org.onap.sdnc.apps.ms.gra.swagger.model.*;
21 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
23 import org.springframework.boot.test.context.SpringBootTest;
24 import org.springframework.http.MediaType;
25 import org.springframework.test.context.junit4.SpringRunner;
26 import org.springframework.test.web.servlet.MockMvc;
27 import org.springframework.test.web.servlet.MvcResult;
28 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
29 import org.springframework.transaction.annotation.Transactional;
30
31 @RunWith(SpringRunner.class)
32 @SpringBootTest(classes={GenericResourceMsApp.class})
33 @AutoConfigureMockMvc
34 @Transactional
35 public class OperationsApiControllerTest {
36
37     private final static String PRELOAD_NETWORK_URL = "/operations/GENERIC-RESOURCE-API:preload-network-topology-operation/";
38     private final static String PRELOAD_VFMODULE_URL = "/operations/GENERIC-RESOURCE-API:preload-vf-module-topology-operation/";
39     private final static String SERVICE_TOPOLOGY_URL = "/operations/GENERIC-RESOURCE-API:service-topology-operation/";
40     private final static String NETWORK_TOPOLOGY_URL = "/operations/GENERIC-RESOURCE-API:network-topology-operation/";
41     private final static String VNF_TOPOLOGY_URL = "/operations/GENERIC-RESOURCE-API:vnf-topology-operation/";
42
43
44     @Autowired
45     private MockMvc mvc;
46
47     @Autowired
48     ConfigPreloadDataRepository configPreloadDataRepository;
49
50     @Autowired
51     ConfigServicesRepository configServicesRepository;
52
53     @Autowired
54     OperationalServicesRepository operationalServicesRepository;
55
56     @Autowired
57     OperationsApiController operationsApiController;
58
59     @BeforeClass
60     public static void setUp() throws Exception {
61         System.out.println("OperationsApiControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
62         System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
63         System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
64         System.setProperty("sdnc.config.dir", "src/test/resources");
65    
66     }
67
68
69     @Test
70     public void operationsGENERICRESOURCEAPIpreloadNetworkTopologyOperationPost() throws Exception {
71
72         // Remove any existing preload data
73         configPreloadDataRepository.deleteAll();
74
75         // Add invalid content
76         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
77         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
78                 .andReturn();
79         assertEquals(403, mvcResult.getResponse().getStatus());
80         assertEquals(0, configPreloadDataRepository.count());
81
82         // Add valid content
83         content = readFileContent("src/test/resources/preload1-rpc-network.json");
84         mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
85                 .andReturn();
86         assertEquals(200, mvcResult.getResponse().getStatus());
87         assertEquals(1, configPreloadDataRepository.count());
88
89     }
90
91     @Test
92     public void operationsGENERICRESOURCEAPIpreloadVfModuleTopologyOperationPost() throws Exception {
93         // Remove any existing preload data
94         configPreloadDataRepository.deleteAll();
95
96         // Add invalid content
97         String content = readFileContent("src/test/resources/preload1-rpc-network.json");
98         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
99                 .andReturn();
100         assertEquals(403, mvcResult.getResponse().getStatus());
101         assertEquals(0, configPreloadDataRepository.count());
102
103         // Add valid content
104         content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
105         mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
106                 .andReturn();
107         assertEquals(200, mvcResult.getResponse().getStatus());
108         assertEquals(1, configPreloadDataRepository.count());
109     }
110
111     @Test
112     public void operationsGENERICRESOURCEAPIserviceTopologyOperationAssignPost() throws Exception {
113
114         // Remove any existing service data
115         configServicesRepository.deleteAll();
116         operationalServicesRepository.deleteAll();
117
118         // Add invalid content
119         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
120         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
121                 .andReturn();
122         assertEquals(200, mvcResult.getResponse().getStatus());
123         assertEquals(0, configServicesRepository.count());
124         assertEquals(0, operationalServicesRepository.count());
125
126         // Add valid content
127         content = readFileContent("src/test/resources/service-assign-rpc.json");
128         mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
129                 .andReturn();
130         assertEquals(200, mvcResult.getResponse().getStatus());
131         assertEquals(1, configServicesRepository.count());
132         assertEquals(0, operationalServicesRepository.count());
133     }
134
135     @Test
136     public void serviceTopologyOperationAsync() throws Exception {
137         configServicesRepository.deleteAll();
138         GenericResourceApiVnfOperationInformationBodyparam inputParam = operationsApiController.getObjectMapper().get().readValue(readFileContent("src/test/resources/vnf-assign-rpc.json"), GenericResourceApiVnfOperationInformationBodyparam.class);
139         operationsApiController.processAsyncVnfTopologyOperation("vnf-topology-operation",inputParam);
140
141         loadVnfData("src/test/resources/vnf-data.json");
142         inputParam.getInput().getServiceInformation().setServiceInstanceId("98f189dd-2971-46f5-b4f1-1a9a323f39a4");
143         operationsApiController.processAsyncVnfTopologyOperation("vnf-topology-operation",inputParam);
144         configServicesRepository.deleteAll();
145     }
146
147     private void loadVnfData(String path) throws IOException {
148         ObjectMapper objectMapper = new ObjectMapper();
149         String content = readFileContent(path);
150         GenericResourceApiServicedataServiceData svcData = new GenericResourceApiServicedataServiceData();
151         GenericResourceApiServicedataServicedataVnfsVnf vnfData = objectMapper.readValue(content, GenericResourceApiServicedataServicedataVnfsVnf.class);
152         svcData.setVnfs(new GenericResourceApiServicedataServicedataVnfs());
153         svcData.getVnfs().setVnf(new ArrayList<>());
154         svcData.getVnfs().addVnfItem(vnfData);
155         ConfigServices newService = new ConfigServices();
156         newService.setSvcData(objectMapper.writeValueAsString(svcData));
157         newService.setSvcInstanceId("98f189dd-2971-46f5-b4f1-1a9a323f39a4");
158         configServicesRepository.save(newService);
159     }
160
161     @Test
162     public void operationsGENERICRESOURCEAPInetworkTopologyOperationAssignPost() throws Exception {
163
164         // Remove any existing service data
165         configServicesRepository.deleteAll();
166         operationalServicesRepository.deleteAll();
167
168         // Load services data
169         loadServicesData("src/test/resources/service1.json");
170
171         // Add invalid content
172         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
173         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
174                 .andReturn();
175         assertEquals(200, mvcResult.getResponse().getStatus());
176
177         // Add valid content
178         content = readFileContent("src/test/resources/network-assign-rpc.json");
179         mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
180                 .andReturn();
181         assertEquals(200, mvcResult.getResponse().getStatus());
182
183     }
184
185     @Test
186     public void operationsGENERICRESOURCEAPIvnfTopologyOperationAssignPost() throws Exception {
187
188         // Remove any existing service data
189         configServicesRepository.deleteAll();
190         operationalServicesRepository.deleteAll();
191
192         // Load services data
193         loadServicesData("src/test/resources/service1.json");
194
195         // Add invalid content
196         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
197         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
198                 .andReturn();
199         assertEquals(200, mvcResult.getResponse().getStatus());
200
201         // Add valid content
202         content = readFileContent("src/test/resources/vnf-assign-rpc.json");
203         mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
204                 .andReturn();
205         assertEquals(200, mvcResult.getResponse().getStatus());
206
207     }
208
209
210     private void loadServicesData(String path) throws IOException {
211         ObjectMapper objectMapper = new ObjectMapper();
212         String content = readFileContent(path);
213         GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
214
215         for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
216             ConfigServices newService = new ConfigServices();
217             newService.setSvcInstanceId(service.getServiceInstanceId());
218             newService.setSvcData(objectMapper.writeValueAsString(service.getServiceData()));
219             newService.setServiceStatus(service.getServiceStatus());
220             configServicesRepository.save(newService);
221         }
222     }
223
224     private String readFileContent(String path) throws IOException {
225         String content = new String(Files.readAllBytes(Paths.get(path)));
226         return content;
227     }
228 }