From f4600723aa5e0fa99905eacd3338b33728a6c68f Mon Sep 17 00:00:00 2001 From: Jan Srnicek Date: Wed, 26 Oct 2016 10:14:11 +0200 Subject: HONEYCOMB-266 - Test data injection with @InjectTestData Field/Method param injection of data from json files that are bindable/parsable by provided yang schema Change-Id: I726ef5d92e85d93d1e48175287b6192538965dd5 Signed-off-by: Maros Marsalek Signed-off-by: Jan Srnicek --- .../test/tools/annotations/InjectTestData.java | 33 +++++++ .../tools/annotations/InjectablesProcessor.java | 101 +++++++++++++++++++++ .../tools/annotations/SchemaContextProvider.java | 27 ++++++ 3 files changed, 161 insertions(+) create mode 100644 infra/test-utils/test-tools/src/main/java/io/fd/honeycomb/test/tools/annotations/InjectTestData.java create mode 100644 infra/test-utils/test-tools/src/main/java/io/fd/honeycomb/test/tools/annotations/InjectablesProcessor.java create mode 100644 infra/test-utils/test-tools/src/main/java/io/fd/honeycomb/test/tools/annotations/SchemaContextProvider.java (limited to 'infra/test-utils/test-tools/src/main/java/io/fd/honeycomb/test/tools/annotations') diff --git a/infra/test-utils/test-tools/src/main/java/io/fd/honeycomb/test/tools/annotations/InjectTestData.java b/infra/test-utils/test-tools/src/main/java/io/fd/honeycomb/test/tools/annotations/InjectTestData.java new file mode 100644 index 000000000..e30d82ce0 --- /dev/null +++ b/infra/test-utils/test-tools/src/main/java/io/fd/honeycomb/test/tools/annotations/InjectTestData.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * 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. + */ + +package io.fd.honeycomb.test.tools.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.FIELD, ElementType.PARAMETER}) +public @interface InjectTestData { + + String NO_ID = "NO_ID"; + + String resourcePath(); + + String id() default NO_ID; +} diff --git a/infra/test-utils/test-tools/src/main/java/io/fd/honeycomb/test/tools/annotations/InjectablesProcessor.java b/infra/test-utils/test-tools/src/main/java/io/fd/honeycomb/test/tools/annotations/InjectablesProcessor.java new file mode 100644 index 000000000..c364938ed --- /dev/null +++ b/infra/test-utils/test-tools/src/main/java/io/fd/honeycomb/test/tools/annotations/InjectablesProcessor.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * 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. + */ + +package io.fd.honeycomb.test.tools.annotations; + +import static io.fd.honeycomb.test.tools.annotations.InjectTestData.NO_ID; + +import com.google.common.base.Optional; +import java.lang.reflect.Field; +import java.lang.reflect.Parameter; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import org.apache.commons.lang3.reflect.FieldUtils; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hc.data.rev150105.$YangModuleInfoImpl; +import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.YangModuleInfo; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; + +/** + * Common logic for @InjectTestData + */ +public interface InjectablesProcessor { + + default List injectableFields(final Class testClass) { + return FieldUtils.getFieldsListWithAnnotation(testClass, InjectTestData.class); + } + + default boolean isInjectable(final Parameter parameter) { + return parameter.isAnnotationPresent(InjectTestData.class); + } + + default String resourcePath(final Field field) { + return field.getAnnotation(InjectTestData.class).resourcePath(); + } + + default String resourcePath(final Parameter parameter) { + return parameter.getAnnotation(InjectTestData.class).resourcePath(); + } + + default Optional instanceIdentifier(final Field field) { + final String identifier = field.getAnnotation(InjectTestData.class).id(); + // == used instead of equals to ensure constant was used + if (NO_ID.equals(identifier)) { + return Optional.absent(); + } else { + return Optional.of(identifier); + } + } + + default Optional instanceIdentifier(final Parameter parameter) { + final String identifier = parameter.getAnnotation(InjectTestData.class).id(); + // == used instead of equals to ensure constant was used + if (NO_ID.equals(identifier)) { + return Optional.absent(); + } else { + return Optional.of(identifier); + } + } + + default void injectField(final Field field, final Object testInstance, final DataObject data) { + field.setAccessible(true); + try { + FieldUtils.writeField(field, testInstance, data); + } catch (IllegalAccessException e) { + throw new IllegalStateException("Unable to access field " + field); + } + } + + default YangInstanceIdentifier getRootInstanceIdentifier(final Class type) { + try { + return YangInstanceIdentifier.of(QName.class.cast(type.getField("QNAME").get(null))); + } catch (IllegalAccessException e) { + throw new IllegalStateException("Constant QNAME not accessible for type" + type); + } catch (NoSuchFieldException e) { + throw new IllegalStateException("Class " + type + " does not have QName defined"); + } + } + + default ModuleInfoBackedContext provideSchemaContextFor(final Set modules) { + final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create(); + moduleInfoBackedContext.addModuleInfos(modules); + return moduleInfoBackedContext; + } +} diff --git a/infra/test-utils/test-tools/src/main/java/io/fd/honeycomb/test/tools/annotations/SchemaContextProvider.java b/infra/test-utils/test-tools/src/main/java/io/fd/honeycomb/test/tools/annotations/SchemaContextProvider.java new file mode 100644 index 000000000..5639dd837 --- /dev/null +++ b/infra/test-utils/test-tools/src/main/java/io/fd/honeycomb/test/tools/annotations/SchemaContextProvider.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * 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. + */ + +package io.fd.honeycomb.test.tools.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface SchemaContextProvider { +} -- cgit 1.2.3-korg