775be1bc636cd71915b2aa8c87dacd41d268a34b
[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.core.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
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(1, 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
157     private void loadServicesData(String path) throws IOException {
158         ObjectMapper objectMapper = new ObjectMapper();
159         String content = readFileContent(path);
160         GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
161
162         for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
163             ConfigServices newService = new ConfigServices();
164             newService.setSvcInstanceId(service.getServiceInstanceId());
165             newService.setSvcData(objectMapper.writeValueAsString(service.getServiceData()));
166             newService.setServiceStatus(service.getServiceStatus());
167             configServicesRepository.save(newService);
168         }
169     }
170
171     private String readFileContent(String path) throws IOException {
172         String content = new String(Files.readAllBytes(Paths.get(path)));
173         return content;
174     }
175 }