diff options
author | adheli.tavares <adheli.tavares@est.tech> | 2024-09-23 15:13:36 +0100 |
---|---|---|
committer | adheli.tavares <adheli.tavares@est.tech> | 2024-11-01 10:36:08 +0000 |
commit | ac4b5296ab01fc25d89cc4fc89b6407fb7254f1b (patch) | |
tree | 4fd2fe6f3b611dbdba48d6f4947bfb716eb08dc5 /utils | |
parent | f8ff4184fe5cc5d5a84c266d50e63c289729ef2f (diff) |
Moving message bus configurations to its own module
Issue-ID: POLICY-5131
Change-Id: Ibbcdc487300767e7b10d69e9b388c50f09e1adbc
Signed-off-by: adheli.tavares <adheli.tavares@est.tech>
Diffstat (limited to 'utils')
7 files changed, 407 insertions, 27 deletions
diff --git a/utils/pom.xml b/utils/pom.xml index 791a048a..18550e4e 100644 --- a/utils/pom.xml +++ b/utils/pom.xml @@ -45,12 +45,21 @@ <version>${project.version}</version> </dependency> <dependency> - <groupId>commons-net</groupId> - <artifactId>commons-net</artifactId> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-classic</artifactId> </dependency> <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-api</artifactId> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-core</artifactId> + </dependency> + <dependency> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>com.google.re2j</groupId> + <artifactId>re2j</artifactId> <scope>provided</scope> </dependency> <dependency> @@ -62,42 +71,59 @@ <artifactId>commons-io</artifactId> </dependency> <dependency> - <groupId>jakarta.persistence</groupId> - <artifactId>jakarta.persistence-api</artifactId> + <groupId>commons-net</groupId> + <artifactId>commons-net</artifactId> </dependency> <dependency> - <groupId>ch.qos.logback</groupId> - <artifactId>logback-classic</artifactId> + <groupId>jakarta.persistence</groupId> + <artifactId>jakarta.persistence-api</artifactId> </dependency> <dependency> - <groupId>ch.qos.logback</groupId> - <artifactId>logback-core</artifactId> + <groupId>jakarta.xml.bind</groupId> + <artifactId>jakarta.xml.bind-api</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <dependency> - <groupId>com.google.re2j</groupId> - <artifactId>re2j</artifactId> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <scope>provided</scope> </dependency> <dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> </dependency> <dependency> + <groupId>com.openpojo</groupId> + <artifactId>openpojo</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <scope>test</scope> </dependency> <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>hamcrest</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.mockito</groupId> - <artifactId>mockito-junit-jupiter</artifactId> + <artifactId>mockito-core</artifactId> <scope>test</scope> </dependency> <dependency> - <groupId>jakarta.xml.bind</groupId> - <artifactId>jakarta.xml.bind-api</artifactId> + <groupId>org.mockito</groupId> + <artifactId>mockito-junit-jupiter</artifactId> + <scope>test</scope> </dependency> </dependencies> </project> diff --git a/utils/src/main/java/org/onap/policy/common/utils/properties/PropertyUtils.java b/utils/src/main/java/org/onap/policy/common/utils/properties/PropertyUtils.java new file mode 100644 index 00000000..7155323b --- /dev/null +++ b/utils/src/main/java/org/onap/policy/common/utils/properties/PropertyUtils.java @@ -0,0 +1,123 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019, 2021 AT&T 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.policy.common.utils.properties; + +import java.util.Properties; +import lombok.AllArgsConstructor; +import org.apache.commons.lang3.StringUtils; + +/** + * Utilities for extracting property values and converting them to other types. + */ +@AllArgsConstructor +public class PropertyUtils { + /** + * Properties on which to work. + */ + private Properties properties; + + /** + * Prefix to prepend to property names. + */ + private String prefix; + + /** + * Function to invoke if a property value is invalid. + */ + private TriConsumer<String, String, Exception> invalidHandler; + + /** + * Gets a string property. + * + * @param propName name of the property whose value is to be retrieved + * @param defaultValue value to use if the property value is empty or does not exist + * @return the property's value + */ + public String getString(String propName, String defaultValue) { + String propValue = getProperty(propName); + return (StringUtils.isBlank(propValue) ? defaultValue : propValue); + } + + /** + * Gets a boolean property. + * + * @param propName name of the property whose value is to be retrieved + * @param defaultValue value to use if the property value is empty or does not exist + * @return the property's value + */ + public boolean getBoolean(String propName, boolean defaultValue) { + String propValue = getProperty(propName); + + if (!StringUtils.isBlank(propValue)) { + return Boolean.parseBoolean(propValue); + } + + return defaultValue; + } + + /** + * Gets an integer property. + * + * @param propName name of the property whose value is to be retrieved + * @param defaultValue value to use if the property value is empty or does not exist + * @return the property's value + */ + public int getInteger(String propName, int defaultValue) { + String propValue = getProperty(propName); + + if (!StringUtils.isBlank(propValue)) { + try { + return Integer.parseInt(propValue); + + } catch (NumberFormatException nfe) { + invalidHandler.accept(getFullName(propName), propValue, nfe); + } + } + + return defaultValue; + } + + + /** + * Gets a property's value. + * + * @param propName name of the property whose value is to be retrieved + * @return the property's value, or {@code null} if it does not exist + */ + private String getProperty(String propName) { + return properties.getProperty(getFullName(propName)); + } + + /** + * Gets the full property name, with the prefix prepended. + * + * @param propName property name, without the prefix + * @return the full property name + */ + private String getFullName(String propName) { + return prefix + propName; + } + + @FunctionalInterface + public static interface TriConsumer<A, B, C> { + public void accept(A propName, B propValue, C exception); + } +} diff --git a/utils/src/main/java/org/onap/policy/common/utils/report/HealthCheckReport.java b/utils/src/main/java/org/onap/policy/common/utils/report/HealthCheckReport.java new file mode 100644 index 00000000..78356cfb --- /dev/null +++ b/utils/src/main/java/org/onap/policy/common/utils/report/HealthCheckReport.java @@ -0,0 +1,44 @@ +/*- + * ============LICENSE_START=============================================================== + * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2018, 2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2024 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.policy.common.utils.report; + +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +/** + * Class to represent health check report of a service. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +@Getter +@Setter +@ToString +public class HealthCheckReport { + + private String name; + private String url; + private boolean healthy; + private int code; + private String message; +} diff --git a/utils/src/main/java/org/onap/policy/common/utils/services/OrderedServiceImpl.java b/utils/src/main/java/org/onap/policy/common/utils/services/OrderedServiceImpl.java index 998d6742..3726ef89 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/services/OrderedServiceImpl.java +++ b/utils/src/main/java/org/onap/policy/common/utils/services/OrderedServiceImpl.java @@ -3,6 +3,7 @@ * utils * ================================================================================ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,22 +36,22 @@ import org.slf4j.LoggerFactory; */ public class OrderedServiceImpl<T extends OrderedService> { // logger - private static Logger logger = LoggerFactory.getLogger(OrderedServiceImpl.class); + private static final Logger logger = LoggerFactory.getLogger(OrderedServiceImpl.class); // sorted list of instances implementing the service private List<T> implementers = null; // 'ServiceLoader' that is used to discover and create the services - private ServiceLoader<T> serviceLoader = null; + private final ServiceLoader<T> serviceLoader; // use this to ensure that we only use one unique instance of each class - private static Map<Class<?>, OrderedService> classToSingleton = new HashMap<>(); + private static final Map<Class<?>, OrderedService> classToSingleton = new HashMap<>(); /** * Constructor - create the 'ServiceLoader' instance. * * @param clazz the class object associated with 'T' (I supposed it could - * be a subclass, but I'm not sure this is useful) + * be a subclass, but I'm not sure if this is useful) */ public OrderedServiceImpl(Class<T> clazz) { // This constructor wouldn't be needed if 'T.class' was legal @@ -60,8 +61,7 @@ public class OrderedServiceImpl<T extends OrderedService> { /** * Get List of implementers. * - * @return the sorted list of services implementing interface 'T' discovered - * by 'ServiceLoader'. + * @return the sorted list of services implementing interface 'T' discovered by 'ServiceLoader'. */ public synchronized List<T> getList() { if (implementers == null) { @@ -78,12 +78,11 @@ public class OrderedServiceImpl<T extends OrderedService> { * expensive operation in terms of CPU and elapsed time, so it is best if it * isn't invoked too frequently. * - * @return the sorted list of services implementing interface 'T' discovered - * by 'ServiceLoader'. + * @return the sorted list of services implementing interface 'T' discovered by 'ServiceLoader'. */ @SuppressWarnings("unchecked") public synchronized List<T> rebuildList() { - // build a list of all of the current implementors + // build a list of all the current implementors List<T> tmp = new LinkedList<>(); for (T service : serviceLoader) { tmp.add((T) getSingleton(service)); @@ -91,7 +90,7 @@ public class OrderedServiceImpl<T extends OrderedService> { // Sort the list according to sequence number, and then alphabetically // according to full class name. - Collections.sort(tmp, (o1, o2) -> { + tmp.sort((o1, o2) -> { int s1 = o1.getSequenceNumber(); int s2 = o2.getSequenceNumber(); if (s1 < s2) { @@ -112,7 +111,7 @@ public class OrderedServiceImpl<T extends OrderedService> { /** * If a service implements multiple APIs managed by 'ServiceLoader', a * separate instance is created for each API. This method ensures that - * the first instance is used in all of the lists. + * the first instance is used in all the lists. * * @param service this is the object created by ServiceLoader * @return the object to use in place of 'service'. If 'service' is the first diff --git a/utils/src/test/java/org/onap/policy/common/utils/properties/PropertyUtilsTest.java b/utils/src/test/java/org/onap/policy/common/utils/properties/PropertyUtilsTest.java new file mode 100644 index 00000000..a8b37f57 --- /dev/null +++ b/utils/src/test/java/org/onap/policy/common/utils/properties/PropertyUtilsTest.java @@ -0,0 +1,110 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023-2024 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.utils.properties; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Properties; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class PropertyUtilsTest { + private static final String DFLT_STRING = "my-default"; + private static final int DLFT_INT = 1000; + + private PropertyUtils utils; + private String invalidName; + private String invalidValue; + private Exception invalidEx; + + /** + * Initializes {@link #utils}. + */ + @BeforeEach + public void setUp() { + Properties properties = new Properties(); + properties.put("myPrefix.my-string", "some text"); + properties.put("myPrefix.empty-string", ""); + + properties.put("myPrefix.my-bool", "true"); + properties.put("myPrefix.my-bool2", "false"); + properties.put("myPrefix.empty-bool", ""); + properties.put("myPrefix.invalid-bool", "not a bool"); + + properties.put("myPrefix.my-int", "100"); + properties.put("myPrefix.my-int2", "200"); + properties.put("myPrefix.empty-int", ""); + properties.put("myPrefix.invalid-int", "not an int"); + + utils = new PropertyUtils(properties, "myPrefix", (name, value, ex) -> { + invalidName = name; + invalidValue = value; + invalidEx = ex; + }); + } + + @Test + void testGetString() { + assertEquals("some text", utils.getString(".my-string", DFLT_STRING)); + assertEquals(DFLT_STRING, utils.getString(".empty-string", DFLT_STRING)); + assertEquals(DFLT_STRING, utils.getString(".missing-string", DFLT_STRING)); + + assertNull(invalidName); + assertNull(invalidValue); + assertNull(invalidEx); + } + + @Test + void testGetBoolean() { + assertTrue(utils.getBoolean(".my-bool", false)); + assertFalse(utils.getBoolean(".my-bool2", true)); + assertTrue(utils.getBoolean(".empty-bool", true)); + assertFalse(utils.getBoolean(".invalid-bool", true)); + assertTrue(utils.getBoolean(".missing-bool", true)); + + assertNull(invalidName); + assertNull(invalidValue); + assertNull(invalidEx); + } + + @Test + void testGetInteger() { + assertEquals(100, utils.getInteger(".my-int", DLFT_INT)); + assertEquals(200, utils.getInteger(".my-int2", DLFT_INT)); + assertEquals(DLFT_INT, utils.getInteger(".empty-int", DLFT_INT)); + assertEquals(DLFT_INT, utils.getInteger(".missing-int", DLFT_INT)); + + assertNull(invalidName); + assertNull(invalidValue); + assertNull(invalidEx); + + assertEquals(DLFT_INT, utils.getInteger(".invalid-int", DLFT_INT)); + + assertEquals("myPrefix.invalid-int", invalidName); + assertEquals("not an int", invalidValue); + assertTrue(invalidEx instanceof NumberFormatException); + } + +} diff --git a/utils/src/test/java/org/onap/policy/common/utils/report/TestHealthCheckReport.java b/utils/src/test/java/org/onap/policy/common/utils/report/TestHealthCheckReport.java new file mode 100644 index 00000000..3a207022 --- /dev/null +++ b/utils/src/test/java/org/onap/policy/common/utils/report/TestHealthCheckReport.java @@ -0,0 +1,78 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2024 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.policy.common.utils.report; + +import static org.hamcrest.CoreMatchers.anyOf; +import static org.hamcrest.CoreMatchers.anything; + +import com.openpojo.reflection.PojoClass; +import com.openpojo.reflection.filters.FilterClassName; +import com.openpojo.validation.Validator; +import com.openpojo.validation.ValidatorBuilder; +import com.openpojo.validation.affirm.Affirm; +import com.openpojo.validation.rule.impl.GetterMustExistRule; +import com.openpojo.validation.rule.impl.SetterMustExistRule; +import com.openpojo.validation.test.Tester; +import com.openpojo.validation.test.impl.GetterTester; +import com.openpojo.validation.test.impl.SetterTester; +import com.openpojo.validation.utils.ValidationHelper; +import org.hamcrest.Matcher; +import org.junit.jupiter.api.Test; + +/** + * Class to perform unit test of HealthCheckReport. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +class TestHealthCheckReport { + + @Test + void testHealthCheckReport() { + final Validator validator = + ValidatorBuilder.create().with(new GetterMustExistRule()).with(new SetterMustExistRule()) + .with(new GetterTester()).with(new SetterTester()).with(new ToStringTester()).build(); + validator.validate(HealthCheckReport.class.getPackage().getName(), + new FilterClassName(HealthCheckReport.class.getName())); + } + + static class ToStringTester implements Tester { + + private final Matcher<?> matcher; + + public ToStringTester() { + matcher = anything(); + } + + @Override + public void run(final PojoClass pojoClass) { + final Class<?> clazz = pojoClass.getClazz(); + if (anyOf(matcher).matches(clazz)) { + final Object classInstance = ValidationHelper.getBasicInstance(pojoClass); + + Affirm.affirmFalse("Found default toString output", + classInstance.toString().matches(Object.class.getName() + "@" + "\\w+")); + } + + } + } +} diff --git a/utils/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java b/utils/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java index d1e891ca..9db367cf 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java @@ -313,7 +313,7 @@ class ResourceUtilsTest { normalizePath(resultD2.iterator().next())); Set<String> resultJ0 = ResourceUtils.getDirectoryContents("com"); - assertTrue(resultJ0.contains("com/google/gson/")); + assertTrue(resultJ0.contains("com/google/")); assertEquals("com/google/", normalizePath(resultJ0.iterator().next())); Set<String> resultJ1 = ResourceUtils.getDirectoryContents("com/google/gson"); |