7f5446163056c564f5160bae52af35ea87a24913
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / util / TestCbamUtils.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
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
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util;
18
19 import com.google.gson.JsonObject;
20 import com.google.gson.JsonPrimitive;
21 import org.junit.Test;
22 import org.mockito.Mockito;
23 import org.slf4j.Logger;
24
25 import static junit.framework.TestCase.assertEquals;
26 import static junit.framework.TestCase.fail;
27 import static org.mockito.Mockito.verify;
28
29 public class TestCbamUtils {
30
31     /**
32      * test child of json object
33      */
34     @Test
35     public void testChild() throws Exception {
36         JsonObject parent = new JsonObject();
37         JsonObject child = new JsonObject();
38         parent.add("x", child);
39         assertEquals(child, CbamUtils.child(parent, "x"));
40     }
41
42     /**
43      * if child is not a json object error is propagated
44      */
45     @Test
46     public void testNonJsonObjectChild() throws Exception {
47         JsonObject parent = new JsonObject();
48         JsonPrimitive child = new JsonPrimitive("y");
49         parent.add("x", child);
50         try {
51             CbamUtils.child(parent, "x");
52             fail();
53         } catch (RuntimeException e) {
54             assertEquals("Not a JSON Object: \"y\"", e.getMessage());
55         }
56     }
57
58     /**
59      * if no child is present error is propagated
60      */
61     @Test
62     public void testMissingChild() throws Exception {
63         JsonObject parent = new JsonObject();
64         try {
65             CbamUtils.child(parent, "z");
66             fail();
67         } catch (RuntimeException e) {
68             assertEquals("Missing child z", e.getMessage());
69         }
70     }
71
72     /**
73      * test child of json object
74      */
75     @Test
76     public void testChildElement() throws Exception {
77         JsonObject parent = new JsonObject();
78         JsonPrimitive child = new JsonPrimitive("y");
79         parent.add("x", child);
80         assertEquals(child, CbamUtils.childElement(parent, "x"));
81     }
82
83     /**
84      * if no child is present error is propagated
85      */
86     @Test
87     public void testMissingChildElement() throws Exception {
88         JsonObject parent = new JsonObject();
89         try {
90             CbamUtils.childElement(parent, "z");
91             fail();
92         } catch (RuntimeException e) {
93             assertEquals("Missing child z", e.getMessage());
94         }
95     }
96
97
98     /**
99      * test fatal failure handling
100      */
101     @Test
102     public void testFatalFailure() throws Exception {
103         Exception expectedException = new Exception();
104         Logger logger = Mockito.mock(Logger.class);
105         try {
106             CbamUtils.fatalFailure(logger, "msg", expectedException);
107             fail();
108         } catch (RuntimeException e) {
109             assertEquals("msg", e.getMessage());
110             assertEquals(expectedException, e.getCause());
111             verify(logger).error("msg", expectedException);
112         }
113     }
114
115     /**
116      * test fatal failure handling with no wrapped exception
117      */
118     @Test
119     public void testFatalFailureWithNoException() throws Exception {
120         Logger logger = Mockito.mock(Logger.class);
121         try {
122             CbamUtils.fatalFailure(logger, "msg");
123             fail();
124         } catch (RuntimeException e) {
125             assertEquals("msg", e.getMessage());
126             verify(logger).error("msg");
127         }
128     }
129
130     @Test
131     public void useStaticWay() {
132         TestUtil.coveragePrivateConstructorForClassesWithStaticMethodsOnly(CbamUtils.class);
133     }
134
135
136 }