11c8638de659b861d779acb3d390eceeb5ce228b
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.context.schema.json;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25
26 import com.google.gson.JsonObject;
27 import com.worldturner.medeia.api.ValidationFailedException;
28 import java.util.ArrayList;
29 import java.util.Map;
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;
35
36 public class JsonSchemaHelperUnmarshalTest extends CommonTestData {
37
38     /**
39      * Test Boolean.
40      */
41     @Test
42     public void testBooleanUnmarshal() {
43         var schemaHelper = createSchema(BOOLEAN_SCHEMA);
44         assertThat(schemaHelper.createNewInstance(BOOLEAN_DATA)).isInstanceOf(Boolean.class).isEqualTo(Boolean.TRUE);
45     }
46
47     /**
48      * Test null.
49      */
50     @Test
51     public void testNullUnmarshal() {
52         var schemaHelper = createSchema(NULL_SCHEMA);
53         assertThat(schemaHelper.createNewInstance(NULL_DATA)).isEqualTo(null);
54     }
55
56     /**
57      * Test Array.
58      */
59     @Test
60     public void testArrayUnmarshal() {
61         var schemaHelper = createSchema(MEASUREMENTGROUPS_TYPE);
62         var obj = schemaHelper.createNewInstance(MEASUREMENTGROUPS);
63         assertThat(obj).isInstanceOf(ArrayList.class);
64     }
65
66     /**
67      * Test invlaid schema.
68      */
69     @Test
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");
76     }
77
78     /**
79      * Test Object unmarshal valid scenario using JSON Schema draft 04.
80      *
81      * @throws CoderException the coderException
82      */
83     @Test
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);
88     }
89
90     /**
91      * Test Object unmarshal valid scenario using JSON Schema draft 07.
92      *
93      * @throws CoderException the coderException
94      */
95     @Test
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);
100     }
101
102     /**
103      * Test Object unmarshal invalid scenario using JSON Schema draft 07.
104      *
105      * @throws CoderException the coderException
106      */
107     @Test
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");
114     }
115
116     /**
117      * Test createInstance using invalid format data.
118      *
119      * @throws CoderException the coderException
120      */
121     @Test
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");
126     }
127
128     /**
129      * Test Object unmarshal invalid - required field missing scenario.
130      *
131      * @throws CoderException the coderException
132      */
133     @Test
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");
140     }
141
142     /**
143      * Test Object unmarshal with optional field.
144      *
145      * @throws CoderException the coderException
146      */
147     @Test
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);
155     }
156
157     private Object validateAndUnmarshal(String schemaDef, Object data) {
158         var schemaHelper = createSchema(schemaDef);
159         if (data instanceof String) {
160             return schemaHelper.createNewInstance((String) data);
161         } else {
162             return schemaHelper.createNewInstance(data);
163         }
164     }
165
166 }