diff options
Diffstat (limited to 'hv-collector-utils')
3 files changed, 391 insertions, 0 deletions
diff --git a/hv-collector-utils/pom.xml b/hv-collector-utils/pom.xml new file mode 100644 index 00000000..ddd0a50c --- /dev/null +++ b/hv-collector-utils/pom.xml @@ -0,0 +1,106 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ ============LICENSE_START======================================================= + ~ dcaegen2-collectors-veshv + ~ ================================================================================ + ~ Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + ~ ================================================================================ + ~ 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. + ~ ============LICENSE_END========================================================= + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <licenses> + <license> + <name>The Apache Software License, Version 2.0</name> + <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> + </license> + </licenses> + + <properties> + <skipAnalysis>false</skipAnalysis> + </properties> + + <parent> + <groupId>org.onap.dcaegen2.collectors.veshv</groupId> + <artifactId>ves-hv-collector</artifactId> + <version>1.0.0-SNAPSHOT</version> + <relativePath>..</relativePath> + </parent> + + <artifactId>hv-collector-utils</artifactId> + <description>VES HighVolume Collector :: Utilities</description> + + + <build> + <plugins> + <plugin> + <artifactId>kotlin-maven-plugin</artifactId> + <groupId>org.jetbrains.kotlin</groupId> + </plugin> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <groupId>org.apache.maven.plugins</groupId> + </plugin> + </plugins> + </build> + + <dependencies> + <dependency> + <groupId>org.jetbrains.kotlin</groupId> + <artifactId>kotlin-reflect</artifactId> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </dependency> + <dependency> + <groupId>com.nhaarman</groupId> + <artifactId>mockito-kotlin</artifactId> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + </dependency> + <dependency> + <groupId>org.jetbrains.kotlin</groupId> + <artifactId>kotlin-test</artifactId> + </dependency> + <dependency> + <groupId>org.jetbrains.spek</groupId> + <artifactId>spek-api</artifactId> + </dependency> + <dependency> + <groupId>org.jetbrains.spek</groupId> + <artifactId>spek-junit-platform-engine</artifactId> + </dependency> + <dependency> + <groupId>io.projectreactor</groupId> + <artifactId>reactor-test</artifactId> + </dependency> + <dependency> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-classic</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + + +</project>
\ No newline at end of file diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt new file mode 100644 index 00000000..ab9fc2e6 --- /dev/null +++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt @@ -0,0 +1,99 @@ +/* + * ============LICENSE_START======================================================= + * dcaegen2-collectors-veshv + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.dcae.collectors.veshv.utils.logging + +import org.slf4j.LoggerFactory +import kotlin.reflect.KClass + +class Logger(val logger: org.slf4j.Logger) { + constructor(clazz: KClass<out Any>) : this(LoggerFactory.getLogger(clazz.java)) + + // + // DEBUG + // + + fun debug(message: String) { + logger.debug(message) + } + + fun debug(message: String, ex: Exception) { + logger.debug(message, ex) + } + + fun debug(messageProvider: () -> String) { + if (logger.isDebugEnabled) { + logger.debug(messageProvider()) + } + } + + fun debug(ex: Exception, messageProvider: () -> String) { + if (logger.isDebugEnabled) { + logger.debug(messageProvider(), ex) + } + } + + // + // INFO + // + fun info(message: String) { + logger.info(message) + } + + fun info(message: String, ex: Exception) { + logger.info(message, ex) + } + + fun info(messageProvider: () -> String) { + if (logger.isInfoEnabled) { + logger.info(messageProvider()) + } + } + + fun info(ex: Exception, messageProvider: () -> String) { + if (logger.isInfoEnabled) { + logger.info(messageProvider(), ex) + } + } + + + // + // WARN + // + + fun warn(message: String) { + logger.warn(message) + } + + fun warn(message: String, ex: Exception) { + logger.warn(message, ex) + } + + fun warn(messageProvider: () -> String) { + if (logger.isWarnEnabled) { + logger.warn(messageProvider()) + } + } + + fun warn(ex: Exception, messageProvider: () -> String) { + if (logger.isWarnEnabled) { + logger.warn(messageProvider(), ex) + } + } +}
\ No newline at end of file diff --git a/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LoggerTest.kt b/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LoggerTest.kt new file mode 100644 index 00000000..9e9af1fa --- /dev/null +++ b/hv-collector-utils/src/test/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LoggerTest.kt @@ -0,0 +1,186 @@ +/* + * ============LICENSE_START======================================================= + * dcaegen2-collectors-veshv + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.dcae.collectors.veshv.utils.logging + +import com.nhaarman.mockito_kotlin.mock +import com.nhaarman.mockito_kotlin.verify +import com.nhaarman.mockito_kotlin.verifyNoMoreInteractions +import com.nhaarman.mockito_kotlin.whenever +import org.jetbrains.spek.api.Spek +import org.jetbrains.spek.api.dsl.describe +import org.jetbrains.spek.api.dsl.it + +/** + * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com> + * @since May 2018 + */ +object LoggerTest : Spek({ + + lateinit var slf4jLogger: org.slf4j.Logger + lateinit var cut: Logger + + beforeEachTest { + slf4jLogger = mock() + cut = Logger(slf4jLogger) + } + + afterEachTest { + verifyNoMoreInteractions(slf4jLogger) + } + + describe("Thin Kotlin logging facade for Slf4j") { + val message = "sample message" + val exception = Exception("fail") + + describe("debug levels") { + it("should log message") { + cut.debug(message) + verify(slf4jLogger).debug(message) + } + + it("should log message with exception") { + cut.debug(message, exception) + verify(slf4jLogger).debug(message, exception) + } + + describe("lazy logging message") { + + it("should log when debug is ON") { + whenever(slf4jLogger.isDebugEnabled).thenReturn(true) + cut.debug { message } + verify(slf4jLogger).isDebugEnabled + verify(slf4jLogger).debug(message) + } + + it("should not log when debug is OFF") { + whenever(slf4jLogger.isDebugEnabled).thenReturn(false) + cut.debug { message } + verify(slf4jLogger).isDebugEnabled + } + } + + describe("lazy logging message with exception") { + + it("should log when debug is ON") { + whenever(slf4jLogger.isDebugEnabled).thenReturn(true) + cut.debug(exception) { message } + verify(slf4jLogger).isDebugEnabled + verify(slf4jLogger).debug(message, exception) + } + + it("should not log when debug is OFF") { + whenever(slf4jLogger.isDebugEnabled).thenReturn(false) + cut.debug(exception) { message } + verify(slf4jLogger).isDebugEnabled + } + } + } + + describe("info levels") { + it("should log message") { + cut.info(message) + verify(slf4jLogger).info(message) + } + + it("should log message with exception") { + cut.info(message, exception) + verify(slf4jLogger).info(message, exception) + } + + describe("lazy logging message") { + + it("should log when debug is ON") { + whenever(slf4jLogger.isInfoEnabled).thenReturn(true) + cut.info { message } + verify(slf4jLogger).isInfoEnabled + verify(slf4jLogger).info(message) + } + + it("should not log when debug is OFF") { + whenever(slf4jLogger.isInfoEnabled).thenReturn(false) + cut.info { message } + verify(slf4jLogger).isInfoEnabled + } + } + + describe("lazy logging message with exception") { + + it("should log when debug is ON") { + whenever(slf4jLogger.isInfoEnabled).thenReturn(true) + cut.info(exception) { message } + verify(slf4jLogger).isInfoEnabled + verify(slf4jLogger).info(message, exception) + } + + it("should not log when debug is OFF") { + whenever(slf4jLogger.isInfoEnabled).thenReturn(false) + cut.info(exception) { message } + verify(slf4jLogger).isInfoEnabled + } + } + } + + describe("warning levels") { + it("should log message") { + cut.warn(message) + verify(slf4jLogger).warn(message) + } + + it("should log message with exception") { + cut.warn(message, exception) + verify(slf4jLogger).warn(message, exception) + } + + describe("lazy logging message") { + + it("should log when debug is ON") { + whenever(slf4jLogger.isWarnEnabled).thenReturn(true) + cut.warn { message } + verify(slf4jLogger).isWarnEnabled + verify(slf4jLogger).warn(message) + } + + it("should not log when debug is OFF") { + whenever(slf4jLogger.isWarnEnabled).thenReturn(false) + cut.warn { message } + verify(slf4jLogger).isWarnEnabled + } + } + + describe("lazy logging message with exception") { + + it("should log when debug is ON") { + whenever(slf4jLogger.isWarnEnabled).thenReturn(true) + cut.warn(exception) { message } + verify(slf4jLogger).isWarnEnabled + verify(slf4jLogger).warn(message, exception) + } + + it("should not log when debug is OFF") { + whenever(slf4jLogger.isWarnEnabled).thenReturn(false) + cut.warn(exception) { message } + verify(slf4jLogger).isWarnEnabled + } + } + } + + + } +}) |