Convert DatastoreSnapshotRestore to OSGi DS
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / databroker / ClientBackedDataStoreTest.java
index 24a58759673f134b6c24f0f020a7f2f0a09caa2d..f4a614c0305806dcc32620234b2625a6da6199de 100644 (file)
@@ -7,9 +7,13 @@
  */
 package org.opendaylight.controller.cluster.databroker;
 
-import java.lang.reflect.Field;
-import org.junit.Assert;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.junit.AfterClass;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.mockito.Mock;
 import org.mockito.Mockito;
@@ -20,37 +24,38 @@ import org.opendaylight.controller.cluster.access.concepts.FrontendType;
 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
 import org.opendaylight.controller.cluster.access.concepts.MemberName;
 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
-import org.opendaylight.controller.cluster.databroker.actors.dds.AbstractClientHandle;
 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientLocalHistory;
 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientSnapshot;
 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
 import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
-import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
+import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
-import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
-import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
-import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
-import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
+import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
+import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
+import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 
 public class ClientBackedDataStoreTest {
 
     private static final ClientIdentifier UNKNOWN_ID = ClientIdentifier.create(
             FrontendIdentifier.create(MemberName.forName("local"), FrontendType.forName("unknown")), 0);
 
-    private static FrontendIdentifier FRONTEND_IDENTIFIER = FrontendIdentifier.create(
+    private static final FrontendIdentifier FRONTEND_IDENTIFIER = FrontendIdentifier.create(
             MemberName.forName("member"), FrontendType.forName("frontend"));
     private static final ClientIdentifier CLIENT_IDENTIFIER = ClientIdentifier.create(FRONTEND_IDENTIFIER, 0);
 
-    private static LocalHistoryIdentifier HISTORY_ID = new LocalHistoryIdentifier(CLIENT_IDENTIFIER, 0);
+    private static final LocalHistoryIdentifier HISTORY_ID = new LocalHistoryIdentifier(CLIENT_IDENTIFIER, 0);
     private static final TransactionIdentifier TRANSACTION_IDENTIFIER = new TransactionIdentifier(HISTORY_ID, 0);
 
+    private static EffectiveModelContext SCHEMA_CONTEXT;
+
     @Mock
     private DataStoreClient clientActor;
 
     @Mock
-    private ActorContext actorContext;
+    private ActorUtils actorUtils;
 
     @Mock
     private ClientLocalHistory clientLocalHistory;
@@ -61,65 +66,68 @@ public class ClientBackedDataStoreTest {
     @Mock
     private ClientSnapshot clientSnapshot;
 
-    @Before
-    public void setUp() throws Exception {
-        MockitoAnnotations.initMocks(this);
-
-        final SchemaContext schemaContext = TestModel.createTestContext();
+    @BeforeClass
+    public static void beforeClass() {
+        SCHEMA_CONTEXT = TestModel.createTestContext();
+    }
 
-        Mockito.when(actorContext.getSchemaContext()).thenReturn(schemaContext);
-        Mockito.when(actorContext.getDatastoreContext()).thenReturn(DatastoreContext.newBuilder().build());
+    @AfterClass
+    public static void afterClass() {
+        SCHEMA_CONTEXT = null;
+    }
 
-        final Field transactionIdField = AbstractClientHandle.class.getDeclaredField("transactionId");
-        transactionIdField.setAccessible(true);
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
 
-        // set transaction ids to mocked objects
-        transactionIdField.set(clientTransaction, TRANSACTION_IDENTIFIER);
-        transactionIdField.set(clientSnapshot, TRANSACTION_IDENTIFIER);
+        when(actorUtils.getSchemaContext()).thenReturn(SCHEMA_CONTEXT);
+        when(actorUtils.getDatastoreContext()).thenReturn(DatastoreContext.newBuilder().build());
+        when(clientTransaction.getIdentifier()).thenReturn(TRANSACTION_IDENTIFIER);
+        when(clientSnapshot.getIdentifier()).thenReturn(TRANSACTION_IDENTIFIER);
 
-        Mockito.when(clientActor.getIdentifier()).thenReturn(CLIENT_IDENTIFIER);
-        Mockito.when(clientActor.createTransaction()).thenReturn(clientTransaction);
-        Mockito.when(clientActor.createLocalHistory()).thenReturn(clientLocalHistory);
-        Mockito.when(clientActor.createSnapshot()).thenReturn(clientSnapshot);
+        when(clientActor.getIdentifier()).thenReturn(CLIENT_IDENTIFIER);
+        when(clientActor.createTransaction()).thenReturn(clientTransaction);
+        when(clientActor.createLocalHistory()).thenReturn(clientLocalHistory);
+        when(clientActor.createSnapshot()).thenReturn(clientSnapshot);
     }
 
     @Test
-    public void testCreateTransactionChain() throws Exception {
-        try (final ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
-                actorContext, UNKNOWN_ID, clientActor)) {
+    public void testCreateTransactionChain() {
+        try (ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
+                actorUtils, UNKNOWN_ID, clientActor)) {
             final DOMStoreTransactionChain txChain = clientBackedDataStore.createTransactionChain();
-            Assert.assertNotNull(txChain);
-            Mockito.verify(clientActor, Mockito.times(1)).createLocalHistory();
+            assertNotNull(txChain);
+            verify(clientActor, Mockito.times(1)).createLocalHistory();
         }
     }
 
     @Test
-    public void testNewReadOnlyTransaction() throws Exception {
-        try (final ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
-                actorContext, UNKNOWN_ID, clientActor)) {
+    public void testNewReadOnlyTransaction() {
+        try (ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
+                actorUtils, UNKNOWN_ID, clientActor)) {
             final DOMStoreReadTransaction tx = clientBackedDataStore.newReadOnlyTransaction();
-            Assert.assertNotNull(tx);
-            Mockito.verify(clientActor, Mockito.times(1)).createSnapshot();
+            assertNotNull(tx);
+            verify(clientActor, Mockito.times(1)).createSnapshot();
         }
     }
 
     @Test
-    public void testNewWriteOnlyTransaction() throws Exception {
-        try (final ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
-                actorContext, UNKNOWN_ID, clientActor)) {
+    public void testNewWriteOnlyTransaction() {
+        try (ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
+                actorUtils, UNKNOWN_ID, clientActor)) {
             final DOMStoreWriteTransaction tx = clientBackedDataStore.newWriteOnlyTransaction();
-            Assert.assertNotNull(tx);
-            Mockito.verify(clientActor, Mockito.times(1)).createTransaction();
+            assertNotNull(tx);
+            verify(clientActor, Mockito.times(1)).createTransaction();
         }
     }
 
     @Test
-    public void testNewReadWriteTransaction() throws Exception {
-        try (final ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
-                actorContext, UNKNOWN_ID, clientActor)) {
+    public void testNewReadWriteTransaction() {
+        try (ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
+                actorUtils, UNKNOWN_ID, clientActor)) {
             final DOMStoreReadWriteTransaction tx = clientBackedDataStore.newReadWriteTransaction();
-            Assert.assertNotNull(tx);
-            Mockito.verify(clientActor, Mockito.times(1)).createTransaction();
+            assertNotNull(tx);
+            verify(clientActor, Mockito.times(1)).createTransaction();
         }
     }
-}
\ No newline at end of file
+}