Clean up mdsal-eos-common-api 37/109237/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 8 Dec 2023 21:45:00 +0000 (22:45 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 8 Dec 2023 21:45:26 +0000 (22:45 +0100)
Modernize tests and classes contained here before eliminating
registration specializations.

JIRA: MDSAL-843
Change-Id: Idf452e7b847b66c47de2019c883570964e28a85f
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
entityownership/mdsal-eos-common-api/src/main/java/org/opendaylight/mdsal/eos/common/api/CandidateAlreadyRegisteredException.java
entityownership/mdsal-eos-common-api/src/main/java/org/opendaylight/mdsal/eos/common/api/EntityOwnershipChangeState.java
entityownership/mdsal-eos-common-api/src/main/java/org/opendaylight/mdsal/eos/common/api/EntityOwnershipState.java
entityownership/mdsal-eos-common-api/src/main/java/org/opendaylight/mdsal/eos/common/api/GenericEntity.java
entityownership/mdsal-eos-common-api/src/test/java/org/opendaylight/mdsal/eos/common/api/EntityOwnershipChangeStateTest.java
entityownership/mdsal-eos-common-api/src/test/java/org/opendaylight/mdsal/eos/common/api/EntityOwnershipStateTest.java
entityownership/mdsal-eos-common-api/src/test/java/org/opendaylight/mdsal/eos/common/api/GenericEntityOwnershipChangeTest.java
entityownership/mdsal-eos-common-api/src/test/java/org/opendaylight/mdsal/eos/common/api/GenericEntityTest.java

index 3382d691a4f268d39c92d0a1d95f6ee060f3583c..38cc066d0731a988fdccc918784c4c17da20a99b 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.mdsal.eos.common.api;
 
 import static java.util.Objects.requireNonNull;
 
-import java.io.Serial;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
 
@@ -18,7 +17,7 @@ import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
  * duplicate registration or two different components within the same process trying to register a Candidate.
  */
 public class CandidateAlreadyRegisteredException extends Exception {
-    @Serial
+    @java.io.Serial
     private static final long serialVersionUID = 1L;
 
     private final @NonNull GenericEntity<?> entity;
@@ -33,7 +32,6 @@ public class CandidateAlreadyRegisteredException extends Exception {
      * Gets the entity for which a Candidate has already been registered in the current process.
      *
      * @param <T> the instance identifier path type
-     *
      * @return the entity.
      */
     @SuppressWarnings("unchecked")
index d66022efc6eff35c94fd9789c8353cc4d0de4af6..b3028e292b0e0b8609d07837539ee507330b862c 100644 (file)
@@ -7,9 +7,8 @@
  */
 package org.opendaylight.mdsal.eos.common.api;
 
-import static com.google.common.base.Preconditions.checkArgument;
-
 import com.google.common.collect.ImmutableMap;
+import org.eclipse.jdt.annotation.NonNull;
 
 /**
  * Enumerates the ownership change states for an entity.
@@ -53,7 +52,7 @@ public enum EntityOwnershipChangeState {
 
     static {
         final var builder = ImmutableMap.<Key, EntityOwnershipChangeState>builder();
-        for (final EntityOwnershipChangeState e : values()) {
+        for (var e : values()) {
             builder.put(new Key(e.wasOwner, e.isOwner, e.hasOwner), e);
         }
         BY_KEY = builder.build();
@@ -99,39 +98,17 @@ public enum EntityOwnershipChangeState {
         return name() + " [wasOwner=" + wasOwner + ", isOwner=" + isOwner + ", hasOwner=" + hasOwner + "]";
     }
 
-    public static EntityOwnershipChangeState from(
-            final boolean wasOwner, final boolean isOwner, final boolean hasOwner) {
-        final EntityOwnershipChangeState state = BY_KEY.get(new Key(wasOwner, isOwner, hasOwner));
-        checkArgument(state != null, "Invalid combination of wasOwner: %s, isOwner: %s, hasOwner: %s",
-                wasOwner, isOwner, hasOwner);
-        return state;
-    }
-
-    private static final class Key {
-        private final boolean wasOwner;
-        private final boolean isOwner;
-        private final boolean hasOwner;
-
-        Key(final boolean wasOwner, final boolean isOwner, final boolean hasOwner) {
-            this.wasOwner = wasOwner;
-            this.isOwner = isOwner;
-            this.hasOwner = hasOwner;
-        }
-
-        @Override
-        public int hashCode() {
-            final int prime = 31;
-            int result = 1;
-            result = prime * result + Boolean.hashCode(hasOwner);
-            result = prime * result + Boolean.hashCode(isOwner);
-            result = prime * result + Boolean.hashCode(wasOwner);
-            return result;
+    public static @NonNull EntityOwnershipChangeState from(final boolean wasOwner, final boolean isOwner,
+            final boolean hasOwner) {
+        final var state = BY_KEY.get(new Key(wasOwner, isOwner, hasOwner));
+        if (state != null) {
+            return state;
         }
+        throw new IllegalArgumentException("Invalid combination of wasOwner: %s, isOwner: %s, hasOwner: %s".formatted(
+            wasOwner, isOwner, hasOwner));
+    }
 
-        @Override
-        public boolean equals(final Object obj) {
-            return obj == this || obj instanceof Key other
-                && hasOwner == other.hasOwner && isOwner == other.isOwner && wasOwner == other.wasOwner;
-        }
+    private record Key(boolean wasOwner, boolean isOwner, boolean hasOwner) {
+        // Nothing else
     }
 }
index d5471920150a5d9cb297101858c74b29805685c3..8fda439caf82f8c86c7c49b25a2aa571d9b840bf 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.mdsal.eos.common.api;
 
+import org.eclipse.jdt.annotation.NonNull;
+
 /**
  * Enumerates the current ownership state for an entity.
  *
@@ -28,13 +30,10 @@ public enum EntityOwnershipState {
      */
     NO_OWNER;
 
-    public static EntityOwnershipState from(boolean isOwner, boolean hasOwner) {
+    public static @NonNull EntityOwnershipState from(final boolean isOwner, final boolean hasOwner) {
         if (isOwner) {
             return IS_OWNER;
-        } else if (hasOwner) {
-            return OWNED_BY_OTHER;
-        } else {
-            return NO_OWNER;
         }
+        return hasOwner ? OWNED_BY_OTHER : NO_OWNER;
     }
 }
index 57fb8c361bf175b47de6f1c26ebb8dc851f2a4db..21ded576885574d9d8c68d1d26f7b52ebf96d245 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.mdsal.eos.common.api;
 
 import static java.util.Objects.requireNonNull;
 
-import java.io.Serial;
 import java.io.Serializable;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
@@ -38,12 +37,11 @@ import org.opendaylight.yangtools.concepts.Identifiable;
  *
  * <p>
  *
- * @author Thomas Pantelis
- *
  * @param <T> the entity identifier type
+ * @author Thomas Pantelis
  */
 public class GenericEntity<T extends HierarchicalIdentifier<T>> implements Serializable, Identifiable<T> {
-    @Serial
+    @java.io.Serial
     private static final long serialVersionUID = 1L;
 
     private final @NonNull String type;
@@ -59,7 +57,7 @@ public class GenericEntity<T extends HierarchicalIdentifier<T>> implements Seria
      * @return the id.
      */
     @Override
-    public final @NonNull T getIdentifier() {
+    public final T getIdentifier() {
         return id;
     }
 
index 4ddd83318d4ee75d064de57396a3c74171a1983b..07dd717fb434f64e13022c2e14c232b683828b2a 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.mdsal.eos.common.api;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
 import org.junit.Test;
@@ -19,9 +20,8 @@ import org.junit.Test;
  * @author Thomas Pantelis
  */
 public class EntityOwnershipChangeStateTest {
-
     @Test
-    public void testFromWithValid() throws Exception {
+    public void testFromWithValid() {
         assertEquals("from(false, true, true)", EntityOwnershipChangeState.LOCAL_OWNERSHIP_GRANTED,
                 EntityOwnershipChangeState.from(false, true, true));
         assertEquals("from(true, false, true)", EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NEW_OWNER,
@@ -36,19 +36,19 @@ public class EntityOwnershipChangeStateTest {
                 EntityOwnershipChangeState.from(true, true, true));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testFromWithInvalidFalseTrueFalse() {
-        EntityOwnershipChangeState.from(false, true, false);
+        assertThrows(IllegalArgumentException.class, () -> EntityOwnershipChangeState.from(false, true, false));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testFromWithInvalidTrueTrueFalse() {
-        EntityOwnershipChangeState.from(true, true, false);
+        assertThrows(IllegalArgumentException.class, () -> EntityOwnershipChangeState.from(true, true, false));
     }
 
     @Test
-    public void basicTest() throws Exception {
-        EntityOwnershipChangeState entityOwnershipChangeState = EntityOwnershipChangeState.from(false, true, true);
+    public void basicTest() {
+        final var entityOwnershipChangeState = EntityOwnershipChangeState.from(false, true, true);
         assertTrue(entityOwnershipChangeState.hasOwner());
         assertTrue(entityOwnershipChangeState.isOwner());
         assertFalse(entityOwnershipChangeState.wasOwner());
index a8fff4b4283d364e9da032bcf0fea693e03fab2f..119c6ee8c2ec68ab0f9b7b4b67449e04d62e66a1 100644 (file)
@@ -7,16 +7,15 @@
  */
 package org.opendaylight.mdsal.eos.common.api;
 
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
 
-import org.junit.Test;
-
-public class EntityOwnershipStateTest {
+import org.junit.jupiter.api.Test;
 
+class EntityOwnershipStateTest {
     @Test
-    public void fromTest() throws Exception {
-        assertEquals(EntityOwnershipState.NO_OWNER, EntityOwnershipState.from(false, false));
-        assertEquals(EntityOwnershipState.IS_OWNER, EntityOwnershipState.from(true, false));
-        assertEquals(EntityOwnershipState.OWNED_BY_OTHER, EntityOwnershipState.from(false, true));
+    void fromTest() {
+        assertSame(EntityOwnershipState.NO_OWNER, EntityOwnershipState.from(false, false));
+        assertSame(EntityOwnershipState.IS_OWNER, EntityOwnershipState.from(true, false));
+        assertSame(EntityOwnershipState.OWNED_BY_OTHER, EntityOwnershipState.from(false, true));
     }
 }
\ No newline at end of file
index 003657d7f8b9cbe8da169f430f2faea6330bb960..7d3a8939f7e29d98e9767d845087dfba8536a8dd 100644 (file)
@@ -7,22 +7,26 @@
  */
 package org.opendaylight.mdsal.eos.common.api;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
 
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
 
-public class GenericEntityOwnershipChangeTest {
+@ExtendWith(MockitoExtension.class)
+class GenericEntityOwnershipChangeTest {
+    @Mock
+    private GenericEntity<?> genericEntity;
 
     @Test
-    public void basicTest() throws Exception {
-        final GenericEntity genericEntity = mock(GenericEntity.class);
+    void basicTest() {
         doReturn("testEntity").when(genericEntity).toString();
-        final GenericEntityOwnershipChange genericEntityOwnershipChange =
-                new GenericEntityOwnershipChange(genericEntity, EntityOwnershipChangeState.LOCAL_OWNERSHIP_GRANTED);
+        final var genericEntityOwnershipChange = new GenericEntityOwnershipChange<>(genericEntity,
+            EntityOwnershipChangeState.LOCAL_OWNERSHIP_GRANTED);
 
         assertEquals(genericEntity, genericEntityOwnershipChange.getEntity());
         assertEquals(EntityOwnershipChangeState.LOCAL_OWNERSHIP_GRANTED, genericEntityOwnershipChange.getState());
index 81d23482532d9bdb006a25434e970b97f4602b7e..1633290b7dfc910e97452de60a2334d0cd82141d 100644 (file)
@@ -7,22 +7,20 @@
  */
 package org.opendaylight.mdsal.eos.common.api;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
-import java.io.Serial;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
 
-public class GenericEntityTest {
-
+class GenericEntityTest {
     @Test
-    public void basicTest() throws Exception {
-        final TestClass testClass = new TestClass();
-        final GenericEntity<?> genericEntity = new GenericEntity<>("testType", testClass);
-        final GenericEntity<?> genericEntityDiff = new GenericEntity<>("differentTestType", new TestClassDiff());
+    void basicTest() {
+        final var testClass = new TestClass();
+        final var genericEntity = new GenericEntity<>("testType", testClass);
+        final var genericEntityDiff = new GenericEntity<>("differentTestType", new TestClassDiff());
 
         assertEquals(TestClass.class, genericEntity.getIdentifier().getClass());
         assertEquals("testType", genericEntity.getType());
@@ -36,7 +34,7 @@ public class GenericEntityTest {
     }
 
     private static final class TestClass implements HierarchicalIdentifier<TestClass> {
-        @Serial
+        @java.io.Serial
         private static final long serialVersionUID = 1L;
 
         @Override
@@ -46,7 +44,7 @@ public class GenericEntityTest {
     }
 
     private static final class TestClassDiff implements HierarchicalIdentifier<TestClassDiff> {
-        @Serial
+        @java.io.Serial
         private static final long serialVersionUID = 1L;
 
         @Override