2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.honeycomb.vpp.test.read;
19 import static org.junit.Assert.assertNotNull;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.verify;
23 import io.fd.honeycomb.translate.MappingContext;
24 import io.fd.honeycomb.translate.ModificationCache;
25 import io.fd.honeycomb.translate.read.ReadContext;
26 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
27 import io.fd.honeycomb.translate.read.ReadContext;
28 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
29 import io.fd.honeycomb.vpp.test.util.FutureProducer;
30 import io.fd.honeycomb.vpp.test.util.NamingContextHelper;
31 import java.lang.reflect.Method;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.MockitoAnnotations;
37 import org.opendaylight.yangtools.concepts.Builder;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
43 * Generic test for classes implementing {@link ReaderCustomizer} interface.
45 * @param <D> Specific DataObject derived type (Identifiable), that is handled by this customizer
46 * @param <B> Specific Builder for handled type (D)
48 public abstract class ReaderCustomizerTest<D extends DataObject, B extends Builder<D>> implements FutureProducer,
52 protected FutureJVppCore api;
54 protected ReadContext ctx;
56 protected MappingContext mappingContext;
58 protected final Class<? extends Builder<? extends DataObject>> parentBuilderClass;
59 protected final Class<D> dataObjectClass;
60 protected ModificationCache cache;
61 protected ReaderCustomizer<D, B> customizer;
63 public ReaderCustomizerTest(
64 final Class<D> dataObjectClass,
65 final Class<? extends Builder<? extends DataObject>> parentBuilderClass) {
66 this.dataObjectClass = dataObjectClass;
67 this.parentBuilderClass = parentBuilderClass;
71 public final void setUpParent() throws Exception {
72 MockitoAnnotations.initMocks(this);
73 cache = new ModificationCache();
74 Mockito.doReturn(cache).when(ctx).getModificationCache();
75 Mockito.doReturn(mappingContext).when(ctx).getMappingContext();
77 customizer = initCustomizer();
81 * Optional setup for subclasses. Invoked before customizer is initialized.
83 protected void setUp() throws Exception {
86 protected abstract ReaderCustomizer<D, B> initCustomizer();
88 protected ReaderCustomizer<D, B> getCustomizer() {
93 public void testGetBuilder() throws Exception {
94 assertNotNull(customizer.getBuilder(InstanceIdentifier.create(dataObjectClass)));
98 public void testMerge() throws Exception {
99 final Builder<? extends DataObject> parentBuilder = mock(parentBuilderClass);
100 final D value = mock(dataObjectClass);
101 getCustomizer().merge(parentBuilder, value);
103 final Method method = parentBuilderClass.getMethod("set" + dataObjectClass.getSimpleName(), dataObjectClass);
104 method.invoke(verify(parentBuilder), value);