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