a86924972d9dd9eed3979929acd5300dee003a43
[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.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
11 import org.springframework.boot.test.context.SpringBootTest;
12 import org.springframework.http.MediaType;
13 import org.springframework.test.context.junit4.SpringRunner;
14 import org.springframework.test.web.servlet.MockMvc;
15 import org.springframework.test.web.servlet.MvcResult;
16 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
17 import org.springframework.transaction.annotation.Transactional;
18
19 import java.io.IOException;
20 import java.nio.file.Files;
21 import java.nio.file.Paths;
22
23 import static org.junit.Assert.*;
24
25 @RunWith(SpringRunner.class)
26 @SpringBootTest(classes={GenericResourceMsApp.class})
27 @AutoConfigureMockMvc
28 @Transactional
29 public class OperationsApiControllerTest {
30
31     private final static String PRELOAD_NETWORK_URL = "/operations/GENERIC-RESOURCE-API:preload-network-topology-operation/";
32     private final static String PRELOAD_VFMODULE_URL = "/operations/GENERIC-RESOURCE-API:preload-vf-module-topology-operation/";
33
34
35     @Autowired
36     private MockMvc mvc;
37
38     @Autowired
39     ConfigPreloadDataRepository configPreloadDataRepository;
40
41     @BeforeClass
42     public static void setUp() throws Exception {
43         System.out.println("OperationsApiControllerTest: Setting serviceLogicProperties and serviceLogicDirectory");
44         System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
45         System.setProperty("serviceLogicDirectory", "target/docker-stage/opt/onap/sdnc/svclogic/graphs/generic-resource-api");
46     }
47
48
49     @Test
50     public void operationsGENERICRESOURCEAPIpreloadNetworkTopologyOperationPost() throws Exception {
51
52         // Remove any existing preload data
53         configPreloadDataRepository.deleteAll();
54
55         // Add invalid content
56         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
57         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
58                 .andReturn();
59         assertEquals(403, mvcResult.getResponse().getStatus());
60         assertEquals(0, configPreloadDataRepository.count());
61
62         // Add valid content
63         content = readFileContent("src/test/resources/preload1-rpc-network.json");
64         mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
65                 .andReturn();
66         assertEquals(200, mvcResult.getResponse().getStatus());
67         assertEquals(1, configPreloadDataRepository.count());
68
69     }
70
71     @Test
72     public void operationsGENERICRESOURCEAPIpreloadVfModuleTopologyOperationPost() throws Exception {
73         // Remove any existing preload data
74         configPreloadDataRepository.deleteAll();
75
76         // Add invalid content
77         String content = readFileContent("src/test/resources/preload1-rpc-network.json");
78         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
79                 .andReturn();
80         assertEquals(403, mvcResult.getResponse().getStatus());
81         assertEquals(0, configPreloadDataRepository.count());
82
83         // Add valid content
84         content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
85         mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
86                 .andReturn();
87         assertEquals(200, mvcResult.getResponse().getStatus());
88         assertEquals(1, configPreloadDataRepository.count());
89     }
90
91     private String readFileContent(String path) throws IOException {
92         String content = new String(Files.readAllBytes(Paths.get(path)));
93         return content;
94     }
95 }