2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada. All rights reserved.
4 * Modifications Copyright (C) 2024 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.context.schema.json;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import com.google.gson.JsonObject;
28 import com.worldturner.medeia.api.ValidationFailedException;
29 import java.util.ArrayList;
31 import org.junit.jupiter.api.Test;
32 import org.onap.policy.apex.context.ContextRuntimeException;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
34 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
35 import org.onap.policy.common.utils.coder.CoderException;
37 class JsonSchemaHelperUnmarshalTest extends CommonTestData {
43 void testBooleanUnmarshal() {
44 var schemaHelper = createSchema(BOOLEAN_SCHEMA);
45 assertThat(schemaHelper.createNewInstance(BOOLEAN_DATA)).isInstanceOf(Boolean.class).isEqualTo(Boolean.TRUE);
52 void testNullUnmarshal() {
53 var schemaHelper = createSchema(NULL_SCHEMA);
54 assertThat(schemaHelper.createNewInstance(NULL_DATA)).isNull();
61 void testArrayUnmarshal() {
62 var schemaHelper = createSchema(MEASUREMENTGROUPS_TYPE);
63 var obj = schemaHelper.createNewInstance(MEASUREMENTGROUPS);
64 assertThat(obj).isInstanceOf(ArrayList.class);
68 * Test invalid schema.
71 void testSchemaInvalid() {
72 String schemaDef = "{\"type\": \"object\"}";
73 final AxContextSchema jsonSchema =
74 new AxContextSchema(new AxArtifactKey("JsonObject", VERSION), JSON, schemaDef);
75 var jsonSchemaHelper = new JsonSchemaHelper();
76 assertThatThrownBy(() -> jsonSchemaHelper.init(testKey, jsonSchema))
77 .isInstanceOf(ContextRuntimeException.class).hasMessageContaining("schema is invalid");
81 * Test Object unmarshal valid scenario using JSON Schema draft 04.
83 * @throws CoderException the coderException
86 void testObjectSchemaDraft04_valid() throws CoderException {
87 var dataAsObject = coder.decode(COMMONHEADER, Map.class);
88 var dataReturned = validateAndUnmarshal(COMMONHEADERTYPE_DRAFT04, COMMONHEADER);
89 assertThat(dataReturned).isEqualTo(dataAsObject);
93 * Test Object unmarshal valid scenario using JSON Schema draft 07.
95 * @throws CoderException the coderException
98 void testObjectSchemaDraft07_valid() throws CoderException {
99 var dataAsObject = coder.decode(COMMONHEADER, Map.class);
100 var dataReturned = validateAndUnmarshal(COMMONHEADERTYPE_DRAFT07, COMMONHEADER);
101 assertThat(dataReturned).isEqualTo(dataAsObject);
105 * Test Object unmarshal invalid scenario using JSON Schema draft 07.
107 * @throws CoderException the coderException
110 void testObjectSchemaDraft07_invalid() throws CoderException {
111 var dataAsObject = coder.decode(COMMONHEADER, JsonObject.class);
112 dataAsObject.addProperty("requestId", "abcd");
113 assertThatThrownBy(() -> validateAndUnmarshal(COMMONHEADERTYPE_DRAFT07, dataAsObject))
114 .isInstanceOf(ValidationFailedException.class)
115 .hasMessageContaining("Pattern ^[0-9]*-[0-9]*$ is not contained in text");
119 * Test createInstance using invalid format data.
121 * @throws CoderException the coderException
124 void testCreateNewInstanceInvalid() throws CoderException {
125 var dataAsObject = coder.decode(COMMONHEADER, Map.class);
126 assertThatThrownBy(() -> validateAndUnmarshal(COMMONHEADERTYPE_DRAFT07, dataAsObject))
127 .isInstanceOf(ContextRuntimeException.class).hasMessageContaining("not an instance of JsonObject");
131 * Test Object unmarshal invalid - required field missing scenario.
133 * @throws CoderException the coderException
136 void testObjectSchema_fieldMissing() throws CoderException {
137 var dataAsObject = coder.decode(COMMONHEADER, JsonObject.class);
138 dataAsObject.remove(TEST_ID);
139 assertThatThrownBy(() -> validateAndUnmarshal(COMMONHEADERTYPE_DRAFT07, dataAsObject))
140 .isInstanceOf(ValidationFailedException.class)
141 .hasMessageContaining("Required property testId is missing from object");
145 * Test Object unmarshal with optional field.
147 * @throws CoderException the coderException
150 void testObjectSchema_OptionalField() throws CoderException {
151 var dataAsObject = coder.decode(COMMONHEADER, Map.class);
152 var dataAsjsonObject = coder.decode(COMMONHEADER, JsonObject.class);
153 dataAsObject.remove(TEST_ID);
154 dataAsjsonObject.remove(TEST_ID);
155 var dataReturned = validateAndUnmarshal(COMMONHEADERTYPE_WITH_OPTIONAL, dataAsjsonObject);
156 assertThat(dataReturned).isEqualTo(dataAsObject);
159 private Object validateAndUnmarshal(String schemaDef, Object data) {
160 var schemaHelper = createSchema(schemaDef);
161 if (data instanceof String) {
162 return schemaHelper.createNewInstance((String) data);
164 return schemaHelper.createNewInstance(data);