b630bd0989cf1b777090fc596fc88a3ff810bf2e
[dcaegen2/collectors/datafile.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dcaegen2.collectors.datafile.controllers;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Mockito.when;
28
29 import ch.qos.logback.classic.spi.ILoggingEvent;
30 import ch.qos.logback.core.read.ListAppender;
31 import org.apache.commons.lang3.StringUtils;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.mockito.Mock;
36 import org.mockito.junit.jupiter.MockitoExtension;
37 import org.onap.dcaegen2.collectors.datafile.configuration.SchedulerConfig;
38 import org.onap.dcaegen2.collectors.datafile.utils.LoggingUtils;
39 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables;
40 import org.slf4j.MDC;
41 import org.springframework.http.HttpHeaders;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.ResponseEntity;
44 import reactor.core.publisher.Mono;
45
46 @ExtendWith(MockitoExtension.class)
47 public class ScheduleControllerTest {
48     @Mock
49     private SchedulerConfig schedulerConfigMock;
50
51     private ScheduleController scheduleControllerUnderTest;
52
53     @BeforeEach
54     public void setup() {
55         scheduleControllerUnderTest = new ScheduleController(schedulerConfigMock);
56     }
57
58     @Test
59     public void startTasksSuccess() {
60         when(schedulerConfigMock.tryToStartTask()).thenReturn(Boolean.TRUE);
61
62         HttpHeaders httpHeaders = new HttpHeaders();
63
64         ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ScheduleController.class);
65         Mono<ResponseEntity<String>> response = scheduleControllerUnderTest.startTasks(httpHeaders);
66
67         validateLogging(logAppender, "Start request");
68
69         String body = response.block().getBody();
70         assertTrue(body.startsWith("Datafile Service has been started!"));
71
72         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.REQUEST_ID)));
73         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.INVOCATION_ID)));
74     }
75
76     @Test
77     public void startTasksFail() {
78         when(schedulerConfigMock.tryToStartTask()).thenReturn(Boolean.FALSE);
79
80         HttpHeaders httpHeaders = new HttpHeaders();
81         // The following headers are set to create branch coverage in MappedDiagnosticContext:initializeTraceContext().
82         httpHeaders.set(MdcVariables.X_ONAP_REQUEST_ID, "Onap request ID");
83         httpHeaders.set(MdcVariables.X_INVOCATION_ID, "Invocation ID");
84
85         ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ScheduleController.class);
86         Mono<ResponseEntity<String>> response = scheduleControllerUnderTest.startTasks(httpHeaders);
87
88         validateLogging(logAppender, "Start request");
89
90         String body = response.block().getBody();
91         assertTrue(body.startsWith("Datafile Service is still running!"));
92
93         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.REQUEST_ID)));
94         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.INVOCATION_ID)));
95     }
96
97     @Test
98     public void stopTaskSuccess() {
99         when(schedulerConfigMock.getResponseFromCancellationOfTasks()).thenReturn(
100             Mono.just(new ResponseEntity<>("Datafile Service has already been stopped!", HttpStatus.CREATED)));
101
102         HttpHeaders httpHeaders = new HttpHeaders();
103
104         ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ScheduleController.class);
105         Mono<ResponseEntity<String>> actualResponse = scheduleControllerUnderTest.stopTask(httpHeaders);
106
107         validateLogging(logAppender, "Stop request");
108
109         String body = actualResponse.block().getBody();
110         assertTrue(body.startsWith("Datafile Service has already been stopped!"));
111
112         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.REQUEST_ID)));
113         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.INVOCATION_ID)));
114     }
115
116     private void validateLogging(ListAppender<ILoggingEvent> logAppender, String infoMessage) {
117         assertEquals(logAppender.list.get(0).getMarker().getName(), "ENTRY");
118         assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("InvocationID"));
119         assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("RequestID"));
120         assertTrue("Info missing in log", logAppender.list.toString().contains("[INFO] " + infoMessage));
121         assertEquals(logAppender.list.get(1).getMarker().getName(), "EXIT");
122         logAppender.stop();
123     }
124 }