From 888acd24e8852bdbda0eb7ab0b4992f710a4793a Mon Sep 17 00:00:00 2001 From: danielhanrahan Date: Tue, 17 Feb 2026 15:50:40 +0000 Subject: [PATCH] Fix Pojo tests There are a few tests using OpenPojo library, but many are broken: - ToStringTester uses wrong regular expression, and has no tests - Some tests incorrectly filter the packages, e.g. only including test files instead of excluding them. This commit introduces PojoTester with its own tests, and also adds unit tests of ToStringTester. Issue-ID: POLICY-5546 Change-Id: I94a8ea723f98e13bc14f4de9e82f1148234144ef Signed-off-by: danielhanrahan --- .../kafka/participant/ParticipantMessage.java | 6 +- .../acm/persistence/concepts/JpaMessage.java | 2 +- ...ssage.java => StringToDocMessageConverter.java} | 2 +- .../onap/policy/clamp/models/acm/PojosTest.java | 53 ++++++++++++++++++ .../AutomationCompositionConceptPojosTest.java | 62 --------------------- .../kafka/participant/ParticipantMessageTest.java | 26 ++++++--- .../kafka/participant/ParticipantPojosTest.java | 62 --------------------- .../acm/messages/rest/MessagesRestPojosTest.java | 59 -------------------- ...t.java => StringToDocMessageConverterTest.java} | 6 +- .../handler/ParticipantHandlerTest.java | 5 +- .../onap/policy/common/utils/test/PojoTester.java | 47 +++++++++++----- .../policy/common/utils/test/ToStringTester.java | 10 ++-- .../policy/common/utils/test/PojoTesterTest.java | 51 +++++++++++++++++ .../common/utils/test/ToStringTesterTest.java | 53 ++++++++++++++++++ .../common/utils/test/pojo/empty/NotAPojo.java | 25 +++++++++ .../utils/test/pojo/invalid/InvalidPojo.java | 25 +++++++++ .../common/utils/test/pojo/valid/ValidPojo.java | 29 ++++++++++ .../java/org/onap/policy/models/PojosTest.java | 40 +++++++++++++ .../policy/models/errors/concepts/TestModels.java | 42 -------------- .../tosca/authorative/concepts/PojosTest.java | 65 ---------------------- .../concepts/ToscaIdentifierTestBase.java | 2 +- 21 files changed, 342 insertions(+), 330 deletions(-) rename models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/{StringToDocMessage.java => StringToDocMessageConverter.java} (92%) create mode 100644 models/src/test/java/org/onap/policy/clamp/models/acm/PojosTest.java delete mode 100644 models/src/test/java/org/onap/policy/clamp/models/acm/concepts/AutomationCompositionConceptPojosTest.java delete mode 100644 models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/ParticipantPojosTest.java delete mode 100644 models/src/test/java/org/onap/policy/clamp/models/acm/messages/rest/MessagesRestPojosTest.java rename models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/{StringToDocMessageTest.java => StringToDocMessageConverterTest.java} (93%) rename models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/PojosTest.java => policy-common/src/main/java/org/onap/policy/common/utils/test/PojoTester.java (55%) create mode 100644 policy-common/src/test/java/org/onap/policy/common/utils/test/PojoTesterTest.java create mode 100644 policy-common/src/test/java/org/onap/policy/common/utils/test/ToStringTesterTest.java create mode 100644 policy-common/src/test/java/org/onap/policy/common/utils/test/pojo/empty/NotAPojo.java create mode 100644 policy-common/src/test/java/org/onap/policy/common/utils/test/pojo/invalid/InvalidPojo.java create mode 100644 policy-common/src/test/java/org/onap/policy/common/utils/test/pojo/valid/ValidPojo.java create mode 100644 policy-models/src/test/java/org/onap/policy/models/PojosTest.java delete mode 100644 policy-models/src/test/java/org/onap/policy/models/errors/concepts/TestModels.java delete mode 100644 policy-models/src/test/java/org/onap/policy/models/tosca/authorative/concepts/PojosTest.java diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/ParticipantMessage.java b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/ParticipantMessage.java index d55a610c0..5d19631c3 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/ParticipantMessage.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/ParticipantMessage.java @@ -40,7 +40,7 @@ import lombok.ToString; @Setter @ToString @NoArgsConstructor -public class ParticipantMessage { +public abstract class ParticipantMessage { @Setter(AccessLevel.NONE) private ParticipantMessageType messageType; @@ -79,7 +79,7 @@ public class ParticipantMessage { * * @param messageType the message type */ - public ParticipantMessage(final ParticipantMessageType messageType) { + protected ParticipantMessage(final ParticipantMessageType messageType) { this.messageType = messageType; } @@ -88,7 +88,7 @@ public class ParticipantMessage { * * @param source source from which to copy */ - public ParticipantMessage(final ParticipantMessage source) { + protected ParticipantMessage(final ParticipantMessage source) { this.messageId = source.messageId; this.timestamp = source.timestamp; this.messageType = source.messageType; diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaMessage.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaMessage.java index 86a7c889b..2eed79f5a 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaMessage.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaMessage.java @@ -57,7 +57,7 @@ public class JpaMessage implements PfAuthorative { private Timestamp lastMsg = TimestampHelper.nowTimestamp(); @Column(length = 100000) - @Convert(converter = StringToDocMessage.class) + @Convert(converter = StringToDocMessageConverter.class) @NotNull private DocMessage docMessage; diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/StringToDocMessage.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/StringToDocMessageConverter.java similarity index 92% rename from models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/StringToDocMessage.java rename to models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/StringToDocMessageConverter.java index 66aff1e0f..bfaa00453 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/StringToDocMessage.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/StringToDocMessageConverter.java @@ -25,7 +25,7 @@ import jakarta.persistence.Converter; import org.onap.policy.clamp.models.acm.document.concepts.DocMessage; @Converter(autoApply = true) -public class StringToDocMessage extends AbstractConverter implements AttributeConverter { +public class StringToDocMessageConverter extends AbstractConverter implements AttributeConverter { @Override public String convertToDatabaseColumn(DocMessage docMessage) { diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/PojosTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/PojosTest.java new file mode 100644 index 000000000..a1aa2f7a5 --- /dev/null +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/PojosTest.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021-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. + * 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.clamp.models.acm; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +import org.junit.jupiter.api.Test; +import org.onap.policy.common.utils.test.PojoTester; + +/** + * Class to perform unit tests of all pojos. + */ +class PojosTest { + + @Test + void testConceptsPojos() { + assertDoesNotThrow(() -> PojoTester.testPojos(getClass().getPackageName() + ".concepts")); + } + + @Test + void testDocumentPojos() { + assertDoesNotThrow(() -> PojoTester.testPojos(getClass().getPackageName() + ".document.concepts")); + } + + @Test + void testPersistencePojos() { + assertDoesNotThrow(() -> PojoTester.testPojos(getClass().getPackageName() + ".persistence.concepts")); + } + + @Test + void testMessagesPojos() { + assertDoesNotThrow(() -> PojoTester.testPojos(getClass().getPackageName() + ".messages")); + } + +} diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/concepts/AutomationCompositionConceptPojosTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/concepts/AutomationCompositionConceptPojosTest.java deleted file mode 100644 index 1d45e5919..000000000 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/concepts/AutomationCompositionConceptPojosTest.java +++ /dev/null @@ -1,62 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 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.clamp.models.acm.concepts; - -import com.openpojo.reflection.PojoClass; -import com.openpojo.reflection.impl.PojoClassFactory; -import com.openpojo.validation.Validator; -import com.openpojo.validation.ValidatorBuilder; -import com.openpojo.validation.rule.impl.EqualsAndHashCodeMatchRule; -import com.openpojo.validation.rule.impl.GetterMustExistRule; -import com.openpojo.validation.rule.impl.NoPublicFieldsExceptStaticFinalRule; -import com.openpojo.validation.rule.impl.SetterMustExistRule; -import com.openpojo.validation.test.impl.GetterTester; -import com.openpojo.validation.test.impl.SetterTester; -import java.util.List; -import org.junit.jupiter.api.Test; -import org.onap.policy.common.utils.test.ToStringTester; - -/** - * Class to perform unit tests of all pojos. - */ -class AutomationCompositionConceptPojosTest { - - @Test - void testPojos() { - List pojoClasses = - PojoClassFactory.getPojoClasses(AutomationCompositionConceptPojosTest.class.getPackageName()); - - // @formatter:off - final Validator validator = ValidatorBuilder - .create() - .with(new SetterMustExistRule()) - .with(new GetterMustExistRule()) - .with(new EqualsAndHashCodeMatchRule()) - .with(new NoPublicFieldsExceptStaticFinalRule()) - .with(new SetterTester()) - .with(new GetterTester()) - .with(new ToStringTester()) - .build(); - - validator.validate(pojoClasses); - // @formatter:on - } -} diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/ParticipantMessageTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/ParticipantMessageTest.java index 2fbfdf750..7dfe63f60 100644 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/ParticipantMessageTest.java +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/ParticipantMessageTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved. + * Copyright (C) 2021-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. @@ -37,24 +37,24 @@ class ParticipantMessageTest { @Test void testCopyConstructor() throws CoderException { - assertThatThrownBy(() -> new ParticipantMessage((ParticipantMessage) null)) + assertThatThrownBy(() -> new TestParticipantMessage(null)) .isInstanceOf(NullPointerException.class); // verify with null values - var message = new ParticipantMessage(ParticipantMessageType.PARTICIPANT_STATE_CHANGE); - var newmsg = new ParticipantMessage(message); + var message = new TestParticipantMessage(); + var newmsg = new TestParticipantMessage(message); newmsg.setMessageId(message.getMessageId()); newmsg.setTimestamp(message.getTimestamp()); assertEquals(message.toString(), newmsg.toString()); // verify with all values message = makeMessage(); - newmsg = new ParticipantMessage(message); + newmsg = new TestParticipantMessage(message); newmsg.setMessageId(message.getMessageId()); newmsg.setTimestamp(message.getTimestamp()); assertEquals(message.toString(), newmsg.toString()); - assertSerializable(message, ParticipantMessage.class); + assertSerializable(message, TestParticipantMessage.class); } @Test @@ -100,8 +100,8 @@ class ParticipantMessageTest { assertFalse(message.appliesTo(CommonTestData.getParticipantId(), CommonTestData.getReplicaId())); } - private ParticipantMessage makeMessage() { - var msg = new ParticipantMessage(ParticipantMessageType.PARTICIPANT_STATE_CHANGE); + private TestParticipantMessage makeMessage() { + var msg = new TestParticipantMessage(); msg.setParticipantId(CommonTestData.getParticipantId()); msg.setMessageId(UUID.randomUUID()); @@ -109,4 +109,14 @@ class ParticipantMessageTest { return msg; } + + private static class TestParticipantMessage extends ParticipantMessage { + public TestParticipantMessage() { + super(ParticipantMessageType.PARTICIPANT_STATE_CHANGE); + } + + public TestParticipantMessage(ParticipantMessage source) { + super(source); + } + } } diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/ParticipantPojosTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/ParticipantPojosTest.java deleted file mode 100644 index 2683b3fb5..000000000 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/ParticipantPojosTest.java +++ /dev/null @@ -1,62 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021-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.clamp.models.acm.messages.kafka.participant; - -import com.openpojo.reflection.impl.PojoClassFactory; -import com.openpojo.validation.ValidatorBuilder; -import com.openpojo.validation.rule.impl.GetterMustExistRule; -import com.openpojo.validation.rule.impl.SetterMustExistRule; -import com.openpojo.validation.test.impl.GetterTester; -import com.openpojo.validation.test.impl.SetterTester; -import org.junit.jupiter.api.Test; -import org.onap.policy.common.utils.test.ToStringTester; - -/** - * Class to perform unit tests of all pojos. - */ -class ParticipantPojosTest { - - @Test - void testPojos() { - var pojoClasses = - PojoClassFactory.getPojoClasses(ParticipantPojosTest.class.getPackageName()); - - pojoClasses.remove(PojoClassFactory.getPojoClass(ParticipantMessage.class)); - pojoClasses.remove(PojoClassFactory.getPojoClass(ParticipantMessageTest.class)); - pojoClasses.remove(PojoClassFactory.getPojoClass(ParticipantAckMessage.class)); - pojoClasses.remove(PojoClassFactory.getPojoClass(ParticipantAckMessageTest.class)); - pojoClasses.remove(PojoClassFactory.getPojoClass(AutomationCompositionDeployAck.class)); - pojoClasses.remove(PojoClassFactory.getPojoClass(AutomationCompositionDeployAckTest.class)); - - // @formatter:off - final var validator = ValidatorBuilder - .create() - .with(new ToStringTester()) - .with(new SetterMustExistRule()) - .with(new GetterMustExistRule()) - .with(new SetterTester()) - .with(new GetterTester()) - .build(); - - validator.validate(pojoClasses); - // @formatter:on - } -} diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/messages/rest/MessagesRestPojosTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/messages/rest/MessagesRestPojosTest.java deleted file mode 100644 index 4503be783..000000000 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/messages/rest/MessagesRestPojosTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 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.clamp.models.acm.messages.rest; - -import com.openpojo.reflection.PojoClass; -import com.openpojo.reflection.impl.PojoClassFactory; -import com.openpojo.validation.Validator; -import com.openpojo.validation.ValidatorBuilder; -import com.openpojo.validation.rule.impl.GetterMustExistRule; -import com.openpojo.validation.rule.impl.SetterMustExistRule; -import com.openpojo.validation.test.impl.GetterTester; -import com.openpojo.validation.test.impl.SetterTester; -import java.util.List; -import org.junit.jupiter.api.Test; -import org.onap.policy.common.utils.test.ToStringTester; - -/** - * Class to perform unit tests of all pojos. - */ -class MessagesRestPojosTest { - - @Test - void testPojos() { - List pojoClasses = - PojoClassFactory.getPojoClassesRecursively(MessagesRestPojosTest.class.getPackageName(), null); - - - // @formatter:off - final Validator validator = ValidatorBuilder - .create() - .with(new ToStringTester()) - .with(new SetterMustExistRule()) - .with(new GetterMustExistRule()) - .with(new SetterTester()) - .with(new GetterTester()) - .build(); - - validator.validate(pojoClasses); - // @formatter:on - } -} diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/StringToDocMessageTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/StringToDocMessageConverterTest.java similarity index 93% rename from models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/StringToDocMessageTest.java rename to models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/StringToDocMessageConverterTest.java index 84dc3409d..18cbaa8a9 100644 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/StringToDocMessageTest.java +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/StringToDocMessageConverterTest.java @@ -30,11 +30,11 @@ import org.onap.policy.clamp.models.acm.document.concepts.DocMessage; import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageType; import org.onap.policy.models.base.PfModelRuntimeException; -class StringToDocMessageTest { +class StringToDocMessageConverterTest { @Test void testConvert() { - var stringToDocMessage = new StringToDocMessage(); + var stringToDocMessage = new StringToDocMessageConverter(); var docMessage = new DocMessage(); docMessage.setMessageId(UUID.randomUUID().toString()); docMessage.setInstanceId(UUID.randomUUID()); @@ -48,7 +48,7 @@ class StringToDocMessageTest { @Test void testNull() { - var stringToDocMessage = new StringToDocMessage(); + var stringToDocMessage = new StringToDocMessageConverter(); var dbData = stringToDocMessage.convertToDatabaseColumn(null); assertThat(dbData).isNull(); var map = stringToDocMessage.convertToEntityAttribute(null); diff --git a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/handler/ParticipantHandlerTest.java b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/handler/ParticipantHandlerTest.java index e2f2e8abd..1eca39f1b 100644 --- a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/handler/ParticipantHandlerTest.java +++ b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/handler/ParticipantHandlerTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved. + * Copyright (C) 2021-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. @@ -49,7 +49,6 @@ import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCom import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantAckMessage; import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregister; import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantDeregisterAck; -import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessage; import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageType; import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrime; import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRegister; @@ -306,7 +305,7 @@ class ParticipantHandlerTest { var participantAckMsg = new ParticipantAckMessage(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY); assertTrue(participantHandler.appliesTo(participantAckMsg)); - var participantMsg = new ParticipantMessage(ParticipantMessageType.PARTICIPANT_STATUS); + var participantMsg = new ParticipantStatus(); assertTrue(participantHandler.appliesTo(participantMsg)); participantMsg.setParticipantId(UUID.randomUUID()); diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/PojosTest.java b/policy-common/src/main/java/org/onap/policy/common/utils/test/PojoTester.java similarity index 55% rename from models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/PojosTest.java rename to policy-common/src/main/java/org/onap/policy/common/utils/test/PojoTester.java index 1f40fae5f..e3dd0f935 100644 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/PojosTest.java +++ b/policy-common/src/main/java/org/onap/policy/common/utils/test/PojoTester.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. + * Copyright (C) 2021-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. @@ -18,9 +18,9 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.clamp.models.acm.persistence.concepts; +package org.onap.policy.common.utils.test; -import com.openpojo.reflection.PojoClass; +import com.openpojo.reflection.filters.FilterNonConcrete; import com.openpojo.reflection.impl.PojoClassFactory; import com.openpojo.validation.Validator; import com.openpojo.validation.ValidatorBuilder; @@ -30,21 +30,40 @@ import com.openpojo.validation.rule.impl.NoPublicFieldsExceptStaticFinalRule; import com.openpojo.validation.rule.impl.SetterMustExistRule; import com.openpojo.validation.test.impl.GetterTester; import com.openpojo.validation.test.impl.SetterTester; -import java.util.List; -import org.junit.jupiter.api.Test; -import org.onap.policy.common.utils.test.ToStringTester; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; /** - * Class to perform unit tests of all pojos. + * Utility class for testing POJO classes using OpenPojo validation framework. + * Validates getter/setter methods, equals/hashCode contracts, and other POJO conventions. */ -class PojosTest { +@Slf4j +@NoArgsConstructor(access = lombok.AccessLevel.PRIVATE) +public class PojoTester { - @Test - void testPojos() { - List pojoClasses = - PojoClassFactory.getPojoClasses(PojosTest.class.getPackageName()); + /** + * Tests all POJOs in the specified package using default exclusion pattern. + * + * @param packageName the package to scan for POJO classes + */ + public static void testPojos(String packageName) { + testPojos(packageName, ".*(Test|Utils|Converter|Comparator)$"); + } + + /** + * Tests all POJOs in the specified package, excluding classes matching the pattern. + * + * @param packageName the package to scan for POJO classes + * @param excludePattern regex pattern for class names to exclude + */ + public static void testPojos(String packageName, String excludePattern) { + var pojoClasses = PojoClassFactory.getPojoClassesRecursively(packageName, new FilterNonConcrete()); + pojoClasses.removeIf(clazz -> clazz.getName().matches(excludePattern)); + if (pojoClasses.isEmpty()) { + throw new IllegalArgumentException("No POJO classes found in package: " + packageName); + } + pojoClasses.forEach(clazz -> log.info("Testing class: {}", clazz.getName())); - // @formatter:off final Validator validator = ValidatorBuilder .create() .with(new SetterMustExistRule()) @@ -55,8 +74,6 @@ class PojosTest { .with(new GetterTester()) .with(new ToStringTester()) .build(); - validator.validate(pojoClasses); - // @formatter:on } } diff --git a/policy-common/src/main/java/org/onap/policy/common/utils/test/ToStringTester.java b/policy-common/src/main/java/org/onap/policy/common/utils/test/ToStringTester.java index ab09291c2..f81944b03 100644 --- a/policy-common/src/main/java/org/onap/policy/common/utils/test/ToStringTester.java +++ b/policy-common/src/main/java/org/onap/policy/common/utils/test/ToStringTester.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-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. @@ -29,10 +29,10 @@ import com.openpojo.reflection.PojoClass; import com.openpojo.validation.affirm.Affirm; import com.openpojo.validation.test.Tester; import com.openpojo.validation.utils.ValidationHelper; +import java.util.regex.Pattern; import lombok.AllArgsConstructor; import org.hamcrest.Matcher; - /** * Class to provide toString testing utility for testing pojo classes. * @@ -47,15 +47,15 @@ public class ToStringTester implements Tester { matcher = anything(); } - @SuppressWarnings("unchecked") @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+")); + Affirm.affirmFalse("Class [" + clazz.getName() + + "] uses default Object.toString() - please implement a proper toString() method", + classInstance.toString().matches(Pattern.quote(clazz.getName()) + "@[0-9a-fA-F]+")); } } diff --git a/policy-common/src/test/java/org/onap/policy/common/utils/test/PojoTesterTest.java b/policy-common/src/test/java/org/onap/policy/common/utils/test/PojoTesterTest.java new file mode 100644 index 000000000..616a3ee7b --- /dev/null +++ b/policy-common/src/test/java/org/onap/policy/common/utils/test/PojoTesterTest.java @@ -0,0 +1,51 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +class PojoTesterTest { + + private static final String BASE_PACKAGE = PojoTesterTest.class.getPackageName(); + + @Test + void testValidPojo() { + assertDoesNotThrow(() -> PojoTester.testPojos(BASE_PACKAGE + ".pojo.valid")); + } + + @Test + void testInvalidPojo() { + assertThrows(AssertionError.class, () -> PojoTester.testPojos(BASE_PACKAGE + ".pojo.invalid")); + } + + @Test + void testEmptyPackage() { + assertThrows(Exception.class, () -> PojoTester.testPojos(BASE_PACKAGE + ".pojo.empty")); + } + + @Test + void testNonExistingPackage() { + assertThrows(Exception.class, () -> PojoTester.testPojos(BASE_PACKAGE + ".pojo.nonexistent")); + } +} diff --git a/policy-common/src/test/java/org/onap/policy/common/utils/test/ToStringTesterTest.java b/policy-common/src/test/java/org/onap/policy/common/utils/test/ToStringTesterTest.java new file mode 100644 index 000000000..3c7becc8c --- /dev/null +++ b/policy-common/src/test/java/org/onap/policy/common/utils/test/ToStringTesterTest.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import com.openpojo.reflection.impl.PojoClassFactory; +import lombok.ToString; +import org.junit.jupiter.api.Test; + +class ToStringTesterTest { + + @Test + void testGoodToString() { + final ToStringTester tester = new ToStringTester(); + final var pojoClass = PojoClassFactory.getPojoClass(GoodToStringClass.class); + assertDoesNotThrow(() -> tester.run(pojoClass)); + } + + @Test + void testDefaultToString() { + final ToStringTester tester = new ToStringTester(); + final var pojoClass = PojoClassFactory.getPojoClass(DefaultToStringClass.class); + assertThrows(AssertionError.class, () -> tester.run(pojoClass)); + } + + @ToString + static class GoodToStringClass { + String myField; + } + + static class DefaultToStringClass { + } +} diff --git a/policy-common/src/test/java/org/onap/policy/common/utils/test/pojo/empty/NotAPojo.java b/policy-common/src/test/java/org/onap/policy/common/utils/test/pojo/empty/NotAPojo.java new file mode 100644 index 000000000..25fa0246a --- /dev/null +++ b/policy-common/src/test/java/org/onap/policy/common/utils/test/pojo/empty/NotAPojo.java @@ -0,0 +1,25 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.test.pojo.empty; + +public enum NotAPojo { + DUMMY +} diff --git a/policy-common/src/test/java/org/onap/policy/common/utils/test/pojo/invalid/InvalidPojo.java b/policy-common/src/test/java/org/onap/policy/common/utils/test/pojo/invalid/InvalidPojo.java new file mode 100644 index 000000000..dbdd10c26 --- /dev/null +++ b/policy-common/src/test/java/org/onap/policy/common/utils/test/pojo/invalid/InvalidPojo.java @@ -0,0 +1,25 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.test.pojo.invalid; + +public class InvalidPojo { + public String name; // Public field violates rule +} diff --git a/policy-common/src/test/java/org/onap/policy/common/utils/test/pojo/valid/ValidPojo.java b/policy-common/src/test/java/org/onap/policy/common/utils/test/pojo/valid/ValidPojo.java new file mode 100644 index 000000000..feb7ab571 --- /dev/null +++ b/policy-common/src/test/java/org/onap/policy/common/utils/test/pojo/valid/ValidPojo.java @@ -0,0 +1,29 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.test.pojo.valid; + +import lombok.Data; + +@Data +public class ValidPojo { + private String name; + private int value; +} diff --git a/policy-models/src/test/java/org/onap/policy/models/PojosTest.java b/policy-models/src/test/java/org/onap/policy/models/PojosTest.java new file mode 100644 index 000000000..65539e606 --- /dev/null +++ b/policy-models/src/test/java/org/onap/policy/models/PojosTest.java @@ -0,0 +1,40 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.models; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +import org.junit.jupiter.api.Test; +import org.onap.policy.common.utils.test.PojoTester; + +class PojosTest { + + @Test + void testErrorConceptsPojos() { + assertDoesNotThrow(() -> PojoTester.testPojos(getClass().getPackageName() + ".errors.concepts")); + } + + @Test + void testToscaConceptsPojos() { + assertDoesNotThrow(() -> PojoTester.testPojos(getClass().getPackageName() + ".tosca.authorative.concepts")); + } + +} diff --git a/policy-models/src/test/java/org/onap/policy/models/errors/concepts/TestModels.java b/policy-models/src/test/java/org/onap/policy/models/errors/concepts/TestModels.java deleted file mode 100644 index 4914cc65f..000000000 --- a/policy-models/src/test/java/org/onap/policy/models/errors/concepts/TestModels.java +++ /dev/null @@ -1,42 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Decision Models - * ================================================================================ - * Copyright (C) 2019 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.errors.concepts; - -import com.openpojo.reflection.filters.FilterPackageInfo; -import com.openpojo.validation.Validator; -import com.openpojo.validation.ValidatorBuilder; -import com.openpojo.validation.test.impl.GetterTester; -import com.openpojo.validation.test.impl.SetterTester; -import org.junit.jupiter.api.Test; -import org.onap.policy.common.utils.test.ToStringTester; - -class TestModels { - - @Test - void testDecisionModels() { - final Validator validator = ValidatorBuilder.create().with(new ToStringTester()).with(new SetterTester()) - .with(new GetterTester()).build(); - validator.validate(TestModels.class.getPackage().getName(), new FilterPackageInfo()); - } - - -} diff --git a/policy-models/src/test/java/org/onap/policy/models/tosca/authorative/concepts/PojosTest.java b/policy-models/src/test/java/org/onap/policy/models/tosca/authorative/concepts/PojosTest.java deleted file mode 100644 index 1c7367650..000000000 --- a/policy-models/src/test/java/org/onap/policy/models/tosca/authorative/concepts/PojosTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Model - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2021-2025 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. - * 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.models.tosca.authorative.concepts; - -import com.openpojo.reflection.filters.FilterClassName; -import com.openpojo.reflection.filters.FilterPackageInfo; -import com.openpojo.validation.Validator; -import com.openpojo.validation.ValidatorBuilder; -import com.openpojo.validation.rule.impl.GetterMustExistRule; -import com.openpojo.validation.rule.impl.SetterMustExistRule; -import com.openpojo.validation.test.impl.GetterTester; -import com.openpojo.validation.test.impl.SetterTester; -import org.junit.jupiter.api.Test; -import org.onap.policy.common.utils.test.ToStringTester; - -/** - * Class to perform unit tests of all pojos. - * - * @author Chenfei Gao (cgao@research.att.com) - * - */ -class PojosTest { - - private static final String POJO_PACKAGE = "org.onap.policy.models.tosca.authorative.concepts"; - - @Test - void testPojos() { - // @formatter:off - final Validator validator = ValidatorBuilder - .create() - .with(new ToStringTester()) - .with(new SetterMustExistRule()) - .with(new GetterMustExistRule()) - .with(new SetterTester()) - .with(new GetterTester()) - .build(); - validator.validate(POJO_PACKAGE, - new FilterPackageInfo(), - new FilterClassName(ToscaIdentifierTestBase.class.getName()), - new FilterClassName(".*Test$") - ); - // @formatter:on - } -} diff --git a/policy-models/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaIdentifierTestBase.java b/policy-models/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaIdentifierTestBase.java index ebd31469e..2309c63b4 100644 --- a/policy-models/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaIdentifierTestBase.java +++ b/policy-models/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaIdentifierTestBase.java @@ -33,7 +33,7 @@ import org.onap.policy.common.utils.coder.StandardCoder; * * @param type of key being tested */ -class ToscaIdentifierTestBase> { +abstract class ToscaIdentifierTestBase> { public static final String NAME = "my-name"; public static final String VERSION = "1.2.3"; -- 2.16.6