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