Henry Yu | 0e24d50 | 2017-04-26 14:15:38 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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 | package org.onosproject.restconf.utils; |
| 17 | |
| 18 | import static org.junit.After; |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Before; |
| 21 | import static org.junit.Test; |
| 22 | |
| 23 | /** |
| 24 | * Unit tests for RESTCONF utils. |
| 25 | */ |
| 26 | public class RestconfUtilsTest { |
| 27 | |
| 28 | private static final String testJson = "{\"alpha\":\"abc\",\"beta\":123,\"gamma\":true}"; |
| 29 | |
| 30 | @Before |
| 31 | public void setUp() throws Exception { |
| 32 | |
| 33 | } |
| 34 | |
| 35 | @After |
| 36 | public void tearDown() throws Exception { |
| 37 | |
| 38 | } |
| 39 | |
| 40 | @Test |
| 41 | public void convertInputStreamToObjectNode() throws Exception { |
| 42 | InputStream inputStream = IOUtils.toInputStream(testJson); |
| 43 | ObjectNode testNode = ParseUtils.convertInputStreamToObjectNode(inputStream); |
| 44 | ObjectMapper mapper = new ObjectMapper(); |
| 45 | ObjectNode compareNode = mapper.createObjectNode() |
| 46 | .put("alpha", "abc") |
| 47 | .put("beta", 123) |
| 48 | .put("gamma", true); |
| 49 | assertEquals(testNode, compareNode); |
| 50 | } |
| 51 | |
| 52 | @Test |
| 53 | public void convertObjectNodeToInputStream() throws Exception { |
| 54 | ObjectMapper mapper = new ObjectMapper(); |
| 55 | ObjectNode compareNode = mapper.createObjectNode() |
| 56 | .put("alpha", "abc") |
| 57 | .put("beta", 123) |
| 58 | .put("gamma", true); |
| 59 | InputStream inputStream = ParseUtils.convertObjectNodeToInputStream(compareNode); |
| 60 | String compareJson = IOUtils.toString(inputStream); |
| 61 | assertEquals(testJson, compareJson); |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | public void convertJsonToDataNode() throws Exception { |
| 66 | ObjectMapper mapper = new ObjectMapper(); |
| 67 | ObjectNode compareNode = mapper.createObjectNode() |
| 68 | .put("alpha", "abc") |
| 69 | .put("beta", 123) |
| 70 | .put("gamma", true); |
| 71 | ResourceData resourceData = ParseUtils.convertJsonToDataNode("/xyz", compareNode); |
| 72 | ObjectNode testNode = ParseUtils.convertDataNodeToJson(resourceData.resourceId(), resourceData.dataNode()); |
| 73 | assertEquals(testNode, compareNode); |
| 74 | } |
| 75 | |
| 76 | } |