Merge "Introduce JS unit tests into VID"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / controller / RoleGeneratorControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright © 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright 2019 Nokia
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.vid.controller;
24
25 import static org.mockito.BDDMockito.given;
26 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
27 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
28 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
29
30 import org.apache.log4j.BasicConfigurator;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.runners.MockitoJUnitRunner;
36 import org.onap.vid.services.RoleGeneratorService;
37 import org.springframework.http.MediaType;
38 import org.springframework.test.web.servlet.MockMvc;
39 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
40
41 @RunWith(MockitoJUnitRunner.class)
42 public class RoleGeneratorControllerTest {
43
44     private static final String PATH = "/generateRoleScript/{firstRun}";
45
46     private static final String FIRST_JSON = "{key1: val1}";
47     private static final String SECOND_JSON = "{key2: val2}";
48
49     private RoleGeneratorController roleGeneratorController;
50     private MockMvc mockMvc;
51
52     @Mock
53     private RoleGeneratorService service;
54
55     @Before
56     public void setUp() {
57         roleGeneratorController = new RoleGeneratorController(service);
58         BasicConfigurator.configure();
59         mockMvc = MockMvcBuilders.standaloneSetup(roleGeneratorController).build();
60
61         given(service.generateRoleScript(true)).willReturn(FIRST_JSON);
62         given(service.generateRoleScript(false)).willReturn(SECOND_JSON);
63     }
64
65     @Test
66     public void generateRoleScript_shouldReturnJson_whenFirstRun() throws Exception {
67         mockMvc.perform(get(PATH, "true")
68             .contentType(MediaType.APPLICATION_JSON))
69             .andExpect(status().isOk())
70             .andExpect(content().json(FIRST_JSON));
71     }
72
73     @Test
74     public void generateRoleScript_shouldReturnJson_whenNoFirstRun() throws Exception {
75         mockMvc.perform(get(PATH, "false")
76             .contentType(MediaType.APPLICATION_JSON))
77             .andExpect(status().isOk())
78             .andExpect(content().json(SECOND_JSON));
79     }
80 }