d4cb385c6a7d037da410c620bdc645045ea2be9e
[honeycomb.git] / vpp-common / vpp-translate-test / src / main / java / io / fd / honeycomb / vpp / test / read / ReaderCustomizerTest.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.honeycomb.vpp.test.read;
18
19 import static org.junit.Assert.assertNotNull;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.verify;
22
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;
41
42 /**
43  * Generic test for classes implementing {@link ReaderCustomizer} interface.
44  *
45  * @param <D> Specific DataObject derived type (Identifiable), that is handled by this customizer
46  * @param <B> Specific Builder for handled type (D)
47  */
48 public abstract class ReaderCustomizerTest<D extends DataObject, B extends Builder<D>> implements FutureProducer,
49     NamingContextHelper {
50
51     @Mock
52     protected FutureJVppCore api;
53     @Mock
54     protected ReadContext ctx;
55     @Mock
56     protected MappingContext mappingContext;
57
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;
62
63     public ReaderCustomizerTest(
64         final Class<D> dataObjectClass,
65         final Class<? extends Builder<? extends DataObject>> parentBuilderClass) {
66         this.dataObjectClass = dataObjectClass;
67         this.parentBuilderClass = parentBuilderClass;
68     }
69
70     @Before
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();
76         setUp();
77         customizer = initCustomizer();
78     }
79
80     /**
81      * Optional setup for subclasses. Invoked before customizer is initialized.
82      */
83     protected void setUp() throws Exception {
84     }
85
86     protected abstract ReaderCustomizer<D, B> initCustomizer();
87
88     protected ReaderCustomizer<D, B> getCustomizer() {
89         return customizer;
90     }
91
92     @Test
93     public void testGetBuilder() throws Exception {
94         assertNotNull(customizer.getBuilder(InstanceIdentifier.create(dataObjectClass)));
95     }
96
97     @Test
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);
102
103         final Method method = parentBuilderClass.getMethod("set" + dataObjectClass.getSimpleName(), dataObjectClass);
104         method.invoke(verify(parentBuilder), value);
105     }
106 }