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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.dcaegen2.collectors.datafile.controllers;
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;
29 import ch.qos.logback.classic.spi.ILoggingEvent;
30 import ch.qos.logback.core.read.ListAppender;
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.model.Counters;
39 import org.onap.dcaegen2.collectors.datafile.tasks.ScheduledTasks;
40 import org.onap.dcaegen2.collectors.datafile.utils.LoggingUtils;
41 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables;
43 import org.springframework.http.HttpHeaders;
44 import org.springframework.http.ResponseEntity;
45 import reactor.core.publisher.Mono;
47 @ExtendWith(MockitoExtension.class)
48 public class StatusControllerTest {
50 ScheduledTasks scheduledTasksMock;
52 StatusController controllerUnderTest;
56 controllerUnderTest = new StatusController(scheduledTasksMock);
60 public void heartbeat_success() {
61 HttpHeaders httpHeaders = new HttpHeaders();
63 ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(StatusController.class);
64 Mono<ResponseEntity<String>> result = controllerUnderTest.heartbeat(httpHeaders);
66 validateLogging(logAppender);
68 String body = result.block().getBody();
69 assertTrue(body.startsWith("I'm living!"));
71 assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.REQUEST_ID)));
72 assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.INVOCATION_ID)));
76 public void status() {
77 Counters counters = new Counters();
78 doReturn(counters).when(scheduledTasksMock).getCounters();
80 HttpHeaders httpHeaders = new HttpHeaders();
82 Mono<ResponseEntity<String>> result = controllerUnderTest.status(httpHeaders);
84 String body = result.block().getBody();
85 System.out.println(body);
87 assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.REQUEST_ID)));
88 assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.INVOCATION_ID)));
91 private void validateLogging(ListAppender<ILoggingEvent> logAppender) {
92 assertEquals(logAppender.list.get(0).getMarker().getName(), "ENTRY");
93 assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("InvocationID"));
94 assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("RequestID"));
95 assertTrue("Info missing in log", logAppender.list.toString().contains("[INFO] Heartbeat request"));
96 assertEquals(logAppender.list.get(1).getMarker().getName(), "EXIT");