Do not use null input for discard-changes
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / sal / NetconfKeystoreAdapterTest.java
index 727614f6b56adf33fdee75d47db2545166164643..c2c9f7a511c8887a0d4cc61fd01a4a95fe45c71e 100644 (file)
@@ -7,25 +7,26 @@
  */
 package org.opendaylight.netconf.sal.connect.netconf.sal;
 
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
-import static org.mockito.Matchers.any;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 
 import java.security.KeyStoreException;
 import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
 import java.util.List;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.DataObjectModification;
+import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
+import org.opendaylight.mdsal.binding.api.DataTreeModification;
 import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.Keystore;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017._private.keys.PrivateKey;
@@ -40,6 +41,7 @@ import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class NetconfKeystoreAdapterTest {
     private static final String XML_ELEMENT_PRIVATE_KEY = "private-key";
     private static final String XML_ELEMENT_NAME = "name";
@@ -51,26 +53,19 @@ public class NetconfKeystoreAdapterTest {
     @Mock
     private DataBroker dataBroker;
     @Mock
-    private ListenerRegistration listenerRegistration;
+    private ListenerRegistration<?> listenerRegistration;
 
     @Before
     public void setUp() {
-        MockitoAnnotations.initMocks(this);
-
-        doReturn(listenerRegistration).when(dataBroker).registerDataTreeChangeListener(
-                any(DataTreeIdentifier.class), any(NetconfKeystoreAdapter.class));
+        doReturn(listenerRegistration).when(dataBroker)
+            .registerDataTreeChangeListener(any(DataTreeIdentifier.class), any(NetconfKeystoreAdapter.class));
     }
 
     @Test
     public void testKeystoreAdapterInit() throws Exception {
-        NetconfKeystoreAdapter keystoreAdapter = new NetconfKeystoreAdapter(dataBroker);
-
-        try {
-            keystoreAdapter.getJavaKeyStore();
-            Assert.fail(IllegalStateException.class + "exception expected");
-        } catch (KeyStoreException e) {
-            assertTrue(e.getMessage().startsWith("No keystore private key found"));
-        }
+        final var keystoreAdapter = new NetconfKeystoreAdapter(dataBroker);
+        final var ex = assertThrows(KeyStoreException.class, keystoreAdapter::getJavaKeyStore);
+        assertThat(ex.getMessage(), startsWith("No keystore private key found"));
     }
 
     @SuppressWarnings("unchecked")
@@ -81,20 +76,18 @@ public class NetconfKeystoreAdapterTest {
         doReturn(keystoreObjectModification).when(dataTreeModification).getRootNode();
 
         DataObjectModification<?> childObjectModification = mock(DataObjectModification.class);
-        doReturn(Collections.singletonList(childObjectModification))
-            .when(keystoreObjectModification).getModifiedChildren();
+        doReturn(List.of(childObjectModification)).when(keystoreObjectModification).getModifiedChildren();
         doReturn(PrivateKey.class).when(childObjectModification).getDataType();
 
-        doReturn(DataObjectModification.ModificationType.WRITE)
-            .when(childObjectModification).getModificationType();
+        doReturn(DataObjectModification.ModificationType.WRITE).when(childObjectModification).getModificationType();
 
-        PrivateKey privateKey = getPrivateKey();
+        final var privateKey = getPrivateKey();
         doReturn(privateKey).when(childObjectModification).getDataAfter();
 
-        NetconfKeystoreAdapter keystoreAdapter = new NetconfKeystoreAdapter(dataBroker);
-        keystoreAdapter.onDataTreeChanged(Collections.singletonList(dataTreeModification));
+        final var keystoreAdapter = new NetconfKeystoreAdapter(dataBroker);
+        keystoreAdapter.onDataTreeChanged(List.of(dataTreeModification));
 
-        java.security.KeyStore keyStore = keystoreAdapter.getJavaKeyStore();
+        final var keyStore = keystoreAdapter.getJavaKeyStore();
         assertTrue(keyStore.containsAlias(privateKey.getName()));
     }
 
@@ -107,14 +100,12 @@ public class NetconfKeystoreAdapterTest {
         doReturn(keystoreObjectModification1).when(dataTreeModification1).getRootNode();
 
         DataObjectModification<?> childObjectModification1 = mock(DataObjectModification.class);
-        doReturn(Collections.singletonList(childObjectModification1))
-            .when(keystoreObjectModification1).getModifiedChildren();
+        doReturn(List.of(childObjectModification1)).when(keystoreObjectModification1).getModifiedChildren();
         doReturn(PrivateKey.class).when(childObjectModification1).getDataType();
 
-        doReturn(DataObjectModification.ModificationType.WRITE)
-            .when(childObjectModification1).getModificationType();
+        doReturn(DataObjectModification.ModificationType.WRITE).when(childObjectModification1).getModificationType();
 
-        PrivateKey privateKey = getPrivateKey();
+        final var privateKey = getPrivateKey();
         doReturn(privateKey).when(childObjectModification1).getDataAfter();
 
         // Prepare TrustedCertificate configuration
@@ -123,22 +114,21 @@ public class NetconfKeystoreAdapterTest {
         doReturn(keystoreObjectModification2).when(dataTreeModification2).getRootNode();
 
         DataObjectModification<?> childObjectModification2 = mock(DataObjectModification.class);
-        doReturn(Collections.singletonList(childObjectModification2))
-            .when(keystoreObjectModification2).getModifiedChildren();
+        doReturn(List.of(childObjectModification2)).when(keystoreObjectModification2).getModifiedChildren();
         doReturn(TrustedCertificate.class).when(childObjectModification2).getDataType();
 
         doReturn(DataObjectModification.ModificationType.WRITE)
             .when(childObjectModification2).getModificationType();
 
-        TrustedCertificate trustedCertificate = geTrustedCertificate();
+        final var trustedCertificate = geTrustedCertificate();
         doReturn(trustedCertificate).when(childObjectModification2).getDataAfter();
 
         // Apply configurations
-        NetconfKeystoreAdapter keystoreAdapter = new NetconfKeystoreAdapter(dataBroker);
-        keystoreAdapter.onDataTreeChanged(Arrays.asList(dataTreeModification1, dataTreeModification2));
+        final var keystoreAdapter = new NetconfKeystoreAdapter(dataBroker);
+        keystoreAdapter.onDataTreeChanged(List.of(dataTreeModification1, dataTreeModification2));
 
         // Check result
-        java.security.KeyStore keyStore = keystoreAdapter.getJavaKeyStore();
+        final var keyStore = keystoreAdapter.getJavaKeyStore();
         assertTrue(keyStore.containsAlias(privateKey.getName()));
         assertTrue(keyStore.containsAlias(trustedCertificate.getName()));
     }
@@ -166,7 +156,7 @@ public class NetconfKeystoreAdapterTest {
             }
 
             final PrivateKey privateKey = new PrivateKeyBuilder()
-                    .setKey(new PrivateKeyKey(keyName))
+                    .withKey(new PrivateKeyKey(keyName))
                     .setName(keyName)
                     .setData(keyData)
                     .setCertificateChain(certChain)
@@ -191,7 +181,7 @@ public class NetconfKeystoreAdapterTest {
             final String certData = element.getElementsByTagName(XML_ELEMENT_CERT).item(0).getTextContent();
 
             final TrustedCertificate certificate = new TrustedCertificateBuilder()
-                    .setKey(new TrustedCertificateKey(certName))
+                    .withKey(new TrustedCertificateKey(certName))
                     .setName(certName)
                     .setCertificate(certData)
                     .build();