2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.context.schema.json;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import com.google.gson.JsonObject;
27 import com.worldturner.medeia.api.ValidationFailedException;
28 import java.util.ArrayList;
30 import org.junit.Test;
31 import org.onap.policy.apex.context.ContextRuntimeException;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
33 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
34 import org.onap.policy.common.utils.coder.CoderException;
36 public class JsonSchemaHelperUnmarshalTest extends CommonTestData {
42 public void testBooleanUnmarshal() {
43 var schemaHelper = createSchema(BOOLEAN_SCHEMA);
44 assertThat(schemaHelper.createNewInstance(BOOLEAN_DATA)).isInstanceOf(Boolean.class).isEqualTo(Boolean.TRUE);
51 public void testNullUnmarshal() {
52 var schemaHelper = createSchema(NULL_SCHEMA);
53 assertThat(schemaHelper.createNewInstance(NULL_DATA)).isEqualTo(null);
60 public void testArrayUnmarshal() {
61 var schemaHelper = createSchema(MEASUREMENTGROUPS_TYPE);
62 var obj = schemaHelper.createNewInstance(MEASUREMENTGROUPS);
63 assertThat(obj).isInstanceOf(ArrayList.class);
67 * Test invlaid schema.
70 public void testSchemaInvalid() {
71 String schemaDef = "{\"type\": \"object\"}";
72 final AxContextSchema jsonSchema =
73 new AxContextSchema(new AxArtifactKey("JsonObject", VERSION), JSON, schemaDef);
74 assertThatThrownBy(() -> new JsonSchemaHelper().init(testKey, jsonSchema))
75 .isInstanceOf(ContextRuntimeException.class).hasMessageContaining("schema is invalid");
79 * Test Object unmarshal valid scenario using JSON Schema draft 04.
81 * @throws CoderException the coderException
84 public void testObjectSchemaDraft04_valid() throws CoderException {
85 var dataAsObject = coder.decode(COMMONHEADER, Map.class);
86 var dataReturned = validateAndUnmarshal(COMMONHEADERTYPE_DRAFT04, COMMONHEADER);
87 assertThat(dataReturned).isEqualTo(dataAsObject);
91 * Test Object unmarshal valid scenario using JSON Schema draft 07.
93 * @throws CoderException the coderException
96 public void testObjectSchemaDraft07_valid() throws CoderException {
97 var dataAsObject = coder.decode(COMMONHEADER, Map.class);
98 var dataReturned = validateAndUnmarshal(COMMONHEADERTYPE_DRAFT07, COMMONHEADER);
99 assertThat(dataReturned).isEqualTo(dataAsObject);
103 * Test Object unmarshal invalid scenario using JSON Schema draft 07.
105 * @throws CoderException the coderException
108 public void testObjectSchemaDraft07_invalid() throws CoderException {
109 var dataAsObject = coder.decode(COMMONHEADER, JsonObject.class);
110 dataAsObject.addProperty("requestId", "abcd");
111 assertThatThrownBy(() -> validateAndUnmarshal(COMMONHEADERTYPE_DRAFT07, dataAsObject))
112 .isInstanceOf(ValidationFailedException.class)
113 .hasMessageContaining("Pattern ^[0-9]*-[0-9]*$ is not contained in text");
117 * Test createInstance using invalid format data.
119 * @throws CoderException the coderException
122 public void testCreateNewInstanceInvalid() throws CoderException {
123 var dataAsObject = coder.decode(COMMONHEADER, Map.class);
124 assertThatThrownBy(() -> validateAndUnmarshal(COMMONHEADERTYPE_DRAFT07, dataAsObject))
125 .isInstanceOf(ContextRuntimeException.class).hasMessageContaining("not an instance of JsonObject");
129 * Test Object unmarshal invalid - required field missing scenario.
131 * @throws CoderException the coderException
134 public void testObjectSchema_fieldMissing() throws CoderException {
135 var dataAsObject = coder.decode(COMMONHEADER, JsonObject.class);
136 dataAsObject.remove(TEST_ID);
137 assertThatThrownBy(() -> validateAndUnmarshal(COMMONHEADERTYPE_DRAFT07, dataAsObject))
138 .isInstanceOf(ValidationFailedException.class)
139 .hasMessageContaining("Required property testId is missing from object");
143 * Test Object unmarshal with optional field.
145 * @throws CoderException the coderException
148 public void testObjectSchema_OptionalField() throws CoderException {
149 var dataAsObject = coder.decode(COMMONHEADER, Map.class);
150 var dataAsjsonObject = coder.decode(COMMONHEADER, JsonObject.class);
151 dataAsObject.remove(TEST_ID);
152 dataAsjsonObject.remove(TEST_ID);
153 var dataReturned = validateAndUnmarshal(COMMONHEADERTYPE_WITH_OPTIONAL, dataAsjsonObject);
154 assertThat(dataReturned).isEqualTo(dataAsObject);
157 private Object validateAndUnmarshal(String schemaDef, Object data) {
158 var schemaHelper = createSchema(schemaDef);
159 if (data instanceof String) {
160 return schemaHelper.createNewInstance((String) data);
162 return schemaHelper.createNewInstance(data);