2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
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.vid.controller;
23 import static org.mockito.ArgumentMatchers.argThat;
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;
29 import com.google.common.collect.ImmutableList;
30 import java.util.List;
31 import org.apache.log4j.BasicConfigurator;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.onap.vid.roles.EcompRole;
38 import org.onap.vid.roles.Role;
39 import org.onap.vid.roles.RoleProvider;
40 import org.springframework.test.web.servlet.MockMvc;
41 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
43 @RunWith(MockitoJUnitRunner.class)
44 public class LoggerControllerTest {
46 private static final String ERROR_URL = "/logger/error";
47 private static final String AUDIT_URL = "/logger/audit";
48 private static final String METRICS_URL = "/logger/metrics";
49 private static final String VALID_LOG_PATH = "src/test/resources/loggerFiles/validLog.log";
50 private static final String EMPTY_LOG_PATH = "src/test/resources/loggerFiles/emptyLog.log";
52 private MockMvc mockMvc;
53 private LoggerController loggerController;
56 private RoleProvider provider;
58 private LogfilePathCreator creator;
62 loggerController = new LoggerController(provider, creator);
63 BasicConfigurator.configure();
64 mockMvc = MockMvcBuilders.standaloneSetup(loggerController).build();
68 public void shouldThrowNotAuthorizedException_whenUserIsNotAuthorizedToGetLogs() throws Exception {
69 List<Role> list = ImmutableList.of(new Role(EcompRole.READ, "subName1", "servType1", "tenant1"));
71 given(provider.getUserRoles(argThat(req -> req.getRequestedSessionId().equals("id1")))).willReturn(list);
72 given(provider.userPermissionIsReadLogs(list)).willReturn(false);
74 mockMvc.perform(get(ERROR_URL)
75 .with(req -> {req.setRequestedSessionId("id1");
77 .andExpect(content().string("UNAUTHORIZED"))
78 .andExpect(status().isUnauthorized());
82 public void shouldReturnLastAndOneBeforeLogLines_whenLimitIs2() throws Exception {
83 List<Role> list = ImmutableList.of(new Role(EcompRole.READ, "subName1", "servType1", "tenant1"));
85 given(provider.getUserRoles(argThat(req -> req.getRequestedSessionId().equals("id1")))).willReturn(list);
86 given(provider.userPermissionIsReadLogs(list)).willReturn(true);
87 given(creator.getLogfilePath("audit")).willReturn(VALID_LOG_PATH);
89 mockMvc.perform(get(AUDIT_URL)
90 .with(req -> {req.setRequestedSessionId("id1");
93 .andExpect(content().string("and the third line\nthe second line"))
94 .andExpect(status().isOk());
98 public void shouldReturnEmptyString_whenLogFileIsEmpty() throws Exception {
99 List<Role> list = ImmutableList.of(new Role(EcompRole.READ, "subName1", "servType1", "tenant1"));
101 given(provider.getUserRoles(argThat(req -> req.getRequestedSessionId().equals("id1")))).willReturn(list);
102 given(provider.userPermissionIsReadLogs(list)).willReturn(true);
103 given(creator.getLogfilePath("metrics")).willReturn(EMPTY_LOG_PATH);
105 mockMvc.perform(get(METRICS_URL)
106 .with(req -> {req.setRequestedSessionId("id1");
108 .andExpect(content().string(""))
109 .andExpect(status().isOk());
113 public void shouldReturnEmptyString_whenDebugLogFileIsEmpty() throws Exception {
114 List<Role> list = ImmutableList.of(new Role(EcompRole.READ, "subName1", "servType1", "tenant1"));
116 given(provider.getUserRoles(argThat(req -> req.getRequestedSessionId().equals("id1")))).willReturn(list);
117 given(provider.userPermissionIsReadLogs(list)).willReturn(true);
118 given(creator.getLogfilePath("debug")).willReturn(EMPTY_LOG_PATH);
120 mockMvc.perform(get("/logger/debug")
121 .with(req -> {req.setRequestedSessionId("id1");
123 .andExpect(content().string(""))
124 .andExpect(status().isOk());