Harden yang-model-util test suite 77/70877/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 12 Apr 2018 22:05:00 +0000 (00:05 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 12 Apr 2018 22:32:02 +0000 (00:32 +0200)
Update the test suite to use yangtools mockito configuration, forcing
all mocking to be done.

Change-Id: Icbf0cd0bc6932b6b807bb6a5ac0fd3712543c99e
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-model-util/pom.xml
yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/repo/util/InMemorySchemaSourceCacheTest.java
yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/repo/util/RefcountedRegistrationTest.java
yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/repo/util/SchemaSourceTransformerTest.java
yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/BitsTypeTest.java
yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/DataNodeIteratorTest.java
yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtilTest.java
yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/SimpleSchemaContextTest.java
yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/type/TypeTest.java

index 93a3a6a0610c86aae28d87b5bedad1ac6e21d5c2..d9802501a316e65845b1872756f10176ce0f9eb1 100644 (file)
             <groupId>${project.groupId}</groupId>
             <artifactId>yang-model-api</artifactId>
         </dependency>
+
         <dependency>
-            <groupId>junit</groupId>
-            <artifactId>junit</artifactId>
+            <groupId>org.opendaylight.yangtools</groupId>
+            <artifactId>mockito-configuration</artifactId>
             <scope>test</scope>
         </dependency>
-        <dependency>
-            <groupId>org.mockito</groupId>
-            <artifactId>mockito-core</artifactId>
-        </dependency>
         <dependency>
             <groupId>ch.qos.logback</groupId>
             <artifactId>logback-classic</artifactId>
index d416d3315503415ddd50f46cc83702d566bed7f9..6f86695ebe56180ed65d1aa80f693bfaa9ef39d4 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.yangtools.yang.model.repo.util;
 
 import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 
 import com.google.common.base.MoreObjects;
@@ -49,7 +50,8 @@ public class InMemorySchemaSourceCacheTest {
 
     @Before
     public void setUp() throws Exception {
-        doReturn(this.registration).when(this.registry).registerSchemaSource(any(SchemaSourceProvider.class),
+        doNothing().when(registration).close();
+        doReturn(registration).when(registry).registerSchemaSource(any(SchemaSourceProvider.class),
                 any(PotentialSchemaSource.class));
     }
 
@@ -99,7 +101,7 @@ public class InMemorySchemaSourceCacheTest {
     }
 
     @Test
-    public void inMemorySchemaSourceCache3test() throws Exception {
+    public void inMemorySchemaSourceCache3test() throws InterruptedException, ExecutionException {
         final InMemorySchemaSourceCache<YangSchemaSourceRepresentation> inMemorySchemaSourceCache =
             InMemorySchemaSourceCache.createSoftCache(this.registry, REPRESENTATION);
         final InMemorySchemaSourceCache<YangSchemaSourceRepresentation> inMemorySchemaSourceCache2 =
index 4cdb0233572421ef899894b0ad68a424bf01ec0a..3bd3a7b45f1cad80e5acd16d4b5d300a3b2b5549 100644 (file)
@@ -7,37 +7,50 @@
  */
 package org.opendaylight.yangtools.yang.model.repo.util;
 
-import org.junit.Assert;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import org.junit.Before;
 import org.junit.Test;
-import org.mockito.Mockito;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
 
+@RunWith(MockitoJUnitRunner.class)
 public class RefcountedRegistrationTest {
+    @Mock
+    private SchemaSourceRegistration<?> reg;
+
+    @Before
+    public void before() {
+        doNothing().when(reg).close();
+    }
 
     @Test
     public void refcountDecTrue() {
-        final SchemaSourceRegistration<?> reg = Mockito.mock(SchemaSourceRegistration.class);
         final RefcountedRegistration ref = new RefcountedRegistration(reg);
-        Assert.assertTrue(ref.decRef());
-        Mockito.verify(reg, Mockito.times(1)).close();
+        assertTrue(ref.decRef());
+        verify(reg, times(1)).close();
     }
 
     @Test
     public void refcountIncDecFalse() {
-        final SchemaSourceRegistration<?> reg = Mockito.mock(SchemaSourceRegistration.class);
         final RefcountedRegistration ref = new RefcountedRegistration(reg);
         ref.incRef();
-        Assert.assertFalse(ref.decRef());
-        Mockito.verify(reg, Mockito.times(0)).close();
+        assertFalse(ref.decRef());
+        verify(reg, times(0)).close();
     }
 
     @Test
     public void refcountIncDecTrue() {
-        final SchemaSourceRegistration<?> reg = Mockito.mock(SchemaSourceRegistration.class);
         final RefcountedRegistration ref = new RefcountedRegistration(reg);
         ref.incRef();
-        Assert.assertFalse(ref.decRef());
-        Assert.assertTrue(ref.decRef());
-        Mockito.verify(reg, Mockito.times(1)).close();
+        assertFalse(ref.decRef());
+        assertTrue(ref.decRef());
+        verify(reg, times(1)).close();
     }
 }
index 71482cab8fc3c0a38a6d1fcad791fbb141f4651f..51b8f91978a5e6f07de1352122b4d82ae88eaee1 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.yangtools.yang.model.repo.util;
 
 import com.google.common.util.concurrent.AsyncFunction;
 import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.SettableFuture;
 import java.util.Arrays;
 import java.util.concurrent.Future;
 import javax.annotation.Nonnull;
@@ -16,7 +17,6 @@ import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.Mockito;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
@@ -134,7 +134,7 @@ public class SchemaSourceTransformerTest {
         @Override
         public ListenableFuture<? extends YangSchemaSourceRepresentation> getSource(
                 final SourceIdentifier sourceIdentifier) {
-            return Mockito.mock(ListenableFuture.class);
+            return SettableFuture.create();
         }
 
     }
index 4714fb2b8c513f69e0fb42418333a4fe022e80d2..66f41f838d8744fc217e6b40a6bb50a06445cf12 100644 (file)
@@ -16,23 +16,25 @@ import static org.mockito.Mockito.doReturn;
 import java.util.Collections;
 import java.util.Optional;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.runners.MockitoJUnitRunner;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.Status;
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
 
+@RunWith(MockitoJUnitRunner.class)
 public class BitsTypeTest {
-
     @Mock
     private BitsTypeDefinition.Bit bit;
 
     @Test
     public void canCreateBitsType() {
-        MockitoAnnotations.initMocks(this);
         doReturn("test").when(bit).getName();
+        doReturn(0L).when(bit).getPosition();
+        doReturn("toString").when(bit).toString();
 
         QName qname = QName.create("namespace", "localname");
         SchemaPath schemaPath = SchemaPath.create(true, qname);
@@ -56,7 +58,5 @@ public class BitsTypeTest {
         assertNotEquals("bitsType shouldn't equal to null", null, bitsType);
         assertEquals("bitsType should equals to itself", bitsType, bitsType);
         assertNotEquals("bitsType shouldn't equal to object of other type", "str", bitsType);
-
     }
-
 }
index 799dd63463f519444c5b4ad7caf4ea278221d719..bab599b3eecbfafd15ceef7ec5b288601cc98054 100644 (file)
@@ -21,26 +21,51 @@ import java.util.Set;
 import java.util.SortedMap;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.runners.MockitoJUnitRunner;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.CopyableNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
+import org.opendaylight.yangtools.yang.model.api.OperationDefinition;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 
+@RunWith(MockitoJUnitRunner.class)
 public class DataNodeIteratorTest {
-
     private DataNodeIterator dataNodeIterator;
 
     @Before
-    public void setUp() {
-        DataNodeContainer dataNodeContainer = mock(DataNodeContainer.class);
-        this.dataNodeIterator = new DataNodeIterator(dataNodeContainer);
+    public void before() {
+        this.dataNodeIterator = new DataNodeIterator(mockDataNodeContainer(DataNodeContainer.class));
+    }
+
+    private static <T extends DataNodeContainer> T mockDataNodeContainer(final Class<T> clazz) {
+        final T mock = mock(clazz);
+        doReturn(Collections.emptyList()).when(mock).getChildNodes();
+        doReturn(Collections.emptySet()).when(mock).getGroupings();
+        doReturn(Collections.emptySet()).when(mock).getTypeDefinitions();
+        return mock;
+    }
+
+    private static <T extends OperationDefinition> T mockOperationDefinition(final T mock) {
+        doReturn(Collections.emptySet()).when(mock).getGroupings();
+        doReturn(Collections.emptySet()).when(mock).getTypeDefinitions();
+        doReturn(mockDataNodeContainer(ContainerSchemaNode.class)).when(mock).getInput();
+        doReturn(mockDataNodeContainer(ContainerSchemaNode.class)).when(mock).getOutput();
+        return mock;
+    }
+
+    @Deprecated
+    private static <T extends CopyableNode> T mockCopyableNode(final boolean augmenting, final T node) {
+        doReturn(augmenting).when(node).isAugmenting();
+        return node;
     }
 
     @Test(expected = IllegalArgumentException.class)
@@ -70,19 +95,18 @@ public class DataNodeIteratorTest {
 
     @Test
     public void testTraversal() {
-        final Module mockedModule = mock(Module.class);
-
-        final ContainerSchemaNode mockedAugmentingContainer = mock(ContainerSchemaNode.class);
-        doReturn(true).when(mockedAugmentingContainer).isAugmenting();
+        final Module mockedModule = mockDataNodeContainer(Module.class);
 
-        final ContainerSchemaNode mockedContainer = mock(ContainerSchemaNode.class);
+        final ContainerSchemaNode mockedAugmentingContainer = mockCopyableNode(true, mock(ContainerSchemaNode.class));
+        final ContainerSchemaNode mockedContainer = mockCopyableNode(false, mockDataNodeContainer(
+            ContainerSchemaNode.class));
 
-        final ListSchemaNode mockedList = mock(ListSchemaNode.class);
+        final ListSchemaNode mockedList = mockCopyableNode(false, mockDataNodeContainer(ListSchemaNode.class));
 
-        final ChoiceSchemaNode mockedChoice = mock(ChoiceSchemaNode.class);
-        final CaseSchemaNode mockedCase1 = mock(CaseSchemaNode.class);
+        final ChoiceSchemaNode mockedChoice = mockCopyableNode(false, mock(ChoiceSchemaNode.class));
+        final CaseSchemaNode mockedCase1 = mockDataNodeContainer(CaseSchemaNode.class);
         final QName mockedCase1QName = QName.create("", "case1");
-        final CaseSchemaNode mockedCase2 = mock(CaseSchemaNode.class);
+        final CaseSchemaNode mockedCase2 = mockDataNodeContainer(CaseSchemaNode.class);
         final QName mockedCase2QName = QName.create("", "case2");
         final SortedMap<QName, CaseSchemaNode> cases = ImmutableSortedMap.of(mockedCase1QName, mockedCase1,
             mockedCase2QName, mockedCase2);
@@ -92,17 +116,19 @@ public class DataNodeIteratorTest {
                 mockedChoice);
         doReturn(childNodes).when(mockedModule).getChildNodes();
 
-        final NotificationDefinition mockedNotification = mock(NotificationDefinition.class);
-        final ContainerSchemaNode mockedContainerInNotification = mock(ContainerSchemaNode.class);
+        final NotificationDefinition mockedNotification = mockDataNodeContainer(NotificationDefinition.class);
+        final ContainerSchemaNode mockedContainerInNotification = mockCopyableNode(false,
+            mockDataNodeContainer(ContainerSchemaNode.class));
         final Set<DataSchemaNode> notificationChildNodes = ImmutableSet.of(mockedContainerInNotification);
         doReturn(notificationChildNodes).when(mockedNotification).getChildNodes();
         final Set<NotificationDefinition> notifications = ImmutableSet.of(mockedNotification);
 
         doReturn(notifications).when(mockedModule).getNotifications();
 
-        final RpcDefinition mockedRpc = mock(RpcDefinition.class);
-        final ContainerSchemaNode mockedContainerInRpcInput = mock(ContainerSchemaNode.class);
-        final ListSchemaNode mockedListInRpcInputContainer = mock(ListSchemaNode.class);
+        final RpcDefinition mockedRpc = mockOperationDefinition(mock(RpcDefinition.class));
+        final ContainerSchemaNode mockedContainerInRpcInput = mockDataNodeContainer(ContainerSchemaNode.class);
+        final ListSchemaNode mockedListInRpcInputContainer = mockCopyableNode(false,
+            mockDataNodeContainer(ListSchemaNode.class));
         final Set<DataSchemaNode> rpcInputChildNodes = ImmutableSet.of(mockedListInRpcInputContainer);
         doReturn(rpcInputChildNodes).when(mockedContainerInRpcInput).getChildNodes();
         doReturn(mockedContainerInRpcInput).when(mockedRpc).getInput();
@@ -110,7 +136,7 @@ public class DataNodeIteratorTest {
 
         doReturn(rpcs).when(mockedModule).getRpcs();
 
-        final GroupingDefinition mockedGrouping = mock(GroupingDefinition.class);
+        final GroupingDefinition mockedGrouping = mockDataNodeContainer(GroupingDefinition.class);
         final Set<GroupingDefinition> groupings = ImmutableSet.of(mockedGrouping);
 
         doReturn(groupings).when(mockedModule).getGroupings();
index b2fde22a0bb2fe20129fba19573beb351b096d23..580ec72c53b657e5d3bcf05d53c00d3101eff53b 100644 (file)
@@ -38,6 +38,7 @@ public class SchemaContextUtilTest {
         MockitoAnnotations.initMocks(this);
         doReturn(Optional.empty()).when(mockSchemaContext).findModule(any(QNameModule.class));
 
+        doReturn("test").when(mockModule).getName();
         doReturn("test").when(mockModule).getPrefix();
         doReturn(NAMESPACE).when(mockModule).getNamespace();
         doReturn(QNameModule.create(NAMESPACE)).when(mockModule).getQNameModule();
index 1218471b2f8e2916e3d682b2524385bff3d7ad76..365f5c855ccc0aa2f7860158c46720201cb42550 100644 (file)
@@ -76,6 +76,7 @@ public class SimpleSchemaContextTest {
         doReturn(mod.getRevision()).when(ret).getRevision();
         doReturn(mod).when(ret).getQNameModule();
         doReturn(mod.toString()).when(ret).toString();
+        doReturn(ImmutableSet.of()).when(ret).getImports();
         return ret;
     }
 }
index ec173dc3986953365d11ad4ef9f34768d99d599f..50a0b40910c3d30ef736e1da9ebe8d61b6abe479 100644 (file)
@@ -13,6 +13,7 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 
 import com.google.common.collect.ImmutableList;
@@ -107,6 +108,7 @@ public class TypeTest {
     public void identityrefTypeTest() {
         final IdentityrefTypeBuilder identityrefTypeBuilder1 = BaseTypes.identityrefTypeBuilder(SCHEMA_PATH);
         final IdentitySchemaNode identitySchemaNode = mock(IdentitySchemaNode.class);
+        doReturn("identitySchemaNode").when(identitySchemaNode).toString();
         identityrefTypeBuilder1.addIdentity(identitySchemaNode);
         final IdentityrefTypeDefinition identityrefTypeDefinition1 = identityrefTypeBuilder1.build();
         final IdentityrefTypeBuilder identityrefTypeBuilder2 = BaseTypes.identityrefTypeBuilder(SCHEMA_PATH);