2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.v3po.translate.util.write.registry;
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;
24 import com.google.common.collect.Sets;
25 import io.fd.honeycomb.v3po.translate.write.Writer;
26 import java.util.Collections;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.opendaylight.yangtools.yang.binding.ChildOf;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 public class SubtreeWriterTest {
38 Writer<DataObject1> writer;
40 Writer<DataObject1.DataObject11> writer11;
43 public void setUp() throws Exception {
44 MockitoAnnotations.initMocks(this);
45 when(writer.getManagedDataObjectType()).thenReturn(DataObject1.IID);
46 when(writer11.getManagedDataObjectType()).thenReturn(DataObject1.DataObject11.IID);
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);
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(DataObject1.IID), writer);
62 public void testSubtreeWriterCreation() throws Exception {
63 final SubtreeWriter<?> forWriter = (SubtreeWriter<?>) SubtreeWriter.createForWriter(Sets.newHashSet(
64 DataObject1.DataObject11.IID,
65 DataObject1.DataObject11.DataObject111.IID,
66 DataObject1.DataObject12.IID),
69 assertEquals(writer.getManagedDataObjectType(), forWriter.getManagedDataObjectType());
70 assertEquals(3, forWriter.getHandledChildTypes().size());
74 public void testSubtreeWriterHandledTypes() throws Exception {
75 final SubtreeWriter<?> forWriter = (SubtreeWriter<?>) SubtreeWriter.createForWriter(Sets.newHashSet(
76 DataObject1.DataObject11.DataObject111.IID),
79 assertEquals(writer.getManagedDataObjectType(), forWriter.getManagedDataObjectType());
80 assertEquals(1, forWriter.getHandledChildTypes().size());
81 assertThat(forWriter.getHandledChildTypes(), hasItem(DataObject1.DataObject11.DataObject111.IID));
84 private abstract static class DataObject1 implements DataObject {
85 static InstanceIdentifier<DataObject1> IID = InstanceIdentifier.create(DataObject1.class);
86 private abstract static class DataObject11 implements DataObject, ChildOf<DataObject1> {
87 static InstanceIdentifier<DataObject11> IID = DataObject1.IID.child(DataObject11.class);
88 private abstract static class DataObject111 implements DataObject, ChildOf<DataObject11> {
89 static InstanceIdentifier<DataObject111> IID = DataObject11.IID.child(DataObject111.class);
92 private abstract static class DataObject12 implements DataObject, ChildOf<DataObject1> {
93 static InstanceIdentifier<DataObject12> IID = DataObject1.IID.child(DataObject12.class);