diff options
6 files changed, 146 insertions, 11 deletions
diff --git a/datafile-app-server/pom.xml b/datafile-app-server/pom.xml index fa02b79e..c985a034 100644 --- a/datafile-app-server/pom.xml +++ b/datafile-app-server/pom.xml @@ -211,10 +211,6 @@ <scope>test</scope> </dependency> <dependency> - <groupId>org.apache.commons</groupId> - <artifactId>commons-lang3</artifactId> - </dependency> - <dependency> <groupId>com.github.stefanbirkner</groupId> <artifactId>fake-sftp-server-rule</artifactId> <scope>test</scope> diff --git a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/controllers/HeartbeatController.java b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/controllers/HeartbeatController.java index b0e339ef..c6d56c42 100644 --- a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/controllers/HeartbeatController.java +++ b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/controllers/HeartbeatController.java @@ -16,13 +16,18 @@ package org.onap.dcaegen2.collectors.datafile.controllers; +import static org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext.ENTRY; +import static org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext.EXIT; + import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; import org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext; +import org.onap.dcaegen2.collectors.datafile.tasks.ScheduledTasks; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -43,6 +48,13 @@ public class HeartbeatController { private static final Logger logger = LoggerFactory.getLogger(HeartbeatController.class); + private final ScheduledTasks scheduledTasks; + + @Autowired + public HeartbeatController(ScheduledTasks scheduledTasks) { + this.scheduledTasks = scheduledTasks; + } + /** * Checks the heartbeat of DFC. * @@ -54,13 +66,18 @@ public class HeartbeatController { @ApiResponse(code = 200, message = "DATAFILE service is living"), @ApiResponse(code = 401, message = "You are not authorized to view the resource"), @ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"), - @ApiResponse(code = 404, message = "The resource you were trying to reach is not found")}) + @ApiResponse(code = 404, message = "The resource you were trying to reach is not found") }) public Mono<ResponseEntity<String>> heartbeat(@RequestHeader HttpHeaders headers) { MappedDiagnosticContext.initializeTraceContext(headers); - logger.info(MappedDiagnosticContext.ENTRY, "Heartbeat request"); - Mono<ResponseEntity<String>> response = Mono.just(new ResponseEntity<>("I'm living", HttpStatus.OK)); - logger.trace("Heartbeat"); - logger.info(MappedDiagnosticContext.EXIT, "Heartbeat request"); + logger.info(ENTRY, "Heartbeat request"); + + StringBuilder statusString = new StringBuilder("I'm living! Status: "); + statusString.append("numberOfFileCollectionTasks=").append(scheduledTasks.getCurrentNumberOfTasks()) + .append(","); + statusString.append("fileCacheSize=").append(scheduledTasks.publishedFilesCacheSize()); + + Mono<ResponseEntity<String>> response = Mono.just(new ResponseEntity<>(statusString.toString(), HttpStatus.OK)); + logger.info(EXIT, "Heartbeat request"); return response; } } diff --git a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/service/PublishedFileCache.java b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/service/PublishedFileCache.java index 257c356c..2b3a0ef6 100644 --- a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/service/PublishedFileCache.java +++ b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/service/PublishedFileCache.java @@ -63,7 +63,7 @@ public class PublishedFileCache { } } - int size() { + public int size() { return publishedFiles.size(); } diff --git a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/tasks/ScheduledTasks.java b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/tasks/ScheduledTasks.java index 037f495f..3e4481dd 100644 --- a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/tasks/ScheduledTasks.java +++ b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/tasks/ScheduledTasks.java @@ -111,10 +111,14 @@ public class ScheduledTasks { return new PublishedChecker(applicationConfiguration); } - protected int getCurrentNumberOfTasks() { + public int getCurrentNumberOfTasks() { return currentNumberOfTasks.get(); } + public int publishedFilesCacheSize() { + return alreadyPublishedFiles.size(); + } + protected DMaaPMessageConsumer createConsumerTask() { return new DMaaPMessageConsumer(this.applicationConfiguration); } diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/controller/HeartbeatControllerTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/controller/HeartbeatControllerTest.java new file mode 100644 index 00000000..814509dd --- /dev/null +++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/controller/HeartbeatControllerTest.java @@ -0,0 +1,77 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.collectors.datafile.controller; + +import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.collectors.datafile.controllers.HeartbeatController; +import org.onap.dcaegen2.collectors.datafile.tasks.ScheduledTasks; +import org.onap.dcaegen2.collectors.datafile.utils.LoggingUtils; +import org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables; +import org.slf4j.MDC; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; +import reactor.core.publisher.Mono; + +public class HeartbeatControllerTest { + @Test + public void heartbeat_success() { + ScheduledTasks scheduledTasksMock = mock(ScheduledTasks.class); + when(scheduledTasksMock.getCurrentNumberOfTasks()).thenReturn(10); + when(scheduledTasksMock.publishedFilesCacheSize()).thenReturn(20); + + HttpHeaders httpHeaders = new HttpHeaders(); + + HeartbeatController controllerUnderTest = new HeartbeatController(scheduledTasksMock); + + ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(HeartbeatController.class); + Mono<ResponseEntity<String>> result = controllerUnderTest.heartbeat(httpHeaders); + + validateLogging(logAppender); + + String body = result.block().getBody(); + assertTrue(body.startsWith("I'm living! Status: ")); + assertTrue(body.contains("numberOfFileCollectionTasks=10")); + assertTrue(body.contains("fileCacheSize=20")); + + assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.REQUEST_ID))); + assertFalse(StringUtils.isBlank(MDC.get(MdcVariables.INVOCATION_ID))); + } + + private void validateLogging(ListAppender<ILoggingEvent> logAppender) { + assertEquals(logAppender.list.get(0).getMarker().getName(), "ENTRY"); + assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("InvocationID")); + assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("RequestID")); + assertEquals("[INFO] Heartbeat request", logAppender.list.get(0).toString()); + assertEquals(logAppender.list.get(1).getMarker().getName(), "EXIT"); + assertEquals("[INFO] Heartbeat request", logAppender.list.get(1).toString()); + logAppender.stop(); + } +} diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/utils/LoggingUtils.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/utils/LoggingUtils.java new file mode 100644 index 00000000..45edac79 --- /dev/null +++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/utils/LoggingUtils.java @@ -0,0 +1,41 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.collectors.datafile.utils; + +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; +import org.slf4j.LoggerFactory; + +public class LoggingUtils { + + /** + * Returns a ListAppender that contains all logging events. Call this method at the very beginning of the test + * */ + public static ListAppender<ILoggingEvent> getLogListAppender(Class<?> logClass) { + Logger logger = (Logger) LoggerFactory.getLogger(logClass); + ListAppender<ILoggingEvent> listAppender = new ListAppender<>(); + listAppender.start(); + logger.addAppender(listAppender); + + return listAppender; + } +} |