2 * Copyright (c) 2016, 2017 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 java.util.Collections;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.MockitoAnnotations;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
39 public class JsonUtilsTest {
41 public static final String NAMESPACE = "urn:opendaylight:params:xml:ns:yang:test:persistence";
43 private static final QName ROOT_QNAME = QName.create("urn:ietf:params:xml:ns:netconf:base:1.0", "data");
44 private static final QName TOP_CONTAINER_NAME = QName.create(NAMESPACE, "2015-01-05", "top-container");
45 private static final QName TOP_CONTAINER2_NAME = QName.create(NAMESPACE, "2015-01-05", "top-container2");
46 private static final QName STRING_LEAF_QNAME = QName.create(TOP_CONTAINER_NAME, "string");
48 private Path tmpPersistFile;
49 private SchemaContext schemaContext;
52 public void setUp() throws Exception {
53 MockitoAnnotations.initMocks(this);
54 tmpPersistFile = Files.createTempFile("testing-hc-persistence", "json");
55 schemaContext = YangParserTestUtils.parseYangResource("/test-persistence.yang");
59 public void testPersist() throws Exception {
61 NormalizedNode<?, ?> data = getData("testing");
62 JsonUtils.writeJsonRoot(data, schemaContext, Files.newOutputStream(tmpPersistFile, StandardOpenOption.CREATE));
63 assertTrue(Files.exists(tmpPersistFile));
65 String persisted = new String(Files.readAllBytes(tmpPersistFile));
67 new String(ByteStreams.toByteArray(getClass().getResourceAsStream("/expected-persisted-output.txt")));
69 assertEquals(expected, persisted);
71 data = getData("testing2");
72 JsonUtils.writeJsonRoot(data, schemaContext, Files.newOutputStream(tmpPersistFile, StandardOpenOption.CREATE));
73 persisted = new String(Files.readAllBytes(tmpPersistFile));
74 assertEquals(expected.replace("testing", "testing2"), persisted);
76 // File has to stay even after close
77 assertTrue(Files.exists(tmpPersistFile));
81 public void testRestore() throws Exception {
82 final ContainerNode normalizedNodeOptional = JsonUtils
83 .readJsonRoot(schemaContext, getClass().getResourceAsStream("/expected-persisted-output.txt"));
84 assertEquals(getData("testing"), normalizedNodeOptional);
87 @Test(expected = IllegalArgumentException.class)
88 public void testRestoreInvalidFile() throws Exception {
89 JsonUtils.readJsonRoot(schemaContext, getClass().getResourceAsStream("/test-persistence.yang"));
92 private ContainerNode getData(final String stringValue) {
93 return Builders.containerBuilder()
94 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(ROOT_QNAME))
95 .withChild(Builders.containerBuilder()
96 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_NAME))
97 .withChild(ImmutableNodes.leafNode(STRING_LEAF_QNAME, stringValue))
99 .withChild(Builders.containerBuilder()
100 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER2_NAME))
101 .withChild(ImmutableNodes.leafNode(STRING_LEAF_QNAME, stringValue))