2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.cluster.datastore.utils;
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;
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;
31 public class PruningDataTreeModificationTest {
34 DataTreeModification delegate;
37 Set<URI> validNamespaces;
40 NormalizedNode<?,?> prunedNormalizedNode;
42 PruningDataTreeModification pruningDataTreeModification;
46 MockitoAnnotations.initMocks(this);
47 pruningDataTreeModification = new PruningDataTreeModification(delegate, validNamespaces) {
49 NormalizedNode<?, ?> pruneNormalizedNode(NormalizedNode<?, ?> input) {
50 return prunedNormalizedNode;
56 public void testDelete(){
57 pruningDataTreeModification.delete(CarsModel.BASE_PATH);
59 verify(delegate).delete(CarsModel.BASE_PATH);
63 public void testDeleteOnException(){
64 YangInstanceIdentifier path = CarsModel.BASE_PATH;
65 doThrow(SchemaValidationFailedException.class).when(delegate).delete(path);
67 pruningDataTreeModification.delete(path);
69 verify(delegate, times(1)).delete(path);
74 public void testMerge(){
75 NormalizedNode<?, ?> normalizedNode = CarsModel.create();
76 YangInstanceIdentifier path = CarsModel.BASE_PATH;
77 pruningDataTreeModification.merge(path, normalizedNode);
79 verify(delegate, times(1)).merge(path, normalizedNode);
83 public void testMergeOnException(){
84 NormalizedNode<?, ?> normalizedNode = CarsModel.create();
85 YangInstanceIdentifier path = CarsModel.BASE_PATH;
87 doThrow(SchemaValidationFailedException.class).when(delegate).merge(path, normalizedNode);
88 doReturn(true).when(validNamespaces).contains(any(YangInstanceIdentifier.PathArgument.class));
90 pruningDataTreeModification.merge(path, normalizedNode);
92 verify(delegate, times(1)).merge(path, normalizedNode);
93 verify(delegate, times(1)).merge(path, prunedNormalizedNode);
97 public void testWrite(){
98 NormalizedNode<?, ?> normalizedNode = CarsModel.create();
99 YangInstanceIdentifier path = CarsModel.BASE_PATH;
100 pruningDataTreeModification.write(path, normalizedNode);
102 verify(delegate, times(1)).write(path, normalizedNode);
106 public void testWriteOnException(){
107 NormalizedNode<?, ?> normalizedNode = CarsModel.create();
108 YangInstanceIdentifier path = CarsModel.BASE_PATH;
110 doThrow(SchemaValidationFailedException.class).when(delegate).write(path, normalizedNode);
111 doReturn(true).when(validNamespaces).contains(any(YangInstanceIdentifier.PathArgument.class));
113 pruningDataTreeModification.write(path, normalizedNode);
115 verify(delegate, times(1)).write(path, normalizedNode);
116 verify(delegate, times(1)).write(path, prunedNormalizedNode);
120 public void testReady(){
121 pruningDataTreeModification.ready();
123 verify(delegate).ready();
127 public void testApplyToCursor(){
128 DataTreeModificationCursor dataTreeModificationCursor = mock(DataTreeModificationCursor.class);
129 pruningDataTreeModification.applyToCursor(dataTreeModificationCursor);
131 verify(delegate).applyToCursor(dataTreeModificationCursor);
135 public void testReadNode(){
136 pruningDataTreeModification.readNode(CarsModel.BASE_PATH);
138 verify(delegate).readNode(CarsModel.BASE_PATH);
142 public void testNewModification(){
143 DataTreeModification dataTreeModification = pruningDataTreeModification.newModification();
145 assertTrue("new modification not of type PruningDataTreeModification", dataTreeModification instanceof PruningDataTreeModification);