aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro
diff options
context:
space:
mode:
authorrameshiyer27 <ramesh.murugan.iyer@est.tech>2024-10-14 16:09:34 +0100
committerrameshiyer27 <ramesh.murugan.iyer@est.tech>2024-10-22 21:08:59 +0100
commit6b164a94d894c969811c9388b6af8c9a1fca2be2 (patch)
treeb62eb7be108d5eb7faa9986c500c30fa7d653594 /plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro
parent85f95e93a19c778e72b6022a713f0eea55002585 (diff)
Improve coverage on apex-pdp
Issue-ID: POLICY-5059 Signed-off-by: rameshiyer27 <ramesh.murugan.iyer@est.tech> Change-Id: I3d8d71f4ee9db10fe43bfe51586156cf768a8560
Diffstat (limited to 'plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro')
-rw-r--r--plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroNullableMapperTest.java58
-rw-r--r--plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroObjMapperFactoryTest.java60
2 files changed, 118 insertions, 0 deletions
diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroNullableMapperTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroNullableMapperTest.java
new file mode 100644
index 000000000..27a0ca3bf
--- /dev/null
+++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroNullableMapperTest.java
@@ -0,0 +1,58 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2024 Nordix Foundation. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.plugins.context.schema.avro;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+
+import org.apache.avro.Schema;
+import org.junit.jupiter.api.Test;
+import org.onap.policy.apex.context.ContextRuntimeException;
+import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
+import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
+
+class AvroNullableMapperTest {
+
+ @Test
+ void avroNullableMapperTests() {
+ var avroObjectMapper = new AvroDirectObjectMapper();
+ var avroNullableMapper = new AvroNullableMapper(avroObjectMapper);
+ assertDoesNotThrow(() -> avroNullableMapper.init(new AxArtifactKey("test", "1.0.1"),
+ Schema.Type.BOOLEAN));
+
+ assertEquals("class java.lang.Boolean", avroNullableMapper.getJavaClass().toString());
+ assertThat(avroNullableMapper.getAvroType()).isEqualByComparingTo(Schema.Type.UNION);
+
+ var avroObjMapper = new AvroDirectObjectMapper();
+ avroObjMapper.init(new AxArtifactKey("test2", "1.1.2"), Schema.Type.BOOLEAN);
+ assertThrows(ContextRuntimeException.class, () -> avroNullableMapper.mapFromAvro(avroObjMapper));
+ assertNull(avroNullableMapper.mapFromAvro(null));
+
+ assertNull(avroNullableMapper.mapToAvro(null));
+ assertThrows(ApexRuntimeException.class, () -> avroNullableMapper.mapToAvro(avroObjMapper));
+
+ assertDoesNotThrow(() -> avroNullableMapper.createNewInstance(mock(Schema.class)));
+ }
+}
diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroObjMapperFactoryTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroObjMapperFactoryTest.java
new file mode 100644
index 000000000..4ac7a20be
--- /dev/null
+++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroObjMapperFactoryTest.java
@@ -0,0 +1,60 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2024 Nordix Foundation. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.plugins.context.schema.avro;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.List;
+import org.apache.avro.Schema;
+import org.junit.jupiter.api.Test;
+import org.onap.policy.apex.context.ContextRuntimeException;
+import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
+
+class AvroObjMapperFactoryTest {
+ @Test
+ void testObjMapperFactory() {
+ var objMapperFactory = new AvroObjectMapperFactory();
+ var key = new AxArtifactKey("test", "1.0.1");
+ var schema1 = Schema.createUnion(List.of(Schema.create(Schema.Type.NULL)));
+ assertThatThrownBy(() -> objMapperFactory.get(key, schema1))
+ .isInstanceOf(ContextRuntimeException.class)
+ .hasMessageContaining("Apex currently only supports UNION schemas with 2 options, "
+ + "one must be NULL");
+
+ var schema2 = mock(Schema.class);
+ when(schema2.getType()).thenReturn(Schema.Type.UNION);
+ var nullSchema = Schema.create(Schema.Type.NULL);
+ when(schema2.getTypes()).thenReturn(List.of(nullSchema, nullSchema));
+ assertThatThrownBy(() -> objMapperFactory.get(key, schema2))
+ .isInstanceOf(ContextRuntimeException.class)
+ .hasMessageContaining("Apex currently only supports UNION schema2 with 2 options, "
+ + "only one can be NULL, and the other cannot be another UNION");
+
+ var fixedSchema = Schema.createFixed("test1", "doc", "test", 2);
+ when(schema2.getTypes()).thenReturn(List.of(fixedSchema, nullSchema));
+ assertDoesNotThrow(() -> objMapperFactory.get(key, schema2));
+
+ }
+
+}