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.controller;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
30 import ch.qos.logback.classic.spi.ILoggingEvent;
31 import ch.qos.logback.core.read.ListAppender;
32 import org.apache.commons.lang3.StringUtils;
33 import org.junit.jupiter.api.Test;
34 import org.onap.dcaegen2.collectors.datafile.controllers.HeartbeatController;
35 import org.onap.dcaegen2.collectors.datafile.tasks.ScheduledTasks;
36 import org.onap.dcaegen2.collectors.datafile.utils.LoggingUtils;
37 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables;
39 import org.springframework.http.HttpHeaders;
40 import org.springframework.http.ResponseEntity;
41 import reactor.core.publisher.Mono;
43 public class HeartbeatControllerTest {
45 public void heartbeat_success() {
46 ScheduledTasks scheduledTasksMock = mock(ScheduledTasks.class);
47 when(scheduledTasksMock.getCurrentNumberOfTasks()).thenReturn(10);
48 when(scheduledTasksMock.publishedFilesCacheSize()).thenReturn(20);
50 HttpHeaders httpHeaders = new HttpHeaders();
52 HeartbeatController controllerUnderTest = new HeartbeatController(scheduledTasksMock);
54 ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(HeartbeatController.class);
55 Mono<ResponseEntity<String>> result = controllerUnderTest.heartbeat(httpHeaders);
57 validateLogging(logAppender);
59 String body = result.block().getBody();
60 assertTrue(body.startsWith("I'm living! Status: "));
61 assertTrue(body.contains("numberOfFileCollectionTasks=10"));
62 assertTrue(body.contains("fileCacheSize=20"));
64 assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.REQUEST_ID)));
65 assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.INVOCATION_ID)));
68 private void validateLogging(ListAppender<ILoggingEvent> logAppender) {
69 assertEquals(logAppender.list.get(0).getMarker().getName(), "ENTRY");
70 assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("InvocationID"));
71 assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("RequestID"));
72 assertEquals("[INFO] Heartbeat request", logAppender.list.get(0).toString());
73 assertEquals(logAppender.list.get(1).getMarker().getName(), "EXIT");
74 assertEquals("[INFO] Heartbeat request", logAppender.list.get(1).toString());