@SuppressWarnings({ "rawtypes", "unchecked" })
final Class<? extends Enum<?>> enumType = (Class) returnType;
return () -> {
- Map<String, Enum<?>> nameToValue = new HashMap<>();
- for (Enum<?> enumValue : enumType.getEnumConstants()) {
+ final Map<String, Enum<?>> nameToValue = new HashMap<>();
+ for (final Enum<?> enumValue : enumType.getEnumConstants()) {
nameToValue.put(enumValue.toString(), enumValue);
}
- Map<String, Enum<?>> yangNameToBinding = new HashMap<>();
- for (EnumPair yangValue : enumSchema.getValues()) {
+ final Map<String, Enum<?>> yangNameToBinding = new HashMap<>();
+ for (final EnumPair yangValue : enumSchema.getValues()) {
final String bindingName = JavaIdentifierNormalizer.normalizeSpecificIdentifier(yangValue.getName(),
- JavaIdentifier.CLASS);
+ JavaIdentifier.ENUM_VALUE);
final Enum<?> bindingVal = nameToValue.get(bindingName);
yangNameToBinding.put(yangValue.getName(), bindingVal);
}
}
@Override
- public Object deserialize(Object input) {
- Enum<?> value = yangValueToBinding.get(input);
+ public Object deserialize(final Object input) {
+ final Enum<?> value = yangValueToBinding.get(input);
Preconditions.checkArgument(value != null, "Invalid enumeration value %s. Valid values are %s", input,
yangValueToBinding.keySet());
return value;
}
@Override
- public Object serialize(Object input) {
+ public Object serialize(final Object input) {
Preconditions.checkArgument(getTypeClass().isInstance(input), "Input must be instance of %s", getTypeClass());
return yangValueToBinding.inverse().get(input);
}
--- /dev/null
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.javav2.dom.codec;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+import org.junit.Test;
+import org.opendaylight.mdsal.binding.javav2.dom.codec.api.BindingNormalizedNodeCodec;
+import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.NonCachingCodec;
+
+public class NonCachingCodecTest {
+
+ @SuppressWarnings({ "rawtypes", "unchecked", "resource" })
+ @Test
+ public void basicTest() throws Exception {
+ final BindingNormalizedNodeCodec codec = mock(BindingNormalizedNodeCodec.class);
+ doReturn(null).when(codec).serialize(null);
+ doReturn(null).when(codec).deserialize(null);
+ final NonCachingCodec nonCachingCodec = new NonCachingCodec<>(codec);
+ nonCachingCodec.serialize(null);
+ verify(codec).serialize(null);
+ nonCachingCodec.deserialize(null);
+ verify(codec).deserialize(null);
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.javav2.dom.codec.impl;
+
+import org.junit.Before;
+import org.opendaylight.mdsal.binding.javav2.runtime.context.BindingRuntimeContext;
+import org.opendaylight.mdsal.binding.javav2.runtime.context.ModuleInfoBackedContext;
+import org.opendaylight.mdsal.binding.javav2.runtime.reflection.BindingReflections;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+public abstract class AbstractBindingRuntimeTest {
+
+ private SchemaContext schemaContext;
+ private BindingRuntimeContext runtimeContext;
+
+ @Before
+ public void setup() {
+ final ModuleInfoBackedContext ctx = ModuleInfoBackedContext.create();
+ ctx.addModuleInfos(BindingReflections.loadModuleInfos());
+ schemaContext = ctx.tryToCreateSchemaContext().get();
+ runtimeContext = BindingRuntimeContext.create(ctx, schemaContext);
+
+ }
+
+ public SchemaContext getSchemaContext() {
+ return schemaContext;
+ }
+
+ public BindingRuntimeContext getRuntimeContext() {
+ return runtimeContext;
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.lang.reflect.Method;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.value.ValueTypeCodec;
+
+public class UnionValueOptionContextTest {
+
+ private static UnionValueOptionContext TEST_UVOC_1;
+ private static UnionValueOptionContext TEST_UVOC_2;
+
+ @Before
+ public void setUp() throws Exception {
+ final Method methodFoo1 = TestDataObject1.class.getMethod("foo");
+ final Method methodFoo2 = TestDataObject2.class.getMethod("foo");
+ TEST_UVOC_1 = new UnionValueOptionContext(TestUnion.class, TestDataObject1.class, methodFoo1,
+ ValueTypeCodec.EMPTY_CODEC);
+ TEST_UVOC_2 = new UnionValueOptionContext(TestUnion.class, TestDataObject2.class, methodFoo2,
+ ValueTypeCodec.EMPTY_CODEC);
+ }
+
+ @Test
+ public void hashCodeTest() throws Exception {
+ final Method methodFoo1 = TestDataObject1.class.getMethod("foo");
+ final UnionValueOptionContext test_uvoc = new UnionValueOptionContext(TestUnion.class, TestDataObject1.class,
+ methodFoo1, ValueTypeCodec.EMPTY_CODEC);
+
+ assertEquals("HashCode", test_uvoc.hashCode(), TEST_UVOC_1.hashCode());
+ assertNotEquals("HashCode", TEST_UVOC_1.hashCode(), TEST_UVOC_2.hashCode());
+ }
+
+ @Test
+ public void equalsTest() throws Exception {
+ final Method methodFoo1 = TestDataObject1.class.getMethod("foo");
+ final UnionValueOptionContext test_uvoc = new UnionValueOptionContext(TestUnion.class, TestDataObject1.class,
+ methodFoo1, ValueTypeCodec.EMPTY_CODEC);
+
+ assertTrue("Equals", TEST_UVOC_1.equals(test_uvoc));
+ assertFalse("Not equals", TEST_UVOC_1.equals(TEST_UVOC_2));
+ }
+
+ protected static final class TestDataObject1 {
+ public void foo() {
+ }
+ }
+
+ protected static final class TestDataObject2 {
+ public void foo() {
+ }
+ }
+
+ public static final class TestUnion {
+ public TestUnion(final TestDataObject1 arg) {
+ }
+
+ public TestUnion(final TestDataObject2 arg) {
+ }
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.value;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+
+import com.google.common.collect.ImmutableList;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
+
+public class EnumerationCodecTest {
+
+ private enum TestEnum {
+ TEST
+ }
+
+ @Test
+ public void basicTest() throws Exception {
+ final EnumPair pair = mock(EnumPair.class);
+ doReturn(TestEnum.TEST.name()).when(pair).getName();
+ doReturn(0).when(pair).getValue();
+ final EnumTypeDefinition definition = mock(EnumTypeDefinition.class);
+ doReturn(ImmutableList.of(pair)).when(definition).getValues();
+
+ final EnumerationCodec codec = EnumerationCodec.loader(TestEnum.class, definition).call();
+ assertEquals(codec.deserialize(codec.serialize(TestEnum.TEST)), TestEnum.TEST);
+ assertEquals(codec.serialize(codec.deserialize(TestEnum.TEST.name())), TestEnum.TEST.name());
+ }
+}