ae21cef5fd3b6a6620954d22ffe2c906404f3e1b
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - CCSDK
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.sli.core.sliapi.springboot;
22
23 import static org.junit.Assert.assertEquals;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.onap.ccsdk.sli.core.sliapi.model.ExecuteGraphInput;
27 import org.onap.ccsdk.sli.core.sliapi.model.ExecutegraphinputInput;
28 import org.onap.ccsdk.sli.core.sliapi.model.ResponseFields;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
33 import org.springframework.boot.test.context.SpringBootTest;
34 import org.springframework.http.MediaType;
35 import org.springframework.test.context.junit4.SpringRunner;
36 import org.springframework.test.web.servlet.MockMvc;
37 import org.springframework.test.web.servlet.MvcResult;
38 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
39 import com.fasterxml.jackson.core.JsonProcessingException;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41
42 @RunWith(SpringRunner.class)
43 @SpringBootTest
44 @AutoConfigureMockMvc
45 public class RestconfApiControllerTest {
46
47   private static final Logger log = LoggerFactory.getLogger(RestconfApiControllerTest.class);
48
49   @Autowired
50   private MockMvc mvc;
51
52   @Test
53   public void testHealthcheck() throws Exception {
54     String url = "/restconf/operations/SLI-API:healthcheck";
55
56     MvcResult mvcResult =
57         mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content(""))
58             .andReturn();
59
60     assertEquals(200, mvcResult.getResponse().getStatus());
61   }
62
63   @Test
64   public void testVlbcheck() throws Exception {
65     String url = "/restconf/operations/SLI-API:vlbcheck";
66
67     MvcResult mvcResult =
68         mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content(""))
69             .andReturn();
70
71     assertEquals(200, mvcResult.getResponse().getStatus());
72   }
73
74   @Test
75   public void testExecuteHealthcheck() throws Exception {
76     String url = "/restconf/operations/SLI-API:execute-graph";
77
78     ExecuteGraphInput executeGraphInput = new ExecuteGraphInput();
79     ExecutegraphinputInput executeGraphData = new ExecutegraphinputInput();
80
81     executeGraphData.setModuleName("sli");
82     executeGraphData.setRpcName("healthcheck");
83     executeGraphData.setMode("sync");
84     executeGraphInput.setInput(executeGraphData);
85
86     String jsonString = mapToJson(executeGraphInput);
87     log.error("jsonString is {}", jsonString);
88
89     MvcResult mvcResult =
90         mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content(jsonString))
91             .andReturn();
92
93     assertEquals(200, mvcResult.getResponse().getStatus());
94
95   }
96
97   @Test
98   public void testExecuteMissingDg() throws Exception {
99     String url = "/restconf/operations/SLI-API:execute-graph";
100
101     ExecuteGraphInput executeGraphInput = new ExecuteGraphInput();
102     ExecutegraphinputInput executeGraphData = new ExecutegraphinputInput();
103
104     executeGraphData.setModuleName("sli");
105     executeGraphData.setRpcName("noSuchRPC");
106     executeGraphData.setMode("sync");
107     executeGraphInput.setInput(executeGraphData);
108
109     String jsonString = mapToJson(executeGraphInput);
110
111     log.error("jsonString is {}", jsonString);
112
113     MvcResult mvcResult =
114         mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content(jsonString))
115             .andReturn();
116
117     assertEquals(401, mvcResult.getResponse().getStatus());
118
119   }
120
121   @Test
122   public void testTestResultAdd() throws Exception {
123     String url = "/restconf/config/SLI-API:test-results";
124
125     MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(url)).andReturn();
126
127     assertEquals(200, mvcResult.getResponse().getStatus());
128
129     // Delete any existing content before testing insert
130     mvcResult = mvc.perform(MockMvcRequestBuilders.delete(url)).andReturn();
131     assertEquals(200, mvcResult.getResponse().getStatus());
132
133     String jsonString = "{\n" +
134             "  \"test-results\" : [\n" +
135             "        {\n" +
136             "           \"test-identifier\" : \"test-1\",\n" +
137             "           \"results\" : [\"test result 1\"]\n" +
138             "        }\n" +
139             "   ]\n" +
140             "}";
141
142     mvcResult =  mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content(jsonString))
143             .andReturn();
144
145     assertEquals(200, mvcResult.getResponse().getStatus());
146
147     mvcResult = mvc.perform(MockMvcRequestBuilders.get(url)).andReturn();
148
149     assertEquals(200, mvcResult.getResponse().getStatus());
150     assertEquals(jsonString.replaceAll("\\s+",""), mvcResult.getResponse().getContentAsString().replaceAll("\\s+",""));
151   }
152
153   private String mapToJson(Object obj) throws JsonProcessingException {
154     ObjectMapper objectMapper = new ObjectMapper();
155     return objectMapper.writeValueAsString(obj);
156   }
157
158   private ResponseFields respFromJson(String jsonString) throws JsonProcessingException {
159     ObjectMapper objectMapper = new ObjectMapper();
160     return (objectMapper.readValue(jsonString, ResponseFields.class));
161   }
162 }