Add RFC 8040 compliant error handler
[ccsdk/apps.git] / ms / sliboot / src / test / java / org / onap / ccsdk / apps / ms / sliboot / RestconfApiControllerTest.java
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.apps.ms.sliboot;
22
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.onap.ccsdk.apps.ms.sliboot.swagger.model.*;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
30 import org.springframework.boot.test.context.SpringBootTest;
31 import org.springframework.http.MediaType;
32 import org.springframework.test.context.junit4.SpringRunner;
33 import org.springframework.test.web.servlet.MockMvc;
34 import org.springframework.test.web.servlet.MvcResult;
35 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
36 import com.fasterxml.jackson.core.JsonProcessingException;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38
39 import java.util.List;
40
41 import static org.junit.Assert.*;
42
43 @RunWith(SpringRunner.class)
44 @SpringBootTest
45 @AutoConfigureMockMvc
46 public class RestconfApiControllerTest {
47
48   private static final Logger log = LoggerFactory.getLogger(RestconfApiControllerTest.class);
49
50   @Autowired
51   private MockMvc mvc;
52
53   @Test
54   public void testHealthcheck() throws Exception {
55     String url = "/operations/SLI-API:healthcheck/";
56
57     MvcResult mvcResult =
58         mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content(""))
59             .andReturn();
60
61     assertEquals(200, mvcResult.getResponse().getStatus());
62   }
63
64   @Test
65   public void testVlbcheck() throws Exception {
66     String url = "/operations/SLI-API:vlbcheck/";
67
68     MvcResult mvcResult =
69         mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content(""))
70             .andReturn();
71
72     assertEquals(200, mvcResult.getResponse().getStatus());
73   }
74
75   @Test
76   public void testExecuteHealthcheck() throws Exception {
77     String url = "/operations/SLI-API:execute-graph/";
78
79     SliApiExecutegraphInput executeGraphData = new SliApiExecutegraphInput();
80     SliApiExecutegraphInputBodyparam executeGraphInput = new SliApiExecutegraphInputBodyparam();
81
82     executeGraphData.setModuleName("sli");
83     executeGraphData.setRpcName("healthcheck");
84     executeGraphData.setMode(SliApiModeEnumeration.SYNC);
85     executeGraphInput.setInput(executeGraphData);
86
87     String jsonString = mapToJson(executeGraphInput);
88     log.error("jsonString is {}", jsonString);
89
90     MvcResult mvcResult =
91         mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content(jsonString))
92             .andReturn();
93
94     assertEquals(200, mvcResult.getResponse().getStatus());
95
96   }
97
98   @Test
99   public void testExecuteMissingDg() throws Exception {
100     String url = "/operations/SLI-API:execute-graph/";
101
102     SliApiExecutegraphInputBodyparam executeGraphInput = new SliApiExecutegraphInputBodyparam();
103     SliApiExecutegraphInput executeGraphData = new SliApiExecutegraphInput();
104
105     executeGraphData.setModuleName("sli");
106     executeGraphData.setRpcName("noSuchRPC");
107     executeGraphData.setMode(SliApiModeEnumeration.SYNC);
108     executeGraphInput.setInput(executeGraphData);
109
110     String jsonString = mapToJson(executeGraphInput);
111
112     log.error("jsonString is {}", jsonString);
113
114     MvcResult mvcResult =
115         mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content(jsonString))
116             .andReturn();
117
118     assertEquals(401, mvcResult.getResponse().getStatus());
119
120   }
121
122   @Test
123   public void testTestResultAdd() throws Exception {
124     String url = "/config/SLI-API:test-results/";
125
126     // Delete any existing content before testing insert
127     MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(url).contentType(MediaType.APPLICATION_JSON)).andReturn();
128     assertEquals(200, mvcResult.getResponse().getStatus());
129
130     mvcResult = mvc.perform(MockMvcRequestBuilders.get(url).contentType(MediaType.APPLICATION_JSON_VALUE)).andReturn();
131
132     assertEquals(404, mvcResult.getResponse().getStatus());
133     log.info("Empty test-results returns error - {}", mvcResult.getResponse().getContentAsString());
134
135     String jsonString = "{\n" +
136             "  \"test-result\" : [\n" +
137             "        {\n" +
138             "           \"test-identifier\" : \"test-1\",\n" +
139             "           \"results\" : [\"test result 1\"]\n" +
140             "        }\n" +
141             "   ]\n" +
142             "}";
143     mvcResult =  mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).accept(MediaType.APPLICATION_JSON_VALUE).content(jsonString))
144             .andReturn();
145     assertEquals(200, mvcResult.getResponse().getStatus());
146
147     mvcResult = mvc.perform(MockMvcRequestBuilders.get(url).contentType(MediaType.APPLICATION_JSON)).andReturn();
148     assertEquals(200, mvcResult.getResponse().getStatus());
149
150
151     ObjectMapper objectMapper = new ObjectMapper();
152     SliApiTestResults testResults = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), SliApiTestResults.class);
153     assertNotNull(testResults);
154     List<SliApiTestresultsTestResult> testResult = testResults.getTestResult();
155     assertNotNull(testResult);
156     assertFalse(testResult.isEmpty());
157     assertEquals(1, testResult.size());
158     SliApiTestresultsTestResult theResult = testResult.get(0);
159     assertEquals("test-1", theResult.getTestIdentifier());
160     assertEquals("test result 1", theResult.getResults().get(0));
161
162   }
163
164   private String mapToJson(Object obj) throws JsonProcessingException {
165     ObjectMapper objectMapper = new ObjectMapper();
166     return objectMapper.writeValueAsString(obj);
167   }
168
169   private SliApiResponseFields respFromJson(String jsonString) throws JsonProcessingException {
170     ObjectMapper objectMapper = new ObjectMapper();
171     return (objectMapper.readValue(jsonString, SliApiResponseFields.class));
172   }
173
174 }