From 8eaa7f30bfc5085e1affc9ab43db843837c7d3c0 Mon Sep 17 00:00:00 2001 From: danielhanrahan Date: Fri, 13 Feb 2026 18:08:30 +0000 Subject: [PATCH] Move test utils into new module Move test utility classes from policy-common into a separate module, so that dependencies such as hamcrest and openpojo will not be included in final JAR. Issue-ID: POLICY-5503 Change-Id: Ib7703737375ecae347dd7befd7c72d38b83c68dc Signed-off-by: danielhanrahan --- clamp-parent/dependencies/pom.xml | 10 +++ models/pom.xml | 6 ++ participant/participant-impl/pom.xml | 10 +++ policy-common/pom.xml | 12 +++- .../common/utils/jackson/JacksonTestUtilsTest.json | 4 -- policy-models/pom.xml | 6 ++ pom.xml | 1 + runtime-acm/pom.xml | 6 ++ test-utils/pom.xml | 83 ++++++++++++++++++++++ .../common/utils/jackson/JacksonTestUtils.java | 6 -- .../common/utils/resources/ResourceUtils.java | 23 +----- .../onap/policy/common/utils/test/PojoTester.java | 0 .../policy/common/utils/test/ToStringTester.java | 0 .../utils/test/log/logback/ExtractAppender.java | 0 .../common/utils/jackson/JacksonTestUtilsTest.java | 3 + .../common/utils/resources/ResourceUtilsTest.java | 31 +++----- .../policy/common/utils/test/PojoTesterTest.java | 0 .../common/utils/test/ToStringTesterTest.java | 0 .../test/log/logback/ExtractAppenderTest.java | 0 .../common/utils/test/pojo/empty/NotAPojo.java | 0 .../utils/test/pojo/invalid/InvalidPojo.java | 0 .../common/utils/test/pojo/valid/ValidPojo.java | 0 .../common/utils/jackson/JacksonTestUtilsTest.json | 4 ++ .../src/test/resources/testdir/testfile.xml | 0 24 files changed, 150 insertions(+), 55 deletions(-) delete mode 100644 policy-common/src/test/resources/org/onap/policy/common/utils/jackson/JacksonTestUtilsTest.json create mode 100644 test-utils/pom.xml rename {policy-common => test-utils}/src/main/java/org/onap/policy/common/utils/jackson/JacksonTestUtils.java (97%) rename {policy-common => test-utils}/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java (93%) rename {policy-common => test-utils}/src/main/java/org/onap/policy/common/utils/test/PojoTester.java (100%) rename {policy-common => test-utils}/src/main/java/org/onap/policy/common/utils/test/ToStringTester.java (100%) rename {policy-common => test-utils}/src/main/java/org/onap/policy/common/utils/test/log/logback/ExtractAppender.java (100%) rename {policy-common => test-utils}/src/test/java/org/onap/policy/common/utils/jackson/JacksonTestUtilsTest.java (97%) rename {policy-common => test-utils}/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java (89%) rename {policy-common => test-utils}/src/test/java/org/onap/policy/common/utils/test/PojoTesterTest.java (100%) rename {policy-common => test-utils}/src/test/java/org/onap/policy/common/utils/test/ToStringTesterTest.java (100%) rename {policy-common => test-utils}/src/test/java/org/onap/policy/common/utils/test/log/logback/ExtractAppenderTest.java (100%) rename {policy-common => test-utils}/src/test/java/org/onap/policy/common/utils/test/pojo/empty/NotAPojo.java (100%) rename {policy-common => test-utils}/src/test/java/org/onap/policy/common/utils/test/pojo/invalid/InvalidPojo.java (100%) rename {policy-common => test-utils}/src/test/java/org/onap/policy/common/utils/test/pojo/valid/ValidPojo.java (100%) create mode 100644 test-utils/src/test/resources/org/onap/policy/common/utils/jackson/JacksonTestUtilsTest.json rename {policy-common => test-utils}/src/test/resources/testdir/testfile.xml (100%) diff --git a/clamp-parent/dependencies/pom.xml b/clamp-parent/dependencies/pom.xml index afea52205..20bc8d1dd 100644 --- a/clamp-parent/dependencies/pom.xml +++ b/clamp-parent/dependencies/pom.xml @@ -316,6 +316,7 @@ org.hamcrest hamcrest ${version.hamcrest} + test org.hibernate.orm @@ -346,6 +347,7 @@ org.projectlombok lombok ${version.lombok} + provided org.slf4j @@ -949,6 +951,14 @@ org.springframework.boot spring-boot-maven-plugin ${version.springboot} + + + + org.projectlombok + lombok + + + diff --git a/models/pom.xml b/models/pom.xml index bc95825f4..42a63cf0c 100644 --- a/models/pom.xml +++ b/models/pom.xml @@ -84,6 +84,12 @@ org.springframework spring-tx + + org.onap.policy.clamp + test-utils + ${project.version} + test + org.junit.jupiter junit-jupiter-api diff --git a/participant/participant-impl/pom.xml b/participant/participant-impl/pom.xml index d42735c68..eed41a37c 100644 --- a/participant/participant-impl/pom.xml +++ b/participant/participant-impl/pom.xml @@ -50,6 +50,10 @@ policy-clamp-participant-intermediary ${project.version} + + org.projectlombok + lombok + org.springframework.boot spring-boot-starter-webflux @@ -94,5 +98,11 @@ 5.3.2 test + + org.onap.policy.clamp + test-utils + ${project.version} + test + diff --git a/policy-common/pom.xml b/policy-common/pom.xml index ba85c9d37..8cd37bb74 100644 --- a/policy-common/pom.xml +++ b/policy-common/pom.xml @@ -119,17 +119,23 @@ com.openpojo openpojo - compile + test org.hamcrest hamcrest - compile + test org.junit.jupiter junit-jupiter-api - compile + test + + + org.onap.policy.clamp + test-utils + ${project.version} + test org.assertj diff --git a/policy-common/src/test/resources/org/onap/policy/common/utils/jackson/JacksonTestUtilsTest.json b/policy-common/src/test/resources/org/onap/policy/common/utils/jackson/JacksonTestUtilsTest.json deleted file mode 100644 index 56897ba9d..000000000 --- a/policy-common/src/test/resources/org/onap/policy/common/utils/jackson/JacksonTestUtilsTest.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "id": "${obj.id}", - "text": "${obj.text}" -} diff --git a/policy-models/pom.xml b/policy-models/pom.xml index 54e4e5371..f0b6c0dde 100644 --- a/policy-models/pom.xml +++ b/policy-models/pom.xml @@ -72,6 +72,12 @@ org.slf4j slf4j-api + + org.onap.policy.clamp + test-utils + ${project.version} + test + org.junit.jupiter junit-jupiter-api diff --git a/pom.xml b/pom.xml index 29e0bd5d4..821158db8 100644 --- a/pom.xml +++ b/pom.xml @@ -50,6 +50,7 @@ clamp-parent + test-utils policy-common policy-models common diff --git a/runtime-acm/pom.xml b/runtime-acm/pom.xml index 52d46b99c..6faf16b51 100644 --- a/runtime-acm/pom.xml +++ b/runtime-acm/pom.xml @@ -295,6 +295,12 @@ testcontainers-postgresql test + + org.onap.policy.clamp + test-utils + ${project.version} + test + diff --git a/test-utils/pom.xml b/test-utils/pom.xml new file mode 100644 index 000000000..01c4bf589 --- /dev/null +++ b/test-utils/pom.xml @@ -0,0 +1,83 @@ + + + + + 4.0.0 + + + org.onap.policy.clamp + dependencies + 9.0.2-SNAPSHOT + ../clamp-parent/dependencies/pom.xml + + + test-utils + ${project.artifactId} + + + + com.openpojo + openpojo + compile + + + org.hamcrest + hamcrest + compile + + + org.projectlombok + lombok + + + org.junit.jupiter + junit-jupiter-api + compile + + + com.fasterxml.jackson.core + jackson-databind + + + com.google.re2j + re2j + + + commons-io + commons-io + + + org.slf4j + slf4j-api + + + ch.qos.logback + logback-classic + + + org.assertj + assertj-core + test + + + diff --git a/policy-common/src/main/java/org/onap/policy/common/utils/jackson/JacksonTestUtils.java b/test-utils/src/main/java/org/onap/policy/common/utils/jackson/JacksonTestUtils.java similarity index 97% rename from policy-common/src/main/java/org/onap/policy/common/utils/jackson/JacksonTestUtils.java rename to test-utils/src/main/java/org/onap/policy/common/utils/jackson/JacksonTestUtils.java index e7f2b5d0b..29b69a0a4 100644 --- a/policy-common/src/main/java/org/onap/policy/common/utils/jackson/JacksonTestUtils.java +++ b/test-utils/src/main/java/org/onap/policy/common/utils/jackson/JacksonTestUtils.java @@ -28,7 +28,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.NullNode; import com.fasterxml.jackson.databind.node.ObjectNode; -import com.google.re2j.Pattern; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -44,11 +43,6 @@ import lombok.Getter; @Getter public class JacksonTestUtils { - /** - * Matches script items, of the form ${xxx}, within text. - */ - private static final Pattern SCRIPT_PAT = Pattern.compile("\\$\\{([^}]+)\\}"); - /** * Engine used to interpolate strings before they're compared. */ diff --git a/policy-common/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java b/test-utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java similarity index 93% rename from policy-common/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java rename to test-utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java index 3ee062f10..f1351b775 100644 --- a/policy-common/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java +++ b/test-utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java @@ -193,25 +193,6 @@ public final class ResourceUtils { } } - /** - * Gets the file path for a resource on the local file system or on the class path. - * - * @param resource the resource to the get the file path for - * @return the resource file path - */ - public static String getFilePath4Resource(final String resource) { - if (resource == null) { - return null; - } - - var modelFileUrl = getUrl4Resource(resource); - if (modelFileUrl != null) { - return modelFileUrl.getPath(); - } else { - return resource; - } - } - /** * Read the list of entries in a resource directory. * @@ -246,7 +227,7 @@ public final class ResourceUtils { * @param resourceDirectoryName the name of the resource directory * @return a set of the directory contents */ - public static Set getDirectoryContentsLocal(final URL localResourceDirectoryUrl, + private static Set getDirectoryContentsLocal(final URL localResourceDirectoryUrl, final String resourceDirectoryName) { var localDirectory = new File(localResourceDirectoryUrl.getFile()); @@ -275,7 +256,7 @@ public final class ResourceUtils { * @param resourceDirectoryName the name of the resource directory * @return a set of the directory contents */ - public static Set getDirectoryContentsJar(final URL jarResourceDirectoryUrl, + private static Set getDirectoryContentsJar(final URL jarResourceDirectoryUrl, final String resourceDirectoryName) { String dirNameWithSlash = resourceDirectoryName + "/"; int minLength = dirNameWithSlash.length() + 1; diff --git a/policy-common/src/main/java/org/onap/policy/common/utils/test/PojoTester.java b/test-utils/src/main/java/org/onap/policy/common/utils/test/PojoTester.java similarity index 100% rename from policy-common/src/main/java/org/onap/policy/common/utils/test/PojoTester.java rename to test-utils/src/main/java/org/onap/policy/common/utils/test/PojoTester.java diff --git a/policy-common/src/main/java/org/onap/policy/common/utils/test/ToStringTester.java b/test-utils/src/main/java/org/onap/policy/common/utils/test/ToStringTester.java similarity index 100% rename from policy-common/src/main/java/org/onap/policy/common/utils/test/ToStringTester.java rename to test-utils/src/main/java/org/onap/policy/common/utils/test/ToStringTester.java diff --git a/policy-common/src/main/java/org/onap/policy/common/utils/test/log/logback/ExtractAppender.java b/test-utils/src/main/java/org/onap/policy/common/utils/test/log/logback/ExtractAppender.java similarity index 100% rename from policy-common/src/main/java/org/onap/policy/common/utils/test/log/logback/ExtractAppender.java rename to test-utils/src/main/java/org/onap/policy/common/utils/test/log/logback/ExtractAppender.java diff --git a/policy-common/src/test/java/org/onap/policy/common/utils/jackson/JacksonTestUtilsTest.java b/test-utils/src/test/java/org/onap/policy/common/utils/jackson/JacksonTestUtilsTest.java similarity index 97% rename from policy-common/src/test/java/org/onap/policy/common/utils/jackson/JacksonTestUtilsTest.java rename to test-utils/src/test/java/org/onap/policy/common/utils/jackson/JacksonTestUtilsTest.java index caf92ad02..56446efdd 100644 --- a/policy-common/src/test/java/org/onap/policy/common/utils/jackson/JacksonTestUtilsTest.java +++ b/test-utils/src/test/java/org/onap/policy/common/utils/jackson/JacksonTestUtilsTest.java @@ -53,6 +53,9 @@ class JacksonTestUtilsTest { var data = new Data(); data.setId(500); data.setText(HELLO); + // file is found + assertThatCode(() -> utils.compareJson(data, JacksonTestUtilsTest.class)) + .doesNotThrowAnyException(); // file not found var file = new File(JacksonTestUtilsTest.class.getSimpleName() + "-NotFound.json"); assertThatThrownBy(() -> utils.compareJson(data, file)) diff --git a/policy-common/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java b/test-utils/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java similarity index 89% rename from policy-common/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java rename to test-utils/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java index 60307a249..eb304ddc7 100644 --- a/policy-common/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java +++ b/test-utils/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2020-2021, 2023-2024,2026 OpenInfra Foundation Europe. All rights reserved. + * Modifications Copyright (C) 2020-2026 OpenInfra Foundation Europe. 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. @@ -31,7 +31,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.net.MalformedURLException; import java.net.URL; import java.util.Set; import org.junit.jupiter.api.AfterEach; @@ -245,7 +244,7 @@ class ResourceUtilsTest { theString = ResourceUtils.getResourceAsString(""); - assertEquals("keystore-test\nlogback-test.xml\nMETA-INF\norg\ntestdir\nversion.txt\nwebapps\n", theString); + assertEquals("org\ntestdir\n", theString); } @@ -286,15 +285,7 @@ class ResourceUtilsTest { } @Test - void testGetFilePath4Resource() { - assertNull(ResourceUtils.getFilePath4Resource(null)); - assertEquals("/something/else", ResourceUtils.getFilePath4Resource("/something/else")); - assertTrue(ResourceUtils.getFilePath4Resource("xml/example.xml").endsWith("xml/example.xml")); - assertTrue(ResourceUtils.getFilePath4Resource("com/google").contains("com/google")); - } - - @Test - void testGetDirectoryContents() throws MalformedURLException { + void testGetDirectoryContents() { assertTrue(ResourceUtils.getDirectoryContents(null).isEmpty()); assertTrue(ResourceUtils.getDirectoryContents("idontexist").isEmpty()); assertTrue(ResourceUtils.getDirectoryContents("logback-test.xml").isEmpty()); @@ -305,24 +296,22 @@ class ResourceUtilsTest { Set resultD1 = ResourceUtils.getDirectoryContents("org/onap/policy/common/utils"); assertFalse(resultD1.isEmpty()); - assertEquals("org/onap/policy/common/utils/coder/", normalizePath(resultD1.iterator().next())); + assertEquals("org/onap/policy/common/utils/jackson/", normalizePath(resultD1.iterator().next())); - Set resultD2 = ResourceUtils.getDirectoryContents("org/onap/policy/common/utils/coder"); - assertTrue(resultD2.size() >= 15); - assertEquals("org/onap/policy/common/utils/coder/CoderExceptionTest.class", + Set resultD2 = ResourceUtils.getDirectoryContents("org/onap/policy/common/utils/jackson"); + assertEquals(4, resultD2.size()); + assertEquals("org/onap/policy/common/utils/jackson/JacksonTestUtilsTest$1.class", normalizePath(resultD2.iterator().next())); Set resultJ0 = ResourceUtils.getDirectoryContents("com"); - assertTrue(resultJ0.contains("com/fasterxml/")); - assertEquals("com/fasterxml/", normalizePath(resultJ0.iterator().next())); + assertTrue(resultJ0.contains("com/openpojo/")); + assertEquals("com/openpojo/", normalizePath(resultJ0.iterator().next())); Set resultJ1 = ResourceUtils.getDirectoryContents("com/fasterxml/jackson/core"); assertTrue(resultJ1.size() > 1); assertTrue(resultJ1.contains("com/fasterxml/jackson/core/Base64Variants.class")); - URL dummyUrl = new URL("http://even/worse"); - assertTrue(ResourceUtils.getDirectoryContentsJar(dummyUrl, "nonexistantdirectory").isEmpty()); - + assertTrue(ResourceUtils.getDirectoryContents("nonexistantdirectory").isEmpty()); } /** diff --git a/policy-common/src/test/java/org/onap/policy/common/utils/test/PojoTesterTest.java b/test-utils/src/test/java/org/onap/policy/common/utils/test/PojoTesterTest.java similarity index 100% rename from policy-common/src/test/java/org/onap/policy/common/utils/test/PojoTesterTest.java rename to test-utils/src/test/java/org/onap/policy/common/utils/test/PojoTesterTest.java diff --git a/policy-common/src/test/java/org/onap/policy/common/utils/test/ToStringTesterTest.java b/test-utils/src/test/java/org/onap/policy/common/utils/test/ToStringTesterTest.java similarity index 100% rename from policy-common/src/test/java/org/onap/policy/common/utils/test/ToStringTesterTest.java rename to test-utils/src/test/java/org/onap/policy/common/utils/test/ToStringTesterTest.java diff --git a/policy-common/src/test/java/org/onap/policy/common/utils/test/log/logback/ExtractAppenderTest.java b/test-utils/src/test/java/org/onap/policy/common/utils/test/log/logback/ExtractAppenderTest.java similarity index 100% rename from policy-common/src/test/java/org/onap/policy/common/utils/test/log/logback/ExtractAppenderTest.java rename to test-utils/src/test/java/org/onap/policy/common/utils/test/log/logback/ExtractAppenderTest.java diff --git a/policy-common/src/test/java/org/onap/policy/common/utils/test/pojo/empty/NotAPojo.java b/test-utils/src/test/java/org/onap/policy/common/utils/test/pojo/empty/NotAPojo.java similarity index 100% rename from policy-common/src/test/java/org/onap/policy/common/utils/test/pojo/empty/NotAPojo.java rename to test-utils/src/test/java/org/onap/policy/common/utils/test/pojo/empty/NotAPojo.java diff --git a/policy-common/src/test/java/org/onap/policy/common/utils/test/pojo/invalid/InvalidPojo.java b/test-utils/src/test/java/org/onap/policy/common/utils/test/pojo/invalid/InvalidPojo.java similarity index 100% rename from policy-common/src/test/java/org/onap/policy/common/utils/test/pojo/invalid/InvalidPojo.java rename to test-utils/src/test/java/org/onap/policy/common/utils/test/pojo/invalid/InvalidPojo.java diff --git a/policy-common/src/test/java/org/onap/policy/common/utils/test/pojo/valid/ValidPojo.java b/test-utils/src/test/java/org/onap/policy/common/utils/test/pojo/valid/ValidPojo.java similarity index 100% rename from policy-common/src/test/java/org/onap/policy/common/utils/test/pojo/valid/ValidPojo.java rename to test-utils/src/test/java/org/onap/policy/common/utils/test/pojo/valid/ValidPojo.java diff --git a/test-utils/src/test/resources/org/onap/policy/common/utils/jackson/JacksonTestUtilsTest.json b/test-utils/src/test/resources/org/onap/policy/common/utils/jackson/JacksonTestUtilsTest.json new file mode 100644 index 000000000..43e753700 --- /dev/null +++ b/test-utils/src/test/resources/org/onap/policy/common/utils/jackson/JacksonTestUtilsTest.json @@ -0,0 +1,4 @@ +{ + "id": 500, + "text": "hello" +} diff --git a/policy-common/src/test/resources/testdir/testfile.xml b/test-utils/src/test/resources/testdir/testfile.xml similarity index 100% rename from policy-common/src/test/resources/testdir/testfile.xml rename to test-utils/src/test/resources/testdir/testfile.xml -- 2.16.6