2 * Copyright 2016-2017, Nokia Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util;
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;
25 import static junit.framework.TestCase.assertEquals;
26 import static junit.framework.TestCase.fail;
27 import static org.mockito.Mockito.verify;
29 public class TestCbamUtils {
32 * test child of json object
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"));
43 * if child is not a json object error is propagated
46 public void testNonJsonObjectChild() throws Exception {
47 JsonObject parent = new JsonObject();
48 JsonPrimitive child = new JsonPrimitive("y");
49 parent.add("x", child);
51 CbamUtils.child(parent, "x");
53 } catch (RuntimeException e) {
54 assertEquals("Not a JSON Object: \"y\"", e.getMessage());
59 * if no child is present error is propagated
62 public void testMissingChild() throws Exception {
63 JsonObject parent = new JsonObject();
65 CbamUtils.child(parent, "z");
67 } catch (RuntimeException e) {
68 assertEquals("Missing child z", e.getMessage());
73 * test child of json object
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"));
84 * if no child is present error is propagated
87 public void testMissingChildElement() throws Exception {
88 JsonObject parent = new JsonObject();
90 CbamUtils.childElement(parent, "z");
92 } catch (RuntimeException e) {
93 assertEquals("Missing child z", e.getMessage());
99 * test fatal failure handling
102 public void testFatalFailure() throws Exception {
103 Exception expectedException = new Exception();
104 Logger logger = Mockito.mock(Logger.class);
105 RuntimeException e = CbamUtils.buildFatalFailure(logger, "msg", expectedException);
106 assertEquals("msg", e.getMessage());
107 assertEquals(expectedException, e.getCause());
108 verify(logger).error("msg", expectedException);
112 * test fatal failure handling with no wrapped exception
115 public void testFatalFailureWithNoException() throws Exception {
116 Logger logger = Mockito.mock(Logger.class);
117 RuntimeException e = CbamUtils.buildFatalFailure(logger, "msg");
118 assertEquals("msg", e.getMessage());
119 verify(logger).error("msg");
123 public void useStaticWay() {
124 TestUtil.coveragePrivateConstructorForClassesWithStaticMethodsOnly(CbamUtils.class);