345c411cdacb05d61606104ed60b8839a5f83ca8
[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
32 import org.apache.commons.lang3.StringUtils;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.extension.ExtendWith;
36 import org.mockito.Mock;
37 import org.mockito.junit.jupiter.MockitoExtension;
38 import org.onap.dcaegen2.collectors.datafile.configuration.SchedulerConfig;
39 import org.onap.dcaegen2.collectors.datafile.utils.LoggingUtils;
40 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables;
41 import org.slf4j.MDC;
42 import org.springframework.http.HttpHeaders;
43 import org.springframework.http.HttpStatus;
44 import org.springframework.http.ResponseEntity;
45 import reactor.core.publisher.Mono;
46
47 @ExtendWith(MockitoExtension.class)
48 public class ScheduleControllerTest {
49     @Mock
50     private SchedulerConfig schedulerConfigMock;
51
52     private ScheduleController scheduleControllerUnderTest;
53
54     @BeforeEach
55     public void setup() {
56         scheduleControllerUnderTest = new ScheduleController(schedulerConfigMock);
57     }
58
59     @Test
60     public void startTasksSuccess() {
61         when(schedulerConfigMock.tryToStartTask()).thenReturn(Boolean.TRUE);
62
63         HttpHeaders httpHeaders = new HttpHeaders();
64
65         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ScheduleController.class);
66         Mono<ResponseEntity<String>> response = scheduleControllerUnderTest.startTasks(httpHeaders);
67
68         validateLogging(logAppender, "Start request");
69
70         String body = response.block().getBody();
71         assertTrue(body.startsWith("Datafile Service has been started!"));
72
73         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.REQUEST_ID)));
74         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.INVOCATION_ID)));
75     }
76
77     @Test
78     public void startTasksFail() {
79         when(schedulerConfigMock.tryToStartTask()).thenReturn(Boolean.FALSE);
80
81         HttpHeaders httpHeaders = new HttpHeaders();
82         // The following headers are set to create branch coverage in MappedDiagnosticContext:initializeTraceContext().
83         httpHeaders.set(MdcVariables.httpHeader(MdcVariables.REQUEST_ID), "Onap request ID");
84         httpHeaders.set(MdcVariables.httpHeader(MdcVariables.INVOCATION_ID), "Invocation ID");
85
86         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ScheduleController.class);
87         Mono<ResponseEntity<String>> response = scheduleControllerUnderTest.startTasks(httpHeaders);
88
89         validateLogging(logAppender, "Start request");
90
91         String body = response.block().getBody();
92         assertTrue(body.startsWith("Datafile Service is still running!"));
93
94         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.REQUEST_ID)));
95         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.INVOCATION_ID)));
96     }
97
98     @Test
99     public void stopTaskSuccess() {
100         when(schedulerConfigMock.getResponseFromCancellationOfTasks()).thenReturn(
101             Mono.just(new ResponseEntity<>("Datafile Service has already been stopped!", HttpStatus.CREATED)));
102
103         HttpHeaders httpHeaders = new HttpHeaders();
104
105         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ScheduleController.class);
106         Mono<ResponseEntity<String>> actualResponse = scheduleControllerUnderTest.stopTask(httpHeaders);
107
108         validateLogging(logAppender, "Stop request");
109
110         String body = actualResponse.block().getBody();
111         assertTrue(body.startsWith("Datafile Service has already been stopped!"));
112
113         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.REQUEST_ID)));
114         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.INVOCATION_ID)));
115     }
116
117     private void validateLogging(ListAppender<ILoggingEvent> logAppender, String infoMessage) {
118         assertEquals("ENTRY", logAppender.list.get(0).getMarker().getName());
119         assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("InvocationID"));
120         assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("RequestID"));
121         assertTrue("Info missing in log", logAppender.list.toString().contains("[INFO] " + infoMessage));
122         assertEquals("EXIT", logAppender.list.get(1).getMarker().getName());
123         logAppender.stop();
124     }
125 }