Fix preloading more than one policy 19/140319/2
authoradheli.tavares <adheli.tavares@est.tech>
Thu, 27 Feb 2025 09:29:07 +0000 (09:29 +0000)
committeradheli.tavares <adheli.tavares@est.tech>
Thu, 27 Feb 2025 09:58:08 +0000 (09:58 +0000)
- from a list of policies to predeploy, only the last one was
being saved to db
- hibernate validator was missing and throwing a warning

Issue-ID: POLICY-5297
Change-Id: Ia9df1d7dad305e5f42444bf18c9a936b5f2bb381
Signed-off-by: adheli.tavares <adheli.tavares@est.tech>
main/pom.xml
main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java
main/src/test/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializerExceptionsTest.java

index 4804e27..e00fe71 100644 (file)
             <artifactId>hibernate-core</artifactId>
             <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>
index edff300..2be1813 100644 (file)
@@ -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) {
index 8f6f7ae..3241c93 100644 (file)
@@ -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);