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