b285d9e9e5346950690cc9688ebdf15072139a5f
[sdnc/apps.git] /
1 package org.onap.sdnc.apps.ms.gra.controllers;
2
3 import org.junit.BeforeClass;
4 import org.junit.Test;
5 import org.junit.runner.RunWith;
6 import org.onap.sdnc.apps.ms.gra.GenericResourceMsApp;
7 import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadDataRepository;
8 import org.onap.sdnc.apps.ms.gra.data.ConfigServices;
9 import org.onap.sdnc.apps.ms.gra.data.ConfigServicesRepository;
10 import org.onap.sdnc.apps.ms.gra.data.OperationalServicesRepository;
11 import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServiceModelInfrastructure;
12 import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicemodelinfrastructureService;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
15 import org.springframework.boot.test.context.SpringBootTest;
16 import org.springframework.http.MediaType;
17 import org.springframework.test.context.junit4.SpringRunner;
18 import org.springframework.test.web.servlet.MockMvc;
19 import org.springframework.test.web.servlet.MvcResult;
20 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
21 import org.springframework.transaction.annotation.Transactional;
22
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.nio.file.Paths;
26
27 import com.fasterxml.jackson.databind.ObjectMapper;
28
29 import static org.junit.Assert.*;
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     @BeforeClass
57     public static void setUp() throws Exception {
58         System.out.println("OperationsApiControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
59         System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
60         System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
61         System.setProperty("sdnc.config.dir", "src/test/resources");
62    
63     }
64
65
66     @Test
67     public void operationsGENERICRESOURCEAPIpreloadNetworkTopologyOperationPost() throws Exception {
68
69         // Remove any existing preload data
70         configPreloadDataRepository.deleteAll();
71
72         // Add invalid content
73         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
74         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
75                 .andReturn();
76         assertEquals(403, mvcResult.getResponse().getStatus());
77         assertEquals(0, configPreloadDataRepository.count());
78
79         // Add valid content
80         content = readFileContent("src/test/resources/preload1-rpc-network.json");
81         mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
82                 .andReturn();
83         assertEquals(200, mvcResult.getResponse().getStatus());
84         assertEquals(1, configPreloadDataRepository.count());
85
86     }
87
88     @Test
89     public void operationsGENERICRESOURCEAPIpreloadVfModuleTopologyOperationPost() throws Exception {
90         // Remove any existing preload data
91         configPreloadDataRepository.deleteAll();
92
93         // Add invalid content
94         String content = readFileContent("src/test/resources/preload1-rpc-network.json");
95         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
96                 .andReturn();
97         assertEquals(403, mvcResult.getResponse().getStatus());
98         assertEquals(0, configPreloadDataRepository.count());
99
100         // Add valid content
101         content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
102         mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
103                 .andReturn();
104         assertEquals(200, mvcResult.getResponse().getStatus());
105         assertEquals(1, configPreloadDataRepository.count());
106     }
107
108     @Test
109     public void operationsGENERICRESOURCEAPIserviceTopologyOperationAssignPost() throws Exception {
110
111         // Remove any existing service data
112         configServicesRepository.deleteAll();
113         operationalServicesRepository.deleteAll();
114
115         // Add invalid content
116         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
117         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
118                 .andReturn();
119         assertEquals(200, mvcResult.getResponse().getStatus());
120         assertEquals(0, configServicesRepository.count());
121         assertEquals(0, operationalServicesRepository.count());
122
123         // Add valid content
124         content = readFileContent("src/test/resources/service-assign-rpc.json");
125         mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
126                 .andReturn();
127         assertEquals(200, mvcResult.getResponse().getStatus());
128         assertEquals(1, configServicesRepository.count());
129         assertEquals(0, operationalServicesRepository.count());
130
131     }
132
133     @Test
134     public void operationsGENERICRESOURCEAPInetworkTopologyOperationAssignPost() throws Exception {
135
136         // Remove any existing service data
137         configServicesRepository.deleteAll();
138         operationalServicesRepository.deleteAll();
139
140         // Load services data
141         loadServicesData("src/test/resources/service1.json");
142
143         // Add invalid content
144         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
145         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
146                 .andReturn();
147         assertEquals(200, mvcResult.getResponse().getStatus());
148
149         // Add valid content
150         content = readFileContent("src/test/resources/network-assign-rpc.json");
151         mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
152                 .andReturn();
153         assertEquals(200, mvcResult.getResponse().getStatus());
154
155     }
156
157     @Test
158     public void operationsGENERICRESOURCEAPIvnfTopologyOperationAssignPost() throws Exception {
159
160         // Remove any existing service data
161         configServicesRepository.deleteAll();
162         operationalServicesRepository.deleteAll();
163
164         // Load services data
165         loadServicesData("src/test/resources/service1.json");
166
167         // Add invalid content
168         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
169         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
170                 .andReturn();
171         assertEquals(200, mvcResult.getResponse().getStatus());
172
173         // Add valid content
174         content = readFileContent("src/test/resources/vnf-assign-rpc.json");
175         mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
176                 .andReturn();
177         assertEquals(200, mvcResult.getResponse().getStatus());
178
179     }
180
181
182     private void loadServicesData(String path) throws IOException {
183         ObjectMapper objectMapper = new ObjectMapper();
184         String content = readFileContent(path);
185         GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
186
187         for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
188             ConfigServices newService = new ConfigServices();
189             newService.setSvcInstanceId(service.getServiceInstanceId());
190             newService.setSvcData(objectMapper.writeValueAsString(service.getServiceData()));
191             newService.setServiceStatus(service.getServiceStatus());
192             configServicesRepository.save(newService);
193         }
194     }
195
196     private String readFileContent(String path) throws IOException {
197         String content = new String(Files.readAllBytes(Paths.get(path)));
198         return content;
199     }
200 }