9b8197f923cbf1d0c2bb69fbff39ffdd3c1effaf
[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.doReturn;
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.model.Counters;
38 import org.onap.dcaegen2.collectors.datafile.tasks.ScheduledTasks;
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.ResponseEntity;
44 import reactor.core.publisher.Mono;
45
46 @ExtendWith(MockitoExtension.class)
47 public class StatusControllerTest {
48     @Mock
49     ScheduledTasks scheduledTasksMock;
50
51     StatusController controllerUnderTest;
52
53     @BeforeEach
54     public void setup() {
55         controllerUnderTest = new StatusController(scheduledTasksMock);
56     }
57
58     @Test
59     public void heartbeat_success() {
60         HttpHeaders httpHeaders = new HttpHeaders();
61
62         ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(StatusController.class);
63         Mono<ResponseEntity<String>> result = controllerUnderTest.heartbeat(httpHeaders);
64
65         validateLogging(logAppender);
66
67         String body = result.block().getBody();
68         assertTrue(body.startsWith("I'm living!"));
69
70         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.REQUEST_ID)));
71         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.INVOCATION_ID)));
72     }
73
74     @Test
75     public void status() {
76         Counters counters = new Counters();
77         doReturn(counters).when(scheduledTasksMock).getCounters();
78
79         HttpHeaders httpHeaders = new HttpHeaders();
80
81         Mono<ResponseEntity<String>> result = controllerUnderTest.status(httpHeaders);
82
83         String body = result.block().getBody();
84         System.out.println(body);
85
86         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.REQUEST_ID)));
87         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.INVOCATION_ID)));
88     }
89
90     private void validateLogging(ListAppender<ILoggingEvent> logAppender) {
91         assertEquals(logAppender.list.get(0).getMarker().getName(), "ENTRY");
92         assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("InvocationID"));
93         assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("RequestID"));
94         assertTrue("Info missing in log", logAppender.list.toString().contains("[INFO] Heartbeat request"));
95         assertEquals(logAppender.list.get(1).getMarker().getName(), "EXIT");
96         logAppender.stop();
97     }
98 }