51097f52409430e55c957787f215455a98a7bdd5
[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.controller;
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 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
30
31 import ch.qos.logback.classic.spi.ILoggingEvent;
32 import ch.qos.logback.core.read.ListAppender;
33 import org.apache.commons.lang3.StringUtils;
34 import org.junit.jupiter.api.Test;
35 import org.onap.dcaegen2.collectors.datafile.controllers.StatusController;
36 import org.onap.dcaegen2.collectors.datafile.model.Counters;
37 import org.onap.dcaegen2.collectors.datafile.tasks.ScheduledTasks;
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.ResponseEntity;
43 import reactor.core.publisher.Mono;
44
45 public class StatusControllerTest {
46     @Test
47     public void heartbeat_success() {
48         ScheduledTasks scheduledTasksMock = mock(ScheduledTasks.class);
49
50         HttpHeaders httpHeaders = new HttpHeaders();
51
52         StatusController controllerUnderTest = new StatusController(scheduledTasksMock);
53
54         ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(StatusController.class);
55         Mono<ResponseEntity<String>> result = controllerUnderTest.heartbeat(httpHeaders);
56
57         validateLogging(logAppender);
58
59         String body = result.block().getBody();
60         assertTrue(body.startsWith("I'm living!"));
61
62         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.REQUEST_ID)));
63         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.INVOCATION_ID)));
64     }
65
66
67     @Test
68     public void status() {
69         ScheduledTasks scheduledTasksMock = mock(ScheduledTasks.class);
70         Counters counters = new Counters();
71         doReturn(counters).when(scheduledTasksMock).getCounters();
72
73         HttpHeaders httpHeaders = new HttpHeaders();
74
75         StatusController controllerUnderTest = new StatusController(scheduledTasksMock);
76
77         Mono<ResponseEntity<String>> result = controllerUnderTest.status(httpHeaders);
78
79         String body = result.block().getBody();
80         System.out.println(body);
81
82         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.REQUEST_ID)));
83         assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.INVOCATION_ID)));
84     }
85
86
87     private void validateLogging(ListAppender<ILoggingEvent> logAppender) {
88         assertEquals(logAppender.list.get(0).getMarker().getName(), "ENTRY");
89         assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("InvocationID"));
90         assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("RequestID"));
91         assertTrue("Info missing in log", logAppender.list.toString().contains("[INFO] Heartbeat request"));
92         assertEquals(logAppender.list.get(1).getMarker().getName(), "EXIT");
93         logAppender.stop();
94     }
95 }