Merge "Fix mod ui build issues"
[dcaegen2/platform.git] / mod2 / auth-service / src / test / java / org / onap / dcaegen2 / platform / mod / web / RoleControllerTest.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
5  *  *  ================================================================================
6  *  *  Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
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
23 package org.onap.dcaegen2.platform.mod.web;
24
25 import org.hamcrest.Matchers;
26 import org.junit.Assert;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.mockito.Mock;
30 import org.mockito.Mockito;
31 import org.onap.dcaegen2.platform.mod.controllers.RoleController;
32 import org.onap.dcaegen2.platform.mod.models.ModUser;
33 import org.onap.dcaegen2.platform.mod.models.Role;
34 import org.onap.dcaegen2.platform.mod.repositories.RoleRepository;
35 import org.onap.dcaegen2.platform.mod.security.jwt.AuthEntryPointJwt;
36 import org.onap.dcaegen2.platform.mod.security.jwt.JwtUtils;
37 import org.onap.dcaegen2.platform.mod.security.services.UserDetailsServiceImpl;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
40 import org.springframework.boot.test.mock.mockito.MockBean;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.MediaType;
43 import org.springframework.security.core.Authentication;
44 import org.springframework.test.web.servlet.MockMvc;
45 import org.springframework.test.web.servlet.MvcResult;
46 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
47 import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
48 import org.springframework.web.bind.annotation.GetMapping;
49 import org.springframework.web.bind.annotation.ResponseStatus;
50
51 import java.util.Arrays;
52 import java.util.HashSet;
53 import java.util.List;
54 import java.util.Set;
55 import java.util.stream.Collectors;
56
57 import static org.hamcrest.core.IsNull.notNullValue;
58 import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.asJsonString;
59 import static org.onap.dcaegen2.platform.mod.objectmothers.RoleObjectMother.getRoles;
60 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
61 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
62 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
63 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
64
65 /**
66  * @author
67  * @date 09/22/2020
68  * Mock Test cases for RoleController
69  */
70 @WebMvcTest(RoleController.class)
71 public class RoleControllerTest {
72
73     @Autowired
74     private MockMvc mockMvc;
75
76     @MockBean
77     RoleRepository roleRepository;
78
79     @MockBean
80     UserDetailsServiceImpl userDetailsService;
81
82     @MockBean
83     AuthEntryPointJwt authEntryPointJwt;
84
85     @MockBean
86     JwtUtils jwtUtils;
87
88     @Mock
89     Authentication authentication;
90
91     @BeforeEach
92     void setUp() {
93     }
94
95
96     @Test
97     void test_getRoles() throws Exception {
98
99         Mockito.when(roleRepository.findAll()).thenReturn(getRoles());
100
101         MvcResult result =  mockMvc.perform(get("/api/roles")
102             .contentType(MediaType.APPLICATION_JSON))
103             .andExpect(status().isOk()).andReturn();
104
105         Assert.assertNotNull(result.getResponse().getContentAsString());
106
107     }
108
109 }