2e51e8404848d5632282874a04595722978dd595
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.yang.mapper;
23
24 import static org.junit.Assert.assertEquals;
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import java.util.HashMap;
27 import java.util.Map;
28 import org.json.JSONObject;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.IdentifierDeserializer;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
34 import org.opendaylight.yang.gen.v1.urn.test.yang.utils.norev.AddressLocation;
35 import org.opendaylight.yang.gen.v1.urn.test.yang.utils.norev.AddressLocationBuilder;
36 import org.opendaylight.yang.gen.v1.urn.test.yang.utils.norev.AddressType;
37 import org.opendaylight.yang.gen.v1.urn.test.yang.utils.norev.ItemCode;
38 import org.opendaylight.yang.gen.v1.urn.test.yang.utils.norev.address.location.entity.ItemList;
39 import org.opendaylight.yang.gen.v1.urn.test.yang.utils.norev.address.location.entity.ItemListBuilder;
40 import org.opendaylight.yang.gen.v1.urn.test.yang.utils.norev.address.location.entity.ItemListKey;
41
42 public class TestYangToolsMapper {
43
44     private static final YangToolsMapper MAPPER = new YangToolsMapper();
45
46     @Before
47     public void init() {
48         MAPPER.addKeyDeserializer(ItemListKey.class, new IdentifierDeserializer());
49     }
50
51     @Test
52     public void testYangMapperDeser() {
53         AddressLocation al = null;
54
55         try {
56             al = MAPPER.readValue(
57                     "{\n"
58                     + "    \"address-type\": \"OFFICE\",\n"
59                     + "    \"delivery-date-time\": \"2022-03-15T11:12:13.890Z\",\n"
60                     + "    \"delivery-url\": \"delivery.uri\",\n"
61                     + "    \"item-list\": [\n"
62                     + "        {\n"
63                     + "            \"item-key\": \"org.opendaylight.yang.gen.v1.urn.test.yang.utils.norev.ItemCode\"\n"
64                     + "        }\n"
65                     + "    ]\n"
66                     + "}",
67                     AddressLocation.class);
68         } catch (JsonProcessingException e) {
69             e.printStackTrace();
70         }
71         assertEquals(AddressType.OFFICE, al.getAddressType());
72         assertEquals("2022-03-15T11:12:13.890Z", al.getDeliveryDateTime().getValue());
73         System.out.println("Delivery Date = " + al.getDeliveryDateTime().getValue());
74         System.out.println(al.getItemList());
75         System.out.println(al.getDeliveryUrl().getValue());
76     }
77
78     @Test
79     public void testYangMapperSer() {
80         Map<ItemListKey, ItemList> items = new HashMap<ItemListKey, ItemList>();
81         ItemList il = new ItemListBuilder().setItemKey(ItemCode.class).build();
82         items.put(new ItemListKey(ItemCode.class), il);
83
84         Uri uri = new Uri("delivery.uri");
85
86         AddressLocation al = new AddressLocationBuilder().setId("99").setAddressType(AddressType.HOME)
87                 .setDeliveryDateTime(new DateAndTime("2022-03-15T11:12:13.890Z")).setItemList(items)
88                 .setDeliveryUrl(uri).build();
89         String str = null;
90
91         try {
92             str = MAPPER.writeValueAsString(al);
93         } catch (JsonProcessingException e) {
94             e.printStackTrace();
95         }
96         assertEquals("HOME", new JSONObject(str).getString("address-type"));
97         assertEquals("2022-03-15T11:12:13.890Z", new JSONObject(str).getString("delivery-date-time"));
98         System.out.println(new JSONObject(str).getJSONArray("item-list"));
99         System.out.println(str);
100     }
101 }