Deprecate NodeIdentifierWithPredicates constructors
[yangtools.git] / yang / yang-data-api / src / test / java / org / opendaylight / yangtools / yang / data / api / schema / stream / NormalizedNodeWriterTest.java
1 /*
2  * Copyright (c) 2016 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.yangtools.yang.data.api.schema.stream;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.mockito.ArgumentMatchers.any;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18
19 import com.google.common.collect.ImmutableSet;
20 import java.io.IOException;
21 import java.net.URI;
22 import java.util.Optional;
23 import java.util.Set;
24 import javax.xml.transform.dom.DOMSource;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.common.QNameModule;
29 import org.opendaylight.yangtools.yang.common.Revision;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
33 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.YangModeledAnyXmlNode;
48
49 public class NormalizedNodeWriterTest {
50
51     private QNameModule bazModule;
52
53     private QName myKeyedList;
54     private QName myKeyLeaf;
55     private QName myLeafList;
56
57     @Before
58     public void setUp() {
59         bazModule = QNameModule.create(URI.create("baz-namespace"), Revision.of("1970-01-01"));
60         myKeyedList = QName.create(bazModule, "my-keyed-list");
61         myKeyLeaf = QName.create(bazModule, "my-key-leaf");
62         myLeafList = QName.create(bazModule, "my-leaf-list");
63     }
64
65     @Test
66     public void testNormalizedNodeWriter() throws IOException {
67         final NormalizedNodeStreamWriter loggingNormalizedNodeStreamWriter = new LoggingNormalizedNodeStreamWriter();
68         final NormalizedNodeWriter orderedNormalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(
69                 loggingNormalizedNodeStreamWriter);
70
71         assertEquals(loggingNormalizedNodeStreamWriter, orderedNormalizedNodeWriter.getWriter());
72
73         final NormalizedNode<?, ?> mockedNormalizedNode = mock(NormalizedNode.class);
74         try {
75             orderedNormalizedNodeWriter.write(mockedNormalizedNode);
76             fail("An IllegalStateException should have been thrown!");
77         } catch (IllegalStateException ex) {
78             assertTrue(ex.getMessage().startsWith("It wasn't possible to serialize node"));
79         }
80
81         final NormalizedNode<?, ?> mockedLeafSetEntryNode = mock(LeafSetEntryNode.class);
82         doReturn(new NodeWithValue<>(myLeafList, "leaflist-value-1")).when(mockedLeafSetEntryNode).getIdentifier();
83         doReturn("leaflist-value-1").when(mockedLeafSetEntryNode).getValue();
84         assertNotNull(orderedNormalizedNodeWriter.write(mockedLeafSetEntryNode));
85
86         final NormalizedNode<?, ?> mockedLeafNode = mock(LeafNode.class);
87         doReturn("leaf-value-1").when(mockedLeafNode).getValue();
88         assertNotNull(orderedNormalizedNodeWriter.write(mockedLeafNode));
89
90         final NormalizedNode<?, ?> mockedAnyXmlNode = mock(AnyXmlNode.class);
91         doReturn(new DOMSource()).when(mockedAnyXmlNode).getValue();
92         assertNotNull(orderedNormalizedNodeWriter.write(mockedAnyXmlNode));
93
94         final NormalizedNode<?, ?> mockedContainerNode = mock(ContainerNode.class);
95         assertNotNull(orderedNormalizedNodeWriter.write(mockedContainerNode));
96
97         final NormalizedNode<?, ?> mockedYangModeledAnyXmlNode = mock(YangModeledAnyXmlNode.class);
98         assertNotNull(orderedNormalizedNodeWriter.write(mockedYangModeledAnyXmlNode));
99
100         final MapEntryNode mockedMapEntryNode = mock(MapEntryNode.class);
101         doReturn(NodeIdentifierWithPredicates.of(myKeyedList, myKeyLeaf, "list-key-value-1"))
102                 .when(mockedMapEntryNode).getIdentifier();
103         doReturn(Optional.empty()).when(mockedMapEntryNode).getChild(any(NodeIdentifier.class));
104         assertNotNull(orderedNormalizedNodeWriter.write(mockedMapEntryNode));
105
106         final UnkeyedListEntryNode mockedUnkeyedListEntryNode = mock(UnkeyedListEntryNode.class);
107         assertNotNull(orderedNormalizedNodeWriter.write(mockedUnkeyedListEntryNode));
108
109         final ChoiceNode mockedChoiceNode = mock(ChoiceNode.class);
110         assertNotNull(orderedNormalizedNodeWriter.write(mockedChoiceNode));
111
112         final AugmentationNode mockedAugmentationNode = mock(AugmentationNode.class);
113         assertNotNull(orderedNormalizedNodeWriter.write(mockedAugmentationNode));
114
115         final UnkeyedListNode mockedUnkeyedListNode = mock(UnkeyedListNode.class);
116         final Set<?> value = ImmutableSet.builder().add(mockedUnkeyedListEntryNode).build();
117         doReturn(value).when(mockedUnkeyedListNode).getValue();
118         assertNotNull(orderedNormalizedNodeWriter.write(mockedUnkeyedListNode));
119
120         final OrderedMapNode mockedOrderedMapNode = mock(OrderedMapNode.class);
121         assertNotNull(orderedNormalizedNodeWriter.write(mockedOrderedMapNode));
122
123         final MapNode mockedMapNode = mock(MapNode.class);
124         assertNotNull(orderedNormalizedNodeWriter.write(mockedMapNode));
125
126         final OrderedLeafSetNode<?> mockedOrderedLeafSetNode = mock(OrderedLeafSetNode.class);
127         assertNotNull(orderedNormalizedNodeWriter.write(mockedOrderedLeafSetNode));
128
129         final LeafSetNode<?> mockedLeafSetNode = mock(LeafSetNode.class);
130         assertNotNull(orderedNormalizedNodeWriter.write(mockedLeafSetNode));
131
132         orderedNormalizedNodeWriter.flush();
133         orderedNormalizedNodeWriter.close();
134
135         final NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(
136                 loggingNormalizedNodeStreamWriter, false);
137
138         assertNotNull(normalizedNodeWriter.write(mockedMapEntryNode));
139
140         normalizedNodeWriter.flush();
141         normalizedNodeWriter.close();
142     }
143 }