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.tdmt.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.tdmt.exception.ExecuteException;
36 import org.onap.cps.tdmt.exception.RecordNotFoundException;
37 import org.onap.cps.tdmt.model.ExecutionRequest;
38 import org.onap.cps.tdmt.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;
46 @RunWith(SpringRunner.class)
47 @WebMvcTest(ExecutionController.class)
48 public class ExecutionControllerTest {
51 private MockMvc mockMvc;
54 private ExecutionBusinessLogic executionBusinessLogic;
56 private ObjectMapper mapper;
57 private String requestJson;
58 private String executePath;
61 * Setup variables before test.
65 public void setup() throws Exception {
66 executePath = "/execute/ran-network/getNbr";
67 final Map<String, String> input = new HashMap<>();
68 input.put("coverageArea", "Zone 1");
69 final ExecutionRequest request = new ExecutionRequest(input);
70 mapper = new ObjectMapper();
71 requestJson = mapper.writeValueAsString(request);
75 public void testExecuteTemplate() throws Exception {
76 final String result = "{\"key\": \"value\"}";
77 Mockito.when(executionBusinessLogic
78 .executeTemplate(ArgumentMatchers.any(), ArgumentMatchers.any(),
79 ArgumentMatchers.any()))
81 mockMvc.perform(post(executePath).contentType(MediaType.APPLICATION_JSON)
82 .characterEncoding("utf-8")
83 .content(requestJson).accept(MediaType.APPLICATION_JSON))
84 .andExpect(status().isOk())
85 .andExpect(content().string(result));
87 Mockito.when(executionBusinessLogic
88 .executeTemplate(ArgumentMatchers.any(), ArgumentMatchers.any(),
89 ArgumentMatchers.any()))
90 .thenThrow(new RecordNotFoundException("Template does not exist"));
91 mockMvc.perform(post(executePath).contentType(MediaType.APPLICATION_JSON)
92 .characterEncoding("utf-8")
93 .content(requestJson).accept(MediaType.APPLICATION_JSON))
94 .andExpect(status().isNotFound());
96 mockMvc.perform(post(executePath).contentType(MediaType.APPLICATION_JSON)
97 .characterEncoding("utf-8")
98 .content("{\"bad\": \"request\"").accept(MediaType.APPLICATION_JSON))
99 .andExpect(status().isBadRequest());
103 public void testExecuteTemplateException() throws Exception {
104 final String responseJson = "{\n"
105 + " \"message\": \"Error while executing template\",\n"
106 + " \"details\": [\"Response from CPS other than 200: 404\"]\n"
109 Mockito.when(executionBusinessLogic
110 .executeTemplate(ArgumentMatchers.any(), ArgumentMatchers.any(),
111 ArgumentMatchers.any()))
112 .thenThrow(new ExecuteException("Response from CPS other than 200: 404"));
113 mockMvc.perform(post(executePath).contentType(MediaType.APPLICATION_JSON)
114 .characterEncoding("utf-8")
115 .content(requestJson).accept(MediaType.APPLICATION_JSON))
116 .andExpect(status().isOk())
117 .andExpect(content().json(responseJson));