Remove security vulnerabilities in runtime-api
[dcaegen2/platform.git] / mod / runtimeapi / runtime-web / src / test / java / org / onap / dcae / runtime / web / controllers / TestFlowGraphController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18 package org.onap.dcae.runtime.web.controllers;
19
20 import java.util.HashMap;
21 import java.util.LinkedList;
22
23 import org.onap.dcae.runtime.core.Edge;
24 import org.onap.dcae.runtime.core.FlowGraph;
25 import org.onap.dcae.runtime.core.Node;
26 import org.onap.dcae.runtime.web.ClientMocking;
27 import org.onap.dcae.runtime.web.Helper;
28 import org.onap.dcae.runtime.web.TestUtils;
29 import org.onap.dcae.runtime.web.models.Action;
30 import org.onap.dcae.runtime.web.models.DistributeGraphRequest;
31 import org.onap.dcae.runtime.web.models.GraphRequest;
32 import org.onap.dcae.runtime.web.RuntimeapiApplication;
33 import org.onap.dcae.runtime.web.service.GraphServiceImpl;
34 import org.onap.dcae.runtime.web.service.BlueprintInventory;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
40 import org.springframework.boot.test.context.SpringBootTest;
41 import org.springframework.boot.test.mock.mockito.MockBean;
42 import org.springframework.http.MediaType;
43 import org.springframework.test.context.junit4.SpringRunner;
44 import org.springframework.test.web.servlet.MockMvc;
45
46 import static org.mockito.Mockito.when;
47 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
48 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
49 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
50 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
51 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
52
53 @RunWith(SpringRunner.class)
54 @SpringBootTest(classes=RuntimeapiApplication.class)
55 @AutoConfigureMockMvc
56 public class TestFlowGraphController {
57
58     @Autowired
59     private MockMvc mockMvc;
60
61     @Autowired
62     private BlueprintInventory blueprintInventory;
63
64     @Autowired
65     private GraphServiceImpl graphService;
66
67     private DistributeGraphRequest distributeGraphRequest;
68     private GraphRequest graphRequest;
69
70     @Before
71     public void setUp() throws Exception{
72         graphRequest = new GraphRequest();
73         graphRequest.setId("1234");
74         graphRequest.setName("nifi-main");
75         graphRequest.setDescription("mock graph");
76         distributeGraphRequest = new DistributeGraphRequest();
77         ClientMocking inv = new ClientMocking()
78                 .on("POST /ccsdk-app/api-if", "\"OK\"")
79                 .applyTo(blueprintInventory);
80     }
81
82     @Test
83     public void testSwaggerUi() throws Exception {
84         mockMvc.perform(get("/swagger-ui/index.html"))
85                 .andExpect(status().isOk());
86     }
87
88     @Test
89     public void testInitializeGraph() throws Exception{
90         graphRequest.setMain(true);
91         mockMvc.perform(get("/api/graph/main"))
92                 .andExpect(status().isNotFound());
93         mockMvc.perform(post("/api/graph/main")
94                         .contentType(MediaType.APPLICATION_JSON)
95                         .content(TestUtils.convertObjectToJsonBytes(graphRequest)))
96                         .andExpect(status().isCreated());
97         mockMvc.perform(post("/api/graph/main")
98                         .contentType(MediaType.APPLICATION_JSON)
99                         .content(TestUtils.convertObjectToJsonBytes(graphRequest)))
100                         .andExpect(status().isConflict());
101         mockMvc.perform(get("/api/graph/main"))
102                 .andExpect(status().isOk())
103                 .andExpect(jsonPath("id").value("1234"))
104                 .andExpect(jsonPath("name").value("nifi-main"));
105         distributeGraphRequest.setActions(Helper.getAddNodesWithEdge());
106         mockMvc.perform(post("/api/graph/" + graphRequest.getId() + "/distribute")
107                         .contentType(MediaType.APPLICATION_JSON)
108                         .content(TestUtils.convertObjectToJsonBytes(distributeGraphRequest)))
109                         .andExpect(status().isOk());
110         mockMvc.perform(delete("/api/graph/main"))
111                         .andExpect(status().isOk());
112         mockMvc.perform(delete("/api/graph/main"))
113                         .andExpect(status().isNotFound());
114     }
115 }