diff options
3 files changed, 27 insertions, 8 deletions
diff --git a/main/pom.xml b/main/pom.xml index 4804e27d..e00fe71c 100644 --- a/main/pom.xml +++ b/main/pom.xml @@ -185,6 +185,11 @@ <scope>runtime</scope> </dependency> <dependency> + <groupId>org.hibernate.validator</groupId> + <artifactId>hibernate-validator</artifactId> + <scope>runtime</scope> + </dependency> + <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> diff --git a/main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java b/main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java index edff3004..2be1813c 100644 --- a/main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java +++ b/main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java @@ -3,7 +3,7 @@ * ONAP Policy API * ================================================================================ * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019-2021, 2023 Nordix Foundation. + * Modifications Copyright (C) 2019-2021, 2023, 2025 Nordix Foundation. * Modifications Copyright (C) 2022 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -143,10 +143,7 @@ public class ApiDatabaseInitializer { // Consolidate policies var topologyTemplate = singleEntity.getToscaTopologyTemplate(); if (topologyTemplate != null && topologyTemplate.getPolicies() != null) { - serviceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate()); - serviceTemplate.getToscaTopologyTemplate().setPolicies(new LinkedList<>()); - serviceTemplate.getToscaTopologyTemplate().getPolicies() - .addAll(singleEntity.getToscaTopologyTemplate().getPolicies()); + consolidatePolicies(serviceTemplate, topologyTemplate); } } // Preload the specified entities @@ -165,6 +162,24 @@ public class ApiDatabaseInitializer { return createdServiceTemplate; } + /** + * Validates the topology template to have policies and add them to the final service template. + * @param serviceTemplate the service template to be sent to database + * @param topologyTemplate the topology template containing the policies + */ + private static void consolidatePolicies(ToscaServiceTemplate serviceTemplate, + ToscaTopologyTemplate topologyTemplate) { + if (serviceTemplate.getToscaTopologyTemplate() == null) { + serviceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate()); + } + + if (serviceTemplate.getToscaTopologyTemplate().getPolicies() == null) { + serviceTemplate.getToscaTopologyTemplate().setPolicies(new LinkedList<>()); + } + serviceTemplate.getToscaTopologyTemplate().getPolicies() + .addAll(topologyTemplate.getPolicies()); + } + private ToscaServiceTemplate deserializeServiceTemplate(String entity) throws PolicyApiException, CoderException { var entityAsStringYaml = ResourceUtils.getResourceAsString(entity); if (entityAsStringYaml == null) { diff --git a/main/src/test/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializerExceptionsTest.java b/main/src/test/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializerExceptionsTest.java index 8f6f7aea..3241c93c 100644 --- a/main/src/test/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializerExceptionsTest.java +++ b/main/src/test/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializerExceptionsTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2024 Nordix Foundation. All rights reserved. + * Copyright (C) 2024-2025 Nordix Foundation. 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. @@ -22,7 +22,6 @@ package org.onap.policy.api.main.startstop; import static org.mockito.ArgumentMatchers.any; -import io.netty.handler.codec.CodecException; import java.util.HashMap; import java.util.List; import org.junit.jupiter.api.Assertions; @@ -54,7 +53,7 @@ class ApiDatabaseInitializerExceptionsTest { var mockYamlCoder = Mockito.mock(StandardYamlCoder.class); Mockito.when(mockYamlCoder.decode((String) any(), any())) - .thenThrow(new CodecException("fail")); + .thenThrow(new CoderException("fail")); var databaseService = new ApiDatabaseInitializer(mockServiceTemplate, mockPolicyPreload); Assertions.assertThrows(PolicyApiException.class, databaseService::loadData); |