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.translate.util;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
22 import com.google.common.io.ByteStreams;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.StandardOpenOption;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.MockitoAnnotations;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
34 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
35 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
37 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
38 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
40 public class JsonUtilsTest {
42 public static final String NAMESPACE = "urn:opendaylight:params:xml:ns:yang:test:persistence";
44 private static final QName ROOT_QNAME = QName.create("urn:ietf:params:xml:ns:netconf:base:1.0", "data");
45 private static final QName TOP_CONTAINER_NAME = QName.create(NAMESPACE, "2015-01-05", "top-container");
46 private static final QName TOP_CONTAINER2_NAME = QName.create(NAMESPACE, "2015-01-05", "top-container2");
47 private static final QName STRING_LEAF_QNAME = QName.create(TOP_CONTAINER_NAME, "string");
49 private Path tmpPersistFile;
50 private EffectiveSchemaContext effectiveSchemaContext;
53 public void setUp() throws Exception {
54 MockitoAnnotations.initMocks(this);
55 tmpPersistFile = Files.createTempFile("testing-hc-persistence", "json");
57 final CrossSourceStatementReactor.BuildAction buildAction = YangInferencePipeline.RFC6020_REACTOR.newBuild();
58 buildAction.addSource(new YangStatementSourceImpl(getClass().getResourceAsStream("/test-persistence.yang")));
59 effectiveSchemaContext = buildAction.buildEffective();
63 public void testPersist() throws Exception {
65 NormalizedNode<?, ?> data = getData("testing");
66 JsonUtils.writeJsonRoot(data, effectiveSchemaContext, Files.newOutputStream(tmpPersistFile, StandardOpenOption.CREATE));
67 assertTrue(Files.exists(tmpPersistFile));
69 String persisted = new String(Files.readAllBytes(tmpPersistFile));
71 new String(ByteStreams.toByteArray(getClass().getResourceAsStream("/expected-persisted-output.txt")));
73 assertEquals(expected, persisted);
75 data = getData("testing2");
76 JsonUtils.writeJsonRoot(data, effectiveSchemaContext, Files.newOutputStream(tmpPersistFile, StandardOpenOption.CREATE));
77 persisted = new String(Files.readAllBytes(tmpPersistFile));
78 assertEquals(expected.replace("testing", "testing2"), persisted);
80 // File has to stay even after close
81 assertTrue(Files.exists(tmpPersistFile));
85 public void testRestore() throws Exception {
86 final ContainerNode normalizedNodeOptional = JsonUtils
87 .readJsonRoot(effectiveSchemaContext, getClass().getResourceAsStream("/expected-persisted-output.txt"));
88 assertEquals(getData("testing"), normalizedNodeOptional);
91 @Test(expected = IllegalArgumentException.class)
92 public void testRestoreInvalidFile() throws Exception {
93 JsonUtils.readJsonRoot(effectiveSchemaContext, getClass().getResourceAsStream("/test-persistence.yang"));
96 private ContainerNode getData(final String stringValue) {
97 return Builders.containerBuilder()
98 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(ROOT_QNAME))
99 .withChild(Builders.containerBuilder()
100 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_NAME))
101 .withChild(ImmutableNodes.leafNode(STRING_LEAF_QNAME, stringValue))
103 .withChild(Builders.containerBuilder()
104 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER2_NAME))
105 .withChild(ImmutableNodes.leafNode(STRING_LEAF_QNAME, stringValue))