4a5b765a84b38368d59debda0e93ddb9011a3dd0
[ccsdk/sli/plugins.git] / sshapi-call-node / provider / src / test / java / jtest / org / onap / ccsdk / sli / plugins / sshapicall / TestJsonParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2018 Samsung Electronics. All rights
8  *                      reserved.
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package jtest.org.onap.ccsdk.sli.plugins.sshapicall;
25
26 import java.io.BufferedReader;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.Map;
33
34 import org.codehaus.jettison.json.JSONException;
35 import org.junit.Test;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
37 import org.onap.ccsdk.sli.plugins.sshapicall.model.JsonParser;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class TestJsonParser {
42
43     private static final Logger log = LoggerFactory.getLogger(TestJsonParser.class);
44
45     @Test
46     public void test() throws SvcLogicException, IOException {
47         BufferedReader in = new BufferedReader(
48                 new InputStreamReader(ClassLoader.getSystemResourceAsStream("test.json"))
49         );
50         StringBuilder b = new StringBuilder();
51         String line;
52         while ((line = in.readLine()) != null)
53             b.append(line).append('\n');
54         Map<String, String> mm = null;
55         try {
56             mm = JsonParser.convertToProperties(b.toString());
57         } catch (JSONException e){
58             throw new SvcLogicException(e.getMessage());
59         }
60
61         logProperties(mm);
62
63         in.close();
64     }
65
66     @Test(expected = NullPointerException.class)
67     public void testNullString() throws SvcLogicException {
68         Map<String, String> mm = null;
69         try {
70             mm = JsonParser.convertToProperties(null);
71         } catch (JSONException e){
72             throw new SvcLogicException(e.getMessage());
73         }
74     }
75
76     private void logProperties(Map<String, String> mm) {
77         List<String> ll = new ArrayList<>();
78         for (Object o : mm.keySet())
79             ll.add((String) o);
80         Collections.sort(ll);
81         log.info("Properties:");
82         for (String name : ll)
83             log.info("--- {}: {}", name, mm.get(name));
84     }
85 }