Replace Eclipselink with Hibernate 30/133130/2
authorliamfallon <liam.fallon@est.tech>
Thu, 2 Feb 2023 14:46:18 +0000 (14:46 +0000)
committerliamfallon <liam.fallon@est.tech>
Thu, 2 Feb 2023 17:32:43 +0000 (17:32 +0000)
Issue-ID: POLICY-4533
Change-Id: I8ba51dce05f537778a92547bceff6eb6d6099144
Signed-off-by: liamfallon <liam.fallon@est.tech>
main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java
main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java
main/src/test/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandlerTest.java

index 070261d..4d14211 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019-2021 Nordix Foundation.
+ *  Copyright (C) 2019-2021,2023 Nordix Foundation.
  *  Modifications Copyright (C) 2019-2021 AT&T Intellectual Property.
  *  Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved.
  * ================================================================================
@@ -34,7 +34,6 @@ import java.util.Optional;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 import org.apache.commons.lang3.builder.EqualsBuilder;
-import org.eclipse.persistence.exceptions.EclipseLinkException;
 import org.onap.policy.models.base.PfModelException;
 import org.onap.policy.models.pdp.concepts.Pdp;
 import org.onap.policy.models.pdp.concepts.PdpGroup;
@@ -131,7 +130,7 @@ public class PdpStatusMessageHandler extends PdpMessageGenerator {
             } catch (final PolicyPapException exp) {
                 LOGGER.error("Operation Failed", exp);
             } catch (final Exception exp) {
-                if (isDuplicateKeyException(exp)) {
+                if (isDuplicateKeyException(exp, Exception.class)) {
                     /*
                      * this is to be expected, if multiple PAPs are processing the same
                      * heartbeat at a time, thus we log the exception at a trace level
@@ -151,17 +150,17 @@ public class PdpStatusMessageHandler extends PdpMessageGenerator {
      * Determines if the exception indicates a duplicate key.
      *
      * @param thrown exception to check
+     * @param exceptionClazz the class to check against
      * @return {@code true} if the exception occurred due to a duplicate key
      */
-    protected static boolean isDuplicateKeyException(Throwable thrown) {
+    protected static boolean isDuplicateKeyException(Throwable thrown, Class<? extends Throwable> exceptionClazz) {
         while (thrown != null) {
             if (thrown instanceof SQLIntegrityConstraintViolationException) {
                 return true;
             }
 
-            if (thrown instanceof EclipseLinkException) {
-                EclipseLinkException ele = (EclipseLinkException) thrown;
-                if (isDuplicateKeyException(ele.getInternalException())) {
+            if (exceptionClazz.isInstance(thrown)) {
+                if (isDuplicateKeyException(thrown.getCause(), exceptionClazz)) {
                     return true;
                 }
             }
index 19b3c67..825208a 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019, 2022 Nordix Foundation.
+ *  Copyright (C) 2019, 2022-2023 Nordix Foundation.
  *  Modifications Copyright (C) 2019-2021 AT&T Intellectual Property.
  *  Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved.
  * ================================================================================
@@ -137,6 +137,9 @@ public class PapActivator extends ServiceManagerContainer {
 
         // @formatter:off
 
+        // Note: This class is not Thread Safe. If more than one PAP component is started, the code below overwrites
+        //       the parameter service and registry entries.
+
         addAction("Meter Registry",
             () -> Registry.register(PapConstants.REG_METER_REGISTRY, meterRegistry),
             () -> Registry.unregister(PapConstants.REG_METER_REGISTRY));
index 5129bf5..f3ba51a 100644 (file)
@@ -3,6 +3,7 @@
  * ONAP
  * ================================================================================
  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2023 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -23,8 +24,17 @@ package org.onap.policy.pap.main.comm;
 import static org.assertj.core.api.Assertions.assertThat;
 
 import java.sql.SQLIntegrityConstraintViolationException;
-import org.eclipse.persistence.exceptions.EclipseLinkException;
+import org.hibernate.HibernateException;
 import org.junit.Test;
+import org.onap.policy.pap.main.PolicyPapApplication;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest(
+    classes = PolicyPapApplication.class,
+    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
+    properties = {
+        "db.initialize=false"
+    })
 
 public class PdpStatusMessageHandlerTest {
 
@@ -34,68 +44,68 @@ public class PdpStatusMessageHandlerTest {
         // @formatter:off
 
         // null exception
-        assertThat(PdpStatusMessageHandler.isDuplicateKeyException(null)).isFalse();
+        assertThat(PdpStatusMessageHandler.isDuplicateKeyException(null, HibernateException.class)).isFalse();
 
         // plain exception
         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
-                        new Exception()))
+                        new Exception(), HibernateException.class))
             .isFalse();
 
         // cause is also plain
         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
                         new Exception(
-                            new Exception())))
+                            new Exception()), HibernateException.class))
             .isFalse();
 
         // dup key
         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
-                        new SQLIntegrityConstraintViolationException()))
+                        new SQLIntegrityConstraintViolationException(), HibernateException.class))
             .isTrue();
 
         // cause is dup key
         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
                         new Exception(
-                            new SQLIntegrityConstraintViolationException())))
+                            new SQLIntegrityConstraintViolationException()), HibernateException.class))
             .isTrue();
 
         // eclipselink exception, no internal exception
         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
-                        new MyEclipseLinkException()))
+                        new MyHibernateException(), HibernateException.class))
             .isFalse();
 
         // eclipselink exception, cause is plain
         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
-                        new MyEclipseLinkException(
-                            new Exception())))
+                        new MyHibernateException(
+                            new Exception()), HibernateException.class))
             .isFalse();
 
         // eclipselink exception, cause is dup
         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
-                        new MyEclipseLinkException(
-                            new SQLIntegrityConstraintViolationException())))
+                        new MyHibernateException(
+                            new SQLIntegrityConstraintViolationException()), HibernateException.class))
             .isTrue();
 
         // multiple cause both inside and outside of the eclipselink exception
         assertThat(PdpStatusMessageHandler.isDuplicateKeyException(
                         new Exception(
                             new Exception(
-                                new MyEclipseLinkException(
+                                new MyHibernateException(
                                     new Exception(
-                                        new SQLIntegrityConstraintViolationException()))))))
+                                        new SQLIntegrityConstraintViolationException())))), HibernateException.class))
             .isTrue();
 
         // @formatter:on
     }
 
-    public static class MyEclipseLinkException extends EclipseLinkException {
+    public static class MyHibernateException extends HibernateException {
         private static final long serialVersionUID = 1L;
 
-        public MyEclipseLinkException() {
-            // do nothing
+        public MyHibernateException() {
+            super("");
         }
 
-        public MyEclipseLinkException(Exception exception) {
-            setInternalException(exception);
+        public MyHibernateException(Exception exception) {
+            super(exception);
         }
     }
 }