Address items deprecated by move to java 11 08/100208/4
authorJim Hahn <jrh3@att.com>
Fri, 10 Jan 2020 16:30:21 +0000 (11:30 -0500)
committerJim Hahn <jrh3@att.com>
Fri, 10 Jan 2020 19:44:58 +0000 (14:44 -0500)
Addressed the following deprecated items:
- "new Integer(xxx)"
- Observable/Observer
- Mockito.anyObject()

Added assertion to a junit test to address a sonar issue.

Did not address deprecated Nashorn javascript interpreter; that will be
done in a separate review.

Updated licenses.

Issue-ID: POLICY-1406
Signed-off-by: Jim Hahn <jrh3@att.com>
Change-Id: I81fd158f831e01d5410d79898ced658285787d47

integrity-monitor/src/main/java/org/onap/policy/common/im/StateChangeNotifier.java
integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagement.java
integrity-monitor/src/test/java/org/onap/policy/common/im/StateManagementTest.java
utils-test/src/test/java/org/onap/policy/common/utils/gson/GsonTestUtilsTest.java
utils/src/test/java/org/onap/policy/common/utils/validation/TestParameterValidationUtils.java

index a678319..eee3a9e 100644 (file)
@@ -2,14 +2,14 @@
  * ============LICENSE_START=======================================================
  * Integrity Monitor
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017, 2020 AT&T Intellectual Property. 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.
 
 package org.onap.policy.common.im;
 
-import java.util.Observable;
-import java.util.Observer;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-/* 
+/*
  * This is implementing the Observer interface to make it specific for
  * state management.
- * 
- * It saves the StateManagement object and a String message that is 
- * passed in when notifyObservers is called by the Observable 
- * host class. 
- * 
- * It provides an abstract method for handling the state change 
- * so this class must be overwritten and made concrete for the 
- * Observer who is monitoring the state changes. 
+ *
+ * It saves the StateManagement object and a String message that is
+ * passed in when notifyObservers is called by the Observable
+ * host class.
+ *
+ * It provides an abstract method for handling the state change
+ * so this class must be overwritten and made concrete for the
+ * Observer who is monitoring the state changes.
  */
 
 
@@ -46,19 +43,23 @@ import org.slf4j.LoggerFactory;
  * StateManagement class.
  *
  */
-public class StateChangeNotifier implements Observer {
+public class StateChangeNotifier {
     private static final Logger logger = LoggerFactory.getLogger(StateChangeNotifier.class);
     // The observable class
     StateManagement stateManagement;
 
     // A string argument passed by the observable class when
-    // Observable:notifyObservers(Object arg) is called
+    // StateManagement:notifyObservers(String changed) is called
     String message;
 
-    @Override
-    public void update(Observable observable, Object arg) {
-        this.stateManagement = (StateManagement) observable;
-        this.message = (String) arg;
+    /**
+     * Invoked to indicate that something observed by this notifier has changed.
+     * @param observable    item that has changed
+     * @param changed       message indicating what change was made
+     */
+    public void update(StateManagement observable, String changed) {
+        this.stateManagement = observable;
+        this.message = changed;
         handleStateChange();
     }
 
index 68ad3bc..7a018ef 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * Integrity Monitor
  * ================================================================================
- * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2020 AT&T Intellectual Property. 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.
@@ -20,8 +20,9 @@
 
 package org.onap.policy.common.im;
 
+import java.util.Collection;
 import java.util.List;
-import java.util.Observable;
+import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Consumer;
 import javax.persistence.EntityManager;
@@ -43,7 +44,7 @@ import org.slf4j.LoggerFactory;
  * notified.
  *
  */
-public class StateManagement extends Observable {
+public class StateManagement {
     private static final String RESOURCE_NAME = "resource";
     private static final String GET_STATE_MANAGEMENT_ENTITY_QUERY =
             "Select p from StateManagementEntity p where p.resourceName=:" + RESOURCE_NAME;
@@ -96,6 +97,11 @@ public class StateManagement extends Observable {
     private static final Object SYNCLOCK = new Object();
     private static final Object FLUSHLOCK = new Object();
 
+    /**
+     * Observers to be notified when this object changes state.
+     */
+    private final Collection<StateChangeNotifier> observers = new ConcurrentLinkedQueue<>();
+
     /**
      * StateManagement constructor.
      *
@@ -162,7 +168,6 @@ public class StateManagement extends Observable {
                 et.commit();
 
                 if (changed != null) {
-                    setChanged();
                     notifyObservers(changed);
                 }
 
@@ -175,6 +180,20 @@ public class StateManagement extends Observable {
         }
     }
 
+    /**
+     * Adds an observer to list of those to be notified when this changes.
+     * @param observer observer to be added
+     */
+    public void addObserver(StateChangeNotifier observer) {
+        observers.add(observer);
+    }
+
+    private void notifyObservers(String changed) {
+        for (StateChangeNotifier obs : observers) {
+            obs.update(this, changed);
+        }
+    }
+
     private void setStateUsingTable(String actionName, String resourceName, String changeName)
                     throws StateManagementException {
 
index a335b2c..7664244 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * Integrity Monitor
  * ================================================================================
- * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2020 AT&T Intellectual Property. 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.
@@ -251,8 +251,8 @@ public class StateManagementTest extends IntegrityMonitorTestBase {
         final EntityManagerFactory mockedEmf = getMockedEntityManagerFactory(mockedEm);
         final TypedQuery<StateManagementEntity> mockedQuery = mock(TypedQuery.class);
 
-        when(mockedQuery.setFlushMode(Mockito.anyObject())).thenReturn(mockedQuery);
-        when(mockedQuery.setLockMode(Mockito.anyObject())).thenReturn(mockedQuery);
+        when(mockedQuery.setFlushMode(Mockito.any())).thenReturn(mockedQuery);
+        when(mockedQuery.setLockMode(Mockito.any())).thenReturn(mockedQuery);
         when(mockedEm.createQuery(anyString(), any(StateManagementEntity.class.getClass()))).thenReturn(mockedQuery);
 
         doThrow(QueryTimeoutException.class).when(mockedQuery).getResultList();
index 58beb2a..35fea57 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. 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.
@@ -20,6 +20,7 @@
 
 package org.onap.policy.common.utils.gson;
 
+import static org.assertj.core.api.Assertions.assertThatCode;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
@@ -98,7 +99,8 @@ public class GsonTestUtilsTest {
         data.setId(600);
         data.setText(HELLO);
 
-        utils.compareGson(data, "{'id': ${obj.id}, 'text': '${obj.text}'}".replace('\'', '"'));
+        assertThatCode(() -> utils.compareGson(data, "{'id': ${obj.id}, 'text': '${obj.text}'}".replace('\'', '"')))
+                        .doesNotThrowAnyException();
     }
 
     @Test
index d8c4c51..7d46553 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2020 AT&T Intellectual Property. 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.
@@ -42,7 +43,7 @@ public class TestParameterValidationUtils {
     @Test
     public void testValidateIntParameter() {
         assertTrue(ParameterValidationUtils.validateIntParameter(5555));
-        assertTrue(ParameterValidationUtils.validateIntParameter(new Integer(7777)));
+        assertTrue(ParameterValidationUtils.validateIntParameter(Integer.valueOf(7777)));
         assertFalse(ParameterValidationUtils.validateIntParameter(0));
         assertFalse(ParameterValidationUtils.validateIntParameter(-1));
     }
@@ -50,7 +51,7 @@ public class TestParameterValidationUtils {
     @Test
     public void testValidateLongParameter() {
         assertTrue(ParameterValidationUtils.validateLongParameter(5555L));
-        assertTrue(ParameterValidationUtils.validateLongParameter(new Long(7777L)));
+        assertTrue(ParameterValidationUtils.validateLongParameter(Long.valueOf(7777L)));
         assertFalse(ParameterValidationUtils.validateLongParameter(0L));
         assertFalse(ParameterValidationUtils.validateLongParameter(-1L));
     }