3 * * ============LICENSE_START=======================================================
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
12 * * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.dcaegen2.platform.mod.web;
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;
51 import java.util.Arrays;
52 import java.util.HashSet;
53 import java.util.List;
55 import java.util.stream.Collectors;
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;
68 * Mock Test cases for RoleController
70 @WebMvcTest(RoleController.class)
71 public class RoleControllerTest {
74 private MockMvc mockMvc;
77 RoleRepository roleRepository;
80 UserDetailsServiceImpl userDetailsService;
83 AuthEntryPointJwt authEntryPointJwt;
89 Authentication authentication;
97 void test_getRoles() throws Exception {
99 Mockito.when(roleRepository.findAll()).thenReturn(getRoles());
101 MvcResult result = mockMvc.perform(get("/api/roles")
102 .contentType(MediaType.APPLICATION_JSON))
103 .andExpect(status().isOk()).andReturn();
105 Assert.assertNotNull(result.getResponse().getContentAsString());