2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 Wipro Limited.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.cps.tbdmt.rest;
23 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
24 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
25 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import java.util.HashMap;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.ArgumentMatchers;
34 import org.mockito.Mockito;
35 import org.onap.cps.tbdmt.exception.ExecuteException;
36 import org.onap.cps.tbdmt.exception.TemplateNotFoundException;
37 import org.onap.cps.tbdmt.model.ExecutionRequest;
38 import org.onap.cps.tbdmt.service.ExecutionBusinessLogic;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
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;
47 @RunWith(SpringRunner.class)
48 @WebMvcTest(ExecutionController.class)
49 public class ExecutionControllerTest {
52 private MockMvc mockMvc;
55 private ExecutionBusinessLogic executionBusinessLogic;
57 private ObjectMapper mapper;
58 private String requestJson;
59 private String executePath;
62 * Setup variables before test.
66 public void setup() throws Exception {
67 executePath = "/execute/ran-network/getNbr";
68 final Map<String, String> input = new HashMap<>();
69 input.put("coverageArea", "Zone 1");
70 final ExecutionRequest request = new ExecutionRequest(input);
71 mapper = new ObjectMapper();
72 requestJson = mapper.writeValueAsString(request);
76 public void testExecuteTemplate() throws Exception {
77 final String result = "{\"key\": \"value\"}";
78 Mockito.when(executionBusinessLogic
79 .executeTemplate(ArgumentMatchers.any(), ArgumentMatchers.any(),
80 ArgumentMatchers.any()))
82 mockMvc.perform(post(executePath).contentType(MediaType.APPLICATION_JSON)
83 .characterEncoding("utf-8")
84 .content(requestJson).accept(MediaType.APPLICATION_JSON))
85 .andExpect(status().isOk())
86 .andExpect(content().string(result));
88 Mockito.when(executionBusinessLogic
89 .executeTemplate(ArgumentMatchers.any(), ArgumentMatchers.any(),
90 ArgumentMatchers.any()))
91 .thenThrow(new TemplateNotFoundException("Template does not exist"));
92 mockMvc.perform(post(executePath).contentType(MediaType.APPLICATION_JSON)
93 .characterEncoding("utf-8")
94 .content(requestJson).accept(MediaType.APPLICATION_JSON))
95 .andExpect(status().isNotFound());
97 mockMvc.perform(post(executePath).contentType(MediaType.APPLICATION_JSON)
98 .characterEncoding("utf-8")
99 .content("{\"bad\": \"request\"").accept(MediaType.APPLICATION_JSON))
100 .andExpect(status().isBadRequest());
104 public void testExecuteTemplateException() throws Exception {
105 final String responseJson = "{\n"
106 + " \"message\": \"Error while executing template\",\n"
107 + " \"details\": [\"Response from CPS other than 200: 404\"]\n"
110 Mockito.when(executionBusinessLogic
111 .executeTemplate(ArgumentMatchers.any(), ArgumentMatchers.any(),
112 ArgumentMatchers.any()))
113 .thenThrow(new ExecuteException("Response from CPS other than 200: 404"));
114 mockMvc.perform(post(executePath).contentType(MediaType.APPLICATION_JSON)
115 .characterEncoding("utf-8")
116 .content(requestJson).accept(MediaType.APPLICATION_JSON))
117 .andExpect(status().isOk())
118 .andExpect(content().json(responseJson));