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

©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.