summaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java
diff options
context:
space:
mode:
authorJoanna Jeremicz <joanna.jeremicz@nokia.com>2019-02-26 12:31:43 +0100
committerJoanna Jeremicz <joanna.jeremicz@nokia.com>2019-04-19 13:03:50 +0200
commitefef025b64cc03adf5b429170bcd39e4540a43a7 (patch)
tree63da705c076f4bec77c7845254a9e41d20cff6b4 /vid-app-common/src/test/java
parente6571c6df02776c335b156b0fb44e31d723ffa06 (diff)
Rewrite LoggerControllerTests
Add JUnit tests with minor code refactor Issue-ID: VID-384 Change-Id: Ic3bee8e1f6ee58a10053bb0ad2d006912965da1a Signed-off-by: Joanna Jeremicz <joanna.jeremicz@nokia.com>
Diffstat (limited to 'vid-app-common/src/test/java')
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/controller/LogfilePathCreatorTest.java67
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/controller/LoggerControllerTest.java112
2 files changed, 144 insertions, 35 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/controller/LogfilePathCreatorTest.java b/vid-app-common/src/test/java/org/onap/vid/controller/LogfilePathCreatorTest.java
new file mode 100644
index 000000000..a6a88a0a4
--- /dev/null
+++ b/vid-app-common/src/test/java/org/onap/vid/controller/LogfilePathCreatorTest.java
@@ -0,0 +1,67 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * VID
+ * ================================================================================
+ * Copyright (C) 2019 Nokia. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+
+package org.onap.vid.controller;
+
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+
+import javax.ws.rs.InternalServerErrorException;
+import org.junit.Before;
+import org.junit.Test;
+
+public class LogfilePathCreatorTest {
+
+ private static final String AUDIT = "audit";
+ private static final String ERROR = "error";
+ private static final String METRICS = "metrics";
+
+ private LogfilePathCreator creator;
+
+ @Before
+ public void setUp() {
+ creator = new LogfilePathCreator();
+ }
+
+ @Test
+ public void shouldThrowInternalServerErrorException_whenLogfileDoesntExist() {
+ String loggerName = "notExisting";
+ assertThatExceptionOfType(InternalServerErrorException.class)
+ .isThrownBy(() -> creator.getLogfilePath(loggerName))
+ .withMessage(String.format("logfile for %s not found", loggerName));
+ }
+
+ @Test
+ public void shouldReturnAuditPath_whenAuditNameIsGiven() {
+ assertThat(creator.getLogfilePath(AUDIT)).isEqualTo("logs/EELF/audit.log");
+ }
+
+ @Test
+ public void shouldReturnErrorPath_whenErrorNameIsGiven() {
+ assertThat(creator.getLogfilePath(ERROR)).isEqualTo("logs/EELF/error.log");
+ }
+
+ @Test
+ public void shouldReturnMetricsPath_whenMetricsNameIsGiven() {
+ assertThat(creator.getLogfilePath(METRICS)).isEqualTo("logs/EELF/metrics.log");
+ }
+} \ No newline at end of file
diff --git a/vid-app-common/src/test/java/org/onap/vid/controller/LoggerControllerTest.java b/vid-app-common/src/test/java/org/onap/vid/controller/LoggerControllerTest.java
index 1602a3197..fdc0f44d1 100644
--- a/vid-app-common/src/test/java/org/onap/vid/controller/LoggerControllerTest.java
+++ b/vid-app-common/src/test/java/org/onap/vid/controller/LoggerControllerTest.java
@@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,50 +20,92 @@
package org.onap.vid.controller;
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.BDDMockito.given;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+import com.google.common.collect.ImmutableList;
+import java.util.List;
+import org.apache.log4j.BasicConfigurator;
+import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.vid.roles.EcompRole;
+import org.onap.vid.roles.Role;
+import org.onap.vid.roles.RoleProvider;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
-import javax.ws.rs.NotAuthorizedException;
+@RunWith(MockitoJUnitRunner.class)
+public class LoggerControllerTest {
+ private static final String ERROR_URL = "/logger/error";
+ private static final String AUDIT_URL = "/logger/audit";
+ private static final String METRICS_URL = "/logger/metrics";
+ private static final String VALID_LOG_PATH = "src/test/resources/loggerFiles/validLog.log";
+ private static final String EMPTY_LOG_PATH = "src/test/resources/loggerFiles/emptyLog.log";
-public class LoggerControllerTest {
+ private MockMvc mockMvc;
+ private LoggerController loggerController;
- private LoggerController createTestSubject() {
- return new LoggerController();
+ @Mock
+ private RoleProvider provider;
+ @Mock
+ private LogfilePathCreator creator;
+
+ @Before
+ public void setUp() {
+ loggerController = new LoggerController(provider, creator);
+ BasicConfigurator.configure();
+ mockMvc = MockMvcBuilders.standaloneSetup(loggerController).build();
}
- /*@Test
- public void testGetLog() throws Exception {
- LoggerController testSubject;
- String loggerName = "";
- HttpServletRequest request = null;
- Integer limit = 0;
- String result;
+ @Test
+ public void shouldThrowNotAuthorizedException_whenUserIsNotAuthorizedToGetLogs() throws Exception {
+ List<Role> list = ImmutableList.of(new Role(EcompRole.READ, "subName1", "servType1", "tenant1"));
+
+ given(provider.getUserRoles(argThat(req -> req.getRequestedSessionId().equals("id1")))).willReturn(list);
+ given(provider.userPermissionIsReadLogs(list)).willReturn(false);
- // default test
- testSubject = createTestSubject();
- result = testSubject.getLog(loggerName, request, limit);
- }*/
+ mockMvc.perform(get(ERROR_URL)
+ .with(req -> {req.setRequestedSessionId("id1");
+ return req;}))
+ .andExpect(content().string("UNAUTHORIZED"))
+ .andExpect(status().isUnauthorized());
+ }
-
@Test
- public void testNotAuthorizedHandler() throws Exception {
- LoggerController testSubject;
- NotAuthorizedException e = null;
- String result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.notAuthorizedHandler(e);
+ public void shouldReturnLastAndOneBeforeLogLines_whenLimitIs2() throws Exception {
+ List<Role> list = ImmutableList.of(new Role(EcompRole.READ, "subName1", "servType1", "tenant1"));
+
+ given(provider.getUserRoles(argThat(req -> req.getRequestedSessionId().equals("id1")))).willReturn(list);
+ given(provider.userPermissionIsReadLogs(list)).willReturn(true);
+ given(creator.getLogfilePath("audit")).willReturn(VALID_LOG_PATH);
+
+ mockMvc.perform(get(AUDIT_URL)
+ .with(req -> {req.setRequestedSessionId("id1");
+ return req;})
+ .param("limit", "2"))
+ .andExpect(content().string("and the third line\nthe second line"))
+ .andExpect(status().isOk());
}
- /*@Test
- public void testIoExceptionHandler() throws Exception {
- LoggerController testSubject;
- Exception e = null;
- ExceptionResponse result;
+ @Test
+ public void shouldReturnEmptyString_whenLogFileIsEmpty() throws Exception {
+ List<Role> list = ImmutableList.of(new Role(EcompRole.READ, "subName1", "servType1", "tenant1"));
+
+ given(provider.getUserRoles(argThat(req -> req.getRequestedSessionId().equals("id1")))).willReturn(list);
+ given(provider.userPermissionIsReadLogs(list)).willReturn(true);
+ given(creator.getLogfilePath("metrics")).willReturn(EMPTY_LOG_PATH);
- // default test
- testSubject = createTestSubject();
- result = testSubject.ioExceptionHandler(e);
- }*/
+ mockMvc.perform(get(METRICS_URL)
+ .with(req -> {req.setRequestedSessionId("id1");
+ return req;}))
+ .andExpect(content().string(""))
+ .andExpect(status().isOk());
+ }
}