2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.databroker;
10 import org.junit.Assert;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.mockito.Mock;
14 import org.mockito.Mockito;
15 import org.mockito.MockitoAnnotations;
16 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
17 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
18 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
19 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
20 import org.opendaylight.controller.cluster.access.concepts.MemberName;
21 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
22 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientLocalHistory;
23 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientSnapshot;
24 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
25 import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
26 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
27 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
28 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
29 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
30 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
31 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
32 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 public class ClientBackedDataStoreTest {
37 private static final ClientIdentifier UNKNOWN_ID = ClientIdentifier.create(
38 FrontendIdentifier.create(MemberName.forName("local"), FrontendType.forName("unknown")), 0);
40 private static FrontendIdentifier FRONTEND_IDENTIFIER = FrontendIdentifier.create(
41 MemberName.forName("member"), FrontendType.forName("frontend"));
42 private static final ClientIdentifier CLIENT_IDENTIFIER = ClientIdentifier.create(FRONTEND_IDENTIFIER, 0);
44 private static LocalHistoryIdentifier HISTORY_ID = new LocalHistoryIdentifier(CLIENT_IDENTIFIER, 0);
45 private static final TransactionIdentifier TRANSACTION_IDENTIFIER = new TransactionIdentifier(HISTORY_ID, 0);
48 private DataStoreClient clientActor;
51 private ActorContext actorContext;
54 private ClientLocalHistory clientLocalHistory;
57 private ClientTransaction clientTransaction;
60 private ClientSnapshot clientSnapshot;
63 public void setUp() throws Exception {
64 MockitoAnnotations.initMocks(this);
66 final SchemaContext schemaContext = TestModel.createTestContext();
68 Mockito.when(actorContext.getSchemaContext()).thenReturn(schemaContext);
69 Mockito.when(actorContext.getDatastoreContext()).thenReturn(DatastoreContext.newBuilder().build());
70 Mockito.when(clientTransaction.getIdentifier()).thenReturn(TRANSACTION_IDENTIFIER);
71 Mockito.when(clientSnapshot.getIdentifier()).thenReturn(TRANSACTION_IDENTIFIER);
73 Mockito.when(clientActor.getIdentifier()).thenReturn(CLIENT_IDENTIFIER);
74 Mockito.when(clientActor.createTransaction()).thenReturn(clientTransaction);
75 Mockito.when(clientActor.createLocalHistory()).thenReturn(clientLocalHistory);
76 Mockito.when(clientActor.createSnapshot()).thenReturn(clientSnapshot);
80 public void testCreateTransactionChain() throws Exception {
81 try (ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
82 actorContext, UNKNOWN_ID, clientActor)) {
83 final DOMStoreTransactionChain txChain = clientBackedDataStore.createTransactionChain();
84 Assert.assertNotNull(txChain);
85 Mockito.verify(clientActor, Mockito.times(1)).createLocalHistory();
90 public void testNewReadOnlyTransaction() throws Exception {
91 try (ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
92 actorContext, UNKNOWN_ID, clientActor)) {
93 final DOMStoreReadTransaction tx = clientBackedDataStore.newReadOnlyTransaction();
94 Assert.assertNotNull(tx);
95 Mockito.verify(clientActor, Mockito.times(1)).createSnapshot();
100 public void testNewWriteOnlyTransaction() throws Exception {
101 try (ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
102 actorContext, UNKNOWN_ID, clientActor)) {
103 final DOMStoreWriteTransaction tx = clientBackedDataStore.newWriteOnlyTransaction();
104 Assert.assertNotNull(tx);
105 Mockito.verify(clientActor, Mockito.times(1)).createTransaction();
110 public void testNewReadWriteTransaction() throws Exception {
111 try (ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
112 actorContext, UNKNOWN_ID, clientActor)) {
113 final DOMStoreReadWriteTransaction tx = clientBackedDataStore.newReadWriteTransaction();
114 Assert.assertNotNull(tx);
115 Mockito.verify(clientActor, Mockito.times(1)).createTransaction();