Fix for ClassCastException on PfConceptContainer get method usages 69/127269/2
authorRashmi Pujar <rashmi.pujar1@bell.ca>
Thu, 24 Feb 2022 04:40:54 +0000 (23:40 -0500)
committerRashmi Pujar <rashmi.pujar1@bell.ca>
Thu, 24 Feb 2022 15:50:46 +0000 (10:50 -0500)
Typecasting Map to NavigableMap leads to ClassCastException.
The usages of the get methods in delete policy, policyType
endpoints in policy/api (using spring data jpa) cause the
unchecked class-cast exception. Fix is to instead copy the
Map entries into a newly created TreeMap instance.

Issue-ID: POLICY-3924
Signed-off-by: Rashmi Pujar <rashmi.pujar1@bell.ca>
Change-Id: I60625980d6f2692ffa7dd3bd9f53d10b43c13f4a

models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java

index a98a7ac..5f38a39 100644 (file)
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019-2020 Nordix Foundation.
  *  Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ *  Modifications Copyright (C) 2022 Bell Canada. 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.
@@ -331,30 +332,37 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
         if (conceptKey.isNullVersion()) {
             return get(conceptKey.getName());
         } else {
-            return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).get(conceptKey);
+            return new PfConceptGetterImpl<>(getNavigableConceptMap()).get(conceptKey);
         }
     }
 
     @Override
     public C get(final String conceptKeyName) {
-        return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).get(conceptKeyName);
+        return new PfConceptGetterImpl<>(getNavigableConceptMap()).get(conceptKeyName);
     }
 
     @Override
     public C get(final String conceptKeyName, final String conceptKeyVersion) {
-        return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).get(conceptKeyName,
-                conceptKeyVersion);
+        return new PfConceptGetterImpl<>(getNavigableConceptMap()).get(conceptKeyName, conceptKeyVersion);
     }
 
     @Override
     public Set<C> getAll(final String conceptKeyName) {
-        return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).getAll(conceptKeyName);
+        return new PfConceptGetterImpl<>(getNavigableConceptMap()).getAll(conceptKeyName);
     }
 
     @Override
     public Set<C> getAll(final String conceptKeyName, final String conceptKeyVersion) {
-        return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).getAll(conceptKeyName,
-                conceptKeyVersion);
+        return new PfConceptGetterImpl<>(getNavigableConceptMap()).getAll(conceptKeyName, conceptKeyVersion);
+    }
+
+    /**
+     * Get the concept map as a NavigableMap object.
+     *
+     * @return NavigableMap conceptMap instance.
+     */
+    private NavigableMap<PfConceptKey, C> getNavigableConceptMap() {
+        return new TreeMap<>(conceptMap);
     }
 
     /**
@@ -386,4 +394,4 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
                     + keyFieldValue + " does not match the value " + conceptField + " in the concept field");
         }
     }
-}
+}
\ No newline at end of file