BUG 2970 : Create a PruningDataTreeModification
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / utils / PruningDataTreeModificationTest.java
1 package org.opendaylight.controller.cluster.datastore.utils;
2
3 import static org.junit.Assert.assertTrue;
4 import static org.mockito.Matchers.any;
5 import static org.mockito.Mockito.doReturn;
6 import static org.mockito.Mockito.doThrow;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.times;
9 import static org.mockito.Mockito.verify;
10 import java.net.URI;
11 import java.util.Set;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.mockito.Mock;
15 import org.mockito.MockitoAnnotations;
16 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
21
22 public class PruningDataTreeModificationTest {
23
24     @Mock
25     DataTreeModification delegate;
26
27     @Mock
28     Set<URI> validNamespaces;
29
30     @Mock
31     NormalizedNode<?,?> prunedNormalizedNode;
32
33     PruningDataTreeModification pruningDataTreeModification;
34
35     @Before
36     public void setUp(){
37         MockitoAnnotations.initMocks(this);
38         pruningDataTreeModification = new PruningDataTreeModification(delegate, validNamespaces) {
39             @Override
40             NormalizedNode<?, ?> pruneNormalizedNode(NormalizedNode<?, ?> input) {
41                 return prunedNormalizedNode;
42             }
43         };
44     }
45
46     @Test
47     public void testDelete(){
48         pruningDataTreeModification.delete(CarsModel.BASE_PATH);
49
50         verify(delegate).delete(CarsModel.BASE_PATH);
51     }
52
53     @Test
54     public void testDeleteOnException(){
55         YangInstanceIdentifier path = CarsModel.BASE_PATH;
56         doThrow(IllegalArgumentException.class).when(delegate).delete(path);
57
58         pruningDataTreeModification.delete(path);
59
60         verify(delegate, times(1)).delete(path);
61     }
62
63
64     @Test
65     public void testMerge(){
66         NormalizedNode<?, ?> normalizedNode = CarsModel.create();
67         YangInstanceIdentifier path = CarsModel.BASE_PATH;
68         pruningDataTreeModification.merge(path, normalizedNode);
69
70         verify(delegate, times(1)).merge(path, normalizedNode);
71     }
72
73     @Test
74     public void testMergeOnException(){
75         NormalizedNode<?, ?> normalizedNode = CarsModel.create();
76         YangInstanceIdentifier path = CarsModel.BASE_PATH;
77
78         doThrow(IllegalArgumentException.class).when(delegate).merge(path, normalizedNode);
79         doReturn(true).when(validNamespaces).contains(any(YangInstanceIdentifier.PathArgument.class));
80
81         pruningDataTreeModification.merge(path, normalizedNode);
82
83         verify(delegate, times(1)).merge(path, normalizedNode);
84         verify(delegate, times(1)).merge(path, prunedNormalizedNode);
85     }
86
87     @Test
88     public void testWrite(){
89         NormalizedNode<?, ?> normalizedNode = CarsModel.create();
90         YangInstanceIdentifier path = CarsModel.BASE_PATH;
91         pruningDataTreeModification.write(path, normalizedNode);
92
93         verify(delegate, times(1)).write(path, normalizedNode);
94     }
95
96     @Test
97     public void testWriteOnException(){
98         NormalizedNode<?, ?> normalizedNode = CarsModel.create();
99         YangInstanceIdentifier path = CarsModel.BASE_PATH;
100
101         doThrow(IllegalArgumentException.class).when(delegate).write(path, normalizedNode);
102         doReturn(true).when(validNamespaces).contains(any(YangInstanceIdentifier.PathArgument.class));
103
104         pruningDataTreeModification.write(path, normalizedNode);
105
106         verify(delegate, times(1)).write(path, normalizedNode);
107         verify(delegate, times(1)).write(path, prunedNormalizedNode);
108     }
109
110     @Test
111     public void testReady(){
112         pruningDataTreeModification.ready();
113
114         verify(delegate).ready();
115     }
116
117     @Test
118     public void testApplyToCursor(){
119         DataTreeModificationCursor dataTreeModificationCursor = mock(DataTreeModificationCursor.class);
120         pruningDataTreeModification.applyToCursor(dataTreeModificationCursor);
121
122         verify(delegate).applyToCursor(dataTreeModificationCursor);
123     }
124
125     @Test
126     public void testReadNode(){
127         pruningDataTreeModification.readNode(CarsModel.BASE_PATH);
128
129         verify(delegate).readNode(CarsModel.BASE_PATH);
130     }
131
132     @Test
133     public void testNewModification(){
134         DataTreeModification dataTreeModification = pruningDataTreeModification.newModification();
135
136         assertTrue("new modification not of type PruningDataTreeModification", dataTreeModification instanceof PruningDataTreeModification);
137     }
138 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.