081199c466fda13a85adda6b3b8fad1b8ffd552f
[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.ConfigServicesRepository;
9 import org.onap.sdnc.apps.ms.gra.data.OperationalServicesRepository;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
12 import org.springframework.boot.test.context.SpringBootTest;
13 import org.springframework.http.MediaType;
14 import org.springframework.test.context.junit4.SpringRunner;
15 import org.springframework.test.web.servlet.MockMvc;
16 import org.springframework.test.web.servlet.MvcResult;
17 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
18 import org.springframework.transaction.annotation.Transactional;
19
20 import java.io.IOException;
21 import java.nio.file.Files;
22 import java.nio.file.Paths;
23
24 import static org.junit.Assert.*;
25
26 @RunWith(SpringRunner.class)
27 @SpringBootTest(classes={GenericResourceMsApp.class})
28 @AutoConfigureMockMvc
29 @Transactional
30 public class OperationsApiControllerTest {
31
32     private final static String PRELOAD_NETWORK_URL = "/operations/GENERIC-RESOURCE-API:preload-network-topology-operation/";
33     private final static String PRELOAD_VFMODULE_URL = "/operations/GENERIC-RESOURCE-API:preload-vf-module-topology-operation/";
34     private final static String SERVICE_TOPOLOGY_URL = "/operations/GENERIC-RESOURCE-API:service-topology-operation/";
35
36
37     @Autowired
38     private MockMvc mvc;
39
40     @Autowired
41     ConfigPreloadDataRepository configPreloadDataRepository;
42
43     @Autowired
44     ConfigServicesRepository configServicesRepository;
45
46     @Autowired
47     OperationalServicesRepository operationalServicesRepository;
48
49     @BeforeClass
50     public static void setUp() throws Exception {
51         System.out.println("OperationsApiControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
52         System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
53         System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
54         System.setProperty("sdnc.config.dir", "src/test/resources");
55    
56     }
57
58
59     @Test
60     public void operationsGENERICRESOURCEAPIpreloadNetworkTopologyOperationPost() throws Exception {
61
62         // Remove any existing preload data
63         configPreloadDataRepository.deleteAll();
64
65         // Add invalid content
66         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
67         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
68                 .andReturn();
69         assertEquals(403, mvcResult.getResponse().getStatus());
70         assertEquals(0, configPreloadDataRepository.count());
71
72         // Add valid content
73         content = readFileContent("src/test/resources/preload1-rpc-network.json");
74         mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
75                 .andReturn();
76         assertEquals(200, mvcResult.getResponse().getStatus());
77         assertEquals(1, configPreloadDataRepository.count());
78
79     }
80
81     @Test
82     public void operationsGENERICRESOURCEAPIpreloadVfModuleTopologyOperationPost() throws Exception {
83         // Remove any existing preload data
84         configPreloadDataRepository.deleteAll();
85
86         // Add invalid content
87         String content = readFileContent("src/test/resources/preload1-rpc-network.json");
88         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
89                 .andReturn();
90         assertEquals(403, mvcResult.getResponse().getStatus());
91         assertEquals(0, configPreloadDataRepository.count());
92
93         // Add valid content
94         content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
95         mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
96                 .andReturn();
97         assertEquals(200, mvcResult.getResponse().getStatus());
98         assertEquals(1, configPreloadDataRepository.count());
99     }
100
101     @Test
102     public void operationsGENERICRESOURCEAPIserviceTopologyOperationAssignPost() throws Exception {
103
104         // Remove any existing service data
105         configServicesRepository.deleteAll();
106         operationalServicesRepository.deleteAll();
107
108         // Add invalid content
109         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
110         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
111                 .andReturn();
112         assertEquals(200, mvcResult.getResponse().getStatus());
113         assertEquals(0, configServicesRepository.count());
114         assertEquals(0, operationalServicesRepository.count());
115
116         // Add valid content
117         content = readFileContent("src/test/resources/service-assign-rpc.json");
118         mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
119                 .andReturn();
120         assertEquals(200, mvcResult.getResponse().getStatus());
121         assertEquals(1, configServicesRepository.count());
122         assertEquals(1, operationalServicesRepository.count());
123
124     }
125
126     private String readFileContent(String path) throws IOException {
127         String content = new String(Files.readAllBytes(Paths.get(path)));
128         return content;
129     }
130 }