81df007898fbaf4484ec7aa4a75699160556d974
[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.ItemCodeIdentity;
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     @Test
51     public void testYangMapperDeser2() {
52         AddressLocation al = null;
53
54         try {
55             al = MAPPER.readValue(
56                     "{\n"
57                             + "    \"address-type\": \"OFFICE\",\n"
58                             + "    \"delivery-date-time\": \"2022-03-15T11:12:13.890Z\",\n"
59                             + "    \"delivery-url\": \"delivery.uri\",\n"
60                             + "    \"test-id\": \"org.opendaylight.yang.gen.v1.urn.test.yang.utils.norev.ItemCodeIdentity\""
61                             + "}",
62                     AddressLocation.class);
63         } catch (JsonProcessingException e) {
64             e.printStackTrace();
65         }
66         assertEquals(AddressType.OFFICE, al.getAddressType());
67         assertEquals("2022-03-15T11:12:13.890Z", al.getDeliveryDateTime().getValue());
68         assertEquals(ItemCodeIdentity.VALUE, al.getTestId());
69         System.out.println("Delivery Date = " + al.getDeliveryDateTime().getValue());
70         System.out.println(al.getItemList());
71         System.out.println(al.getDeliveryUrl().getValue());
72     }
73     @Test
74     public void testYangMapperDeser() {
75         AddressLocation al = null;
76
77         try {
78             al = MAPPER.readValue(
79                     "{\n"
80                     + "    \"address-type\": \"OFFICE\",\n"
81                     + "    \"delivery-date-time\": \"2022-03-15T11:12:13.890Z\",\n"
82                     + "    \"delivery-url\": \"delivery.uri\",\n"
83                     + "    \"item-list\": [\n"
84                     + "        {\n"
85                     + "            \"item-key\": \"org.opendaylight.yang.gen.v1.urn.test.yang.utils.norev.ItemCodeIdentity\"\n"
86                     + "        }\n"
87                     + "    ]\n"
88                     + "}",
89                     AddressLocation.class);
90         } catch (JsonProcessingException e) {
91             e.printStackTrace();
92         }
93         assertEquals(AddressType.OFFICE, al.getAddressType());
94         assertEquals("2022-03-15T11:12:13.890Z", al.getDeliveryDateTime().getValue());
95         System.out.println("Delivery Date = " + al.getDeliveryDateTime().getValue());
96         System.out.println(al.getItemList());
97         System.out.println(al.getDeliveryUrl().getValue());
98     }
99
100     @Test
101     public void testYangMapperSer() {
102         Map<ItemListKey, ItemList> items = new HashMap<ItemListKey, ItemList>();
103         ItemList il = new ItemListBuilder().setItemKey(ItemCodeIdentity.VALUE).build();
104         items.put(new ItemListKey(ItemCodeIdentity.VALUE), il);
105
106         Uri uri = new Uri("delivery.uri");
107
108         AddressLocation al = new AddressLocationBuilder().setId("99").setAddressType(AddressType.HOME)
109                 .setDeliveryDateTime(new DateAndTime("2022-03-15T11:12:13.890Z")).setItemList(items)
110                 .setDeliveryUrl(uri).build();
111         String str = null;
112
113         try {
114             str = MAPPER.writeValueAsString(al);
115         } catch (JsonProcessingException e) {
116             e.printStackTrace();
117         }
118         assertEquals("HOME", new JSONObject(str).getString("address-type"));
119         assertEquals("2022-03-15T11:12:13.890Z", new JSONObject(str).getString("delivery-date-time"));
120         System.out.println(new JSONObject(str).getJSONArray("item-list"));
121         System.out.println(str);
122     }
123 }