Unit tests for ofoverlay
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / test / TransactionMockUtils.java
1 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.test;
2
3 import static org.mockito.Mockito.mock;
4 import static org.mockito.Mockito.when;
5
6 import java.util.concurrent.ExecutionException;
7
8 import com.google.common.base.Optional;
9 import com.google.common.util.concurrent.CheckedFuture;
10 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
11 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
12 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
13 import org.opendaylight.yangtools.yang.binding.DataObject;
14 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
15
16 public class TransactionMockUtils {
17
18     /**
19      * Stubs {@link ReadOnlyTransaction#read(LogicalDatastoreType, InstanceIdentifier)}
20      * to return a given {@link DataObject}
21      *
22      * @param roTx mocked transaction to stub
23      * @param store {@link LogicalDatastoreType}
24      * @param path {@link InstanceIdentifier}
25      * @param isPresent stub {@link Optional#isPresent()}; if {@code true}, stub
26      *        {@link Optional#get()} to return {@code returnObject}
27      * @param returnObject {@link DataObject} to return
28      * @param <T> type of {@code returnObject}
29      * @throws ExecutionException
30      * @throws InterruptedException
31      */
32     @SuppressWarnings("unchecked")
33     public static <T extends DataObject> void setupRoTx(ReadOnlyTransaction roTx, LogicalDatastoreType store,
34             InstanceIdentifier<T> path, boolean isPresent, T returnObject)
35             throws ExecutionException, InterruptedException {
36
37         CheckedFuture<Optional<T>, ReadFailedException> future = mock(CheckedFuture.class);
38         when(roTx.read(store, path)).thenReturn(future);
39         Optional<T> opt = mock(Optional.class);
40         when(future.get()).thenReturn(opt);
41         when(opt.isPresent()).thenReturn(isPresent);
42         if (isPresent) {
43             when(opt.get()).thenReturn(returnObject);
44         }
45     }
46
47 }