627c69c92595f7c7e0f4dbdf231e5f4b0aad39be
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.v3po.translate.util.write.registry;
18
19 import static org.hamcrest.CoreMatchers.hasItem;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertThat;
22 import static org.mockito.Mockito.when;
23
24 import com.google.common.collect.Sets;
25 import io.fd.honeycomb.v3po.translate.util.DataObjects;
26 import io.fd.honeycomb.v3po.translate.write.Writer;
27 import java.util.Collections;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34
35 public class SubtreeWriterTest {
36
37     @Mock
38     Writer<DataObjects.DataObject4> writer;
39     @Mock
40     Writer<DataObjects.DataObject4.DataObject41> writer11;
41
42     @Before
43     public void setUp() throws Exception {
44         MockitoAnnotations.initMocks(this);
45         when(writer.getManagedDataObjectType()).thenReturn(DataObjects.DataObject4.IID);
46         when(writer11.getManagedDataObjectType()).thenReturn(DataObjects.DataObject4.DataObject41.IID);
47     }
48
49     @Test(expected = IllegalArgumentException.class)
50     public void testSubtreeWriterCreationFail() throws Exception {
51         // The subtree node identified by IID.c(DataObject.class) is not a child of writer.getManagedDataObjectType
52         SubtreeWriter.createForWriter(Collections.singleton(InstanceIdentifier.create(DataObject.class)), writer);
53     }
54
55     @Test(expected = IllegalArgumentException.class)
56     public void testSubtreeWriterCreationFailInvalidIid() throws Exception {
57         // The subtree node identified by IID.c(DataObject.class) is not a child of writer.getManagedDataObjectType
58         SubtreeWriter.createForWriter(Collections.singleton(DataObjects.DataObject4.IID), writer);
59     }
60
61     @Test
62     public void testSubtreeWriterCreation() throws Exception {
63         final SubtreeWriter<?> forWriter = (SubtreeWriter<?>) SubtreeWriter.createForWriter(Sets.newHashSet(
64                 DataObjects.DataObject4.DataObject41.IID,
65                 DataObjects.DataObject4.DataObject41.DataObject411.IID,
66                 DataObjects.DataObject4.DataObject42.IID),
67                 writer);
68
69         assertEquals(writer.getManagedDataObjectType(), forWriter.getManagedDataObjectType());
70         assertEquals(3, forWriter.getHandledChildTypes().size());
71     }
72
73     @Test
74     public void testSubtreeWriterHandledTypes() throws Exception {
75         final SubtreeWriter<?> forWriter = (SubtreeWriter<?>) SubtreeWriter.createForWriter(Sets.newHashSet(
76                 DataObjects.DataObject4.DataObject41.DataObject411.IID),
77                 writer);
78
79         assertEquals(writer.getManagedDataObjectType(), forWriter.getManagedDataObjectType());
80         assertEquals(1, forWriter.getHandledChildTypes().size());
81         assertThat(forWriter.getHandledChildTypes(), hasItem(DataObjects.DataObject4.DataObject41.DataObject411.IID));
82     }
83
84 }