Merge "Improve mod/runtimeapi code coverage"
[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.service.GraphServiceImpl;
33 import org.onap.dcae.runtime.web.service.BlueprintInventory;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
39 import org.springframework.boot.test.context.SpringBootTest;
40 import org.springframework.boot.test.mock.mockito.MockBean;
41 import org.springframework.http.MediaType;
42 import org.springframework.test.context.junit4.SpringRunner;
43 import org.springframework.test.web.servlet.MockMvc;
44
45 import static org.mockito.Mockito.when;
46 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
47 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
48 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
49 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
50 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
51
52 @RunWith(SpringRunner.class)
53 @SpringBootTest
54 @AutoConfigureMockMvc
55 public class TestFlowGraphController {
56
57     @Autowired
58     private MockMvc mockMvc;
59
60     @Autowired
61     private BlueprintInventory blueprintInventory;
62
63     @Autowired
64     private GraphServiceImpl graphService;
65
66     private DistributeGraphRequest distributeGraphRequest;
67     private GraphRequest graphRequest;
68
69     @Before
70     public void setUp() throws Exception{
71         graphRequest = new GraphRequest();
72         graphRequest.setId("1234");
73         graphRequest.setName("nifi-main");
74         graphRequest.setDescription("mock graph");
75         distributeGraphRequest = new DistributeGraphRequest();
76         ClientMocking inv = new ClientMocking()
77                 .on("POST /ccsdk-app/api-if", "\"OK\"")
78                 .applyTo(blueprintInventory);
79     }
80
81     @Test
82     public void testInitializeGraph() throws Exception{
83         graphRequest.setMain(true);
84         mockMvc.perform(get("/api/graph/main"))
85                 .andExpect(status().isNotFound());
86         mockMvc.perform(post("/api/graph/main")
87                         .contentType(MediaType.APPLICATION_JSON)
88                         .content(TestUtils.convertObjectToJsonBytes(graphRequest)))
89                         .andExpect(status().isCreated());
90         mockMvc.perform(post("/api/graph/main")
91                         .contentType(MediaType.APPLICATION_JSON)
92                         .content(TestUtils.convertObjectToJsonBytes(graphRequest)))
93                         .andExpect(status().isConflict());
94         mockMvc.perform(get("/api/graph/main"))
95                 .andExpect(status().isOk())
96                 .andExpect(jsonPath("id").value("1234"))
97                 .andExpect(jsonPath("name").value("nifi-main"));
98         distributeGraphRequest.setActions(Helper.getAddNodesWithEdge());
99         mockMvc.perform(post("/api/graph/" + graphRequest.getId() + "/distribute")
100                         .contentType(MediaType.APPLICATION_JSON)
101                         .content(TestUtils.convertObjectToJsonBytes(distributeGraphRequest)))
102                         .andExpect(status().isOk());
103         mockMvc.perform(delete("/api/graph/main"))
104                         .andExpect(status().isOk());
105         mockMvc.perform(delete("/api/graph/main"))
106                         .andExpect(status().isNotFound());
107     }
108 }