summaryrefslogtreecommitdiffstats
path: root/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/ReflexiveReaderCustomizerTest.java
blob: 7fb67be6e79dea7aa8535bffa830e82957ebef0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * 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.translate.util.read;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verifyZeroInteractions;

import com.google.common.collect.Lists;
import io.fd.honeycomb.translate.read.ReadContext;
import io.fd.honeycomb.translate.read.ReadFailedException;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nonnull;
import org.junit.Test;
import org.mockito.Mock;
import org.opendaylight.yangtools.concepts.Builder;
import org.opendaylight.yangtools.yang.binding.Augmentation;
import org.opendaylight.yangtools.yang.binding.DataContainer;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.Identifiable;
import org.opendaylight.yangtools.yang.binding.Identifier;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;

public class ReflexiveReaderCustomizerTest {

    @Mock
    private ReadContext ctx;
    private InstanceIdentifier<TestingDataObject> id = InstanceIdentifier.create(TestingDataObject.class);

    @Test
    public void testReflexCustomizer() throws Exception {
        final ReflexiveReaderCustomizer<TestingDataObject, TestingBuilder> reflexReaderCustomizer =
                new ReflexiveReaderCustomizer<>(TestingDataObject.class, TestingBuilder.class);

        assertEquals(TestingDataObject.class, reflexReaderCustomizer.getTypeClass());
        assertEquals(TestingBuilder.class, reflexReaderCustomizer.getBuilderClass());

        assertNotNull(reflexReaderCustomizer.getBuilder(id));

        // NOOP
        final TestingBuilder builder = spy(new TestingBuilder());
        reflexReaderCustomizer.readCurrentAttributes(id, builder, ctx);
        verifyZeroInteractions(builder);

        final TestingBuilderParent parentBuilder = new TestingBuilderParent();
        final TestingDataObject readValue = new TestingDataObject();
        reflexReaderCustomizer.merge(parentBuilder, readValue);
        assertEquals(readValue, parentBuilder.getTestingDataObject());
    }

    @Test
    public void testReflexCustomizerAugment() throws Exception {
        final ReflexiveReaderCustomizer<TestingAugmentation, TestingAugmentBuilder> reflexReaderCustomizer =
                new ReflexiveReaderCustomizer<>(TestingAugmentation.class, TestingAugmentBuilder.class);

        final TestingBuilderParent parentBuilder = new TestingBuilderParent();
        final TestingAugmentation readValue = new TestingAugmentation();
        reflexReaderCustomizer.merge(parentBuilder, readValue);
        assertEquals(readValue, parentBuilder.getAugmentation());
    }

    @Test
    public void testReflexCustomizerList() throws Exception {
        final ReflexiveListReaderCustomizer<TestingListObject, TestingListObject.TestingListKey, TestingListBuilder>
                reflexReaderCustomizer =
                new ReflexiveListReaderCustomizer<TestingListObject, TestingListObject.TestingListKey, TestingListBuilder>
                        (TestingListObject.class, TestingListBuilder.class) {

                    @Nonnull
                    @Override
                    public List<TestingListObject.TestingListKey> getAllIds(
                            @Nonnull final InstanceIdentifier<TestingListObject> id,
                            @Nonnull final ReadContext context) throws ReadFailedException {
                        return Lists.newArrayList();
                    }
                };

        final TestingBuilderParent parentBuilder = new TestingBuilderParent();
        final List<TestingListObject> readValue = Lists.newArrayList(new TestingListObject(), new TestingListObject());
        reflexReaderCustomizer.merge(parentBuilder, readValue);
        assertEquals(readValue, parentBuilder.getTestingListObject());

        final TestingListObject single = new TestingListObject();
        reflexReaderCustomizer.merge(parentBuilder, single);
        assertEquals(Collections.singletonList(single), parentBuilder.getTestingListObject());
    }

    static class TestingDataObject implements DataObject {

        @Override
        public Class<? extends DataContainer> getImplementedInterface() {
            return DataObject.class;
        }
    }

    static class TestingAugmentation implements DataObject, Augmentation<DataObject> {

        @Override
        public Class<? extends DataContainer> getImplementedInterface() {
            return DataObject.class;
        }
    }

    static class TestingListObject implements DataObject, Identifiable<TestingListObject.TestingListKey> {

        @Override
        public Class<? extends DataContainer> getImplementedInterface() {
            return DataObject.class;
        }

        @Override
        public TestingListKey getKey() {
            return new TestingListKey();
        }

        static class TestingListKey implements Identifier<TestingListObject> {
        }
    }


    static class TestingBuilder implements Builder<TestingDataObject> {

        @Override
        public TestingDataObject build() {
            return new TestingDataObject();
        }
    }

    static class TestingAugmentBuilder implements Builder<TestingAugmentation> {

        @Override
        public TestingAugmentation build() {
            return new TestingAugmentation();
        }
    }

    static class TestingListBuilder implements Builder<TestingListObject> {

        @Override
        public TestingListObject build() {
            return new TestingListObject();
        }
    }

    static class TestingBuilderParent implements Builder<DataObject> {

        private TestingDataObject object;
        private Augmentation<DataObject> augmentation;
        private List<TestingListObject> listObjects;

        public TestingBuilderParent setTestingDataObject(@Nonnull final TestingDataObject object) {
            this.object = object;
            return this;
        }

        public TestingDataObject getTestingDataObject() {
            return object;
        }

        public TestingBuilderParent setTestingListObject(@Nonnull final List<TestingListObject> listObjects) {
            this.listObjects = listObjects;
            return this;
        }

        public List<TestingListObject> getTestingListObject() {
            return listObjects;
        }

        public TestingBuilderParent addAugmentation(Class<? extends Augmentation<DataObject>> augmentationType,
                                                    Augmentation<DataObject> augmentation) {
            this.augmentation = augmentation;
            return this;
        }

        public Augmentation<DataObject> getAugmentation() {
            return augmentation;
        }

        @Override
        public TestingDataObject build() {
            return new TestingDataObject();
        }
    }
}