summaryrefslogtreecommitdiffstats
path: root/sdnr/wt/common-yang/utils
diff options
context:
space:
mode:
Diffstat (limited to 'sdnr/wt/common-yang/utils')
-rw-r--r--sdnr/wt/common-yang/utils/pom.xml6
-rw-r--r--sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/serialize/EnumSerializer.java9
-rw-r--r--sdnr/wt/common-yang/utils/src/test/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/TestMapper.java92
3 files changed, 106 insertions, 1 deletions
diff --git a/sdnr/wt/common-yang/utils/pom.xml b/sdnr/wt/common-yang/utils/pom.xml
index 5d5ceb1e6..bed9d25a1 100644
--- a/sdnr/wt/common-yang/utils/pom.xml
+++ b/sdnr/wt/common-yang/utils/pom.xml
@@ -73,6 +73,12 @@
<artifactId>mdsal-dom-api</artifactId>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>sdnr-wt-data-provider-model</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/serialize/EnumSerializer.java b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/serialize/EnumSerializer.java
index 55a20d6c0..8959e913a 100644
--- a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/serialize/EnumSerializer.java
+++ b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/serialize/EnumSerializer.java
@@ -25,12 +25,19 @@ import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
+import org.opendaylight.yangtools.yang.binding.Enumeration;
@SuppressWarnings("rawtypes")
public class EnumSerializer extends JsonSerializer<Enum> {
@Override
public void serialize(Enum value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
- gen.writeString(value.name());
+ //sadly a seperate serializer for class Enumeration doesn't work, so we have to catch it here
+ if(value instanceof Enumeration) {
+ gen.writeString(((Enumeration)value).getName());
+ }
+ else {
+ gen.writeString(value.name());
+ }
}
}
diff --git a/sdnr/wt/common-yang/utils/src/test/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/TestMapper.java b/sdnr/wt/common-yang/utils/src/test/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/TestMapper.java
new file mode 100644
index 000000000..2d2e0114c
--- /dev/null
+++ b/sdnr/wt/common-yang/utils/src/test/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/TestMapper.java
@@ -0,0 +1,92 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP : ccsdk features
+ * ================================================================================
+ * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
+ * 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.
+ * ============LICENSE_END=========================================================
+ *
+ */
+package org.onap.ccsdk.features.sdnr.wt.yang.mapper;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import org.json.JSONObject;
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementConnection;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementConnectionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementDeviceType;
+
+public class TestMapper {
+
+ private static final YangToolsMapper MAPPER = new YangToolsMapper();
+
+ @Test
+ public void testYangGenEnumMapperDeser() {
+ NetworkElementConnection con = null;
+ try {
+ con = MAPPER.readValue("{\"device-type\":\"O-RAN\"}", NetworkElementConnection.class);
+ } catch (JsonProcessingException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ assertEquals(NetworkElementDeviceType.ORAN, con.getDeviceType());
+ try {
+ con = MAPPER.readValue("{\"device-type\":\"ORAN\"}", NetworkElementConnection.class);
+ } catch (JsonProcessingException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ assertEquals(NetworkElementDeviceType.ORAN, con.getDeviceType());
+ try {
+ con = MAPPER.readValue("{\"device-type\":\"O-ROADM\"}", NetworkElementConnection.class);
+ } catch (JsonProcessingException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ assertEquals(NetworkElementDeviceType.OROADM, con.getDeviceType());
+ try {
+ con = MAPPER.readValue("{\"device-type\":\"O-ROADM\"}", NetworkElementConnection.class);
+ } catch (JsonProcessingException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ assertEquals(NetworkElementDeviceType.OROADM, con.getDeviceType());
+ }
+
+ @Test
+ public void testYangGenEnumMapperSer() {
+ NetworkElementConnection con =
+ new NetworkElementConnectionBuilder().setDeviceType(NetworkElementDeviceType.ORAN).build();
+ String str = null;
+ try {
+ str = MAPPER.writeValueAsString(con);
+ } catch (JsonProcessingException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ assertEquals("O-RAN", new JSONObject(str).getString("device-type"));
+ con = new NetworkElementConnectionBuilder().setDeviceType(NetworkElementDeviceType.OROADM).build();
+ str = null;
+ try {
+ str = MAPPER.writeValueAsString(con);
+ } catch (JsonProcessingException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ assertEquals("O-ROADM", new JSONObject(str).getString("device-type"));
+ }
+}