HONEYCOMB-130: Rename infra packages(remove vpp/v3po)
[honeycomb.git] / infra / translate-impl / src / test / java / io / fd / honeycomb / translate / impl / write / GenericListWriterTest.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.translate.impl.write;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23
24 import io.fd.honeycomb.translate.write.WriteContext;
25 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
26 import java.util.Collections;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.opendaylight.yangtools.yang.binding.Identifiable;
33 import org.opendaylight.yangtools.yang.binding.Identifier;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35
36 public class GenericListWriterTest {
37
38     private static final InstanceIdentifier<IdentifiableDataObject>
39             DATA_OBJECT_INSTANCE_IDENTIFIER = InstanceIdentifier.create(IdentifiableDataObject.class);
40     @Mock
41     private ListWriterCustomizer<IdentifiableDataObject, DataObjectIdentifier> customizer;
42     @Mock
43     private WriteContext ctx;
44
45     @Before
46     public void setUp() throws Exception {
47         MockitoAnnotations.initMocks(this);
48     }
49
50     @Test
51     public void testUpdate() throws Exception {
52         final GenericListWriter<IdentifiableDataObject, DataObjectIdentifier> writer =
53                 new GenericListWriter<>(DATA_OBJECT_INSTANCE_IDENTIFIER, customizer);
54
55         final IdentifiableDataObject before = mock(IdentifiableDataObject.class);
56         final DataObjectIdentifier beforeKey = mock(DataObjectIdentifier.class);
57         when(before.getKey()).thenReturn(beforeKey);
58         final IdentifiableDataObject after = mock(IdentifiableDataObject.class);
59         final DataObjectIdentifier keyAfter = mock(DataObjectIdentifier.class);
60         when(after.getKey()).thenReturn(keyAfter);
61
62         assertEquals(DATA_OBJECT_INSTANCE_IDENTIFIER, writer.getManagedDataObjectType());
63
64         final InstanceIdentifier<IdentifiableDataObject> keyedIdBefore =
65                 (InstanceIdentifier<IdentifiableDataObject>) InstanceIdentifier.create(Collections
66                         .singleton(new InstanceIdentifier.IdentifiableItem<>(IdentifiableDataObject.class, beforeKey)));
67         final InstanceIdentifier<IdentifiableDataObject> keyedIdAfter =
68                 (InstanceIdentifier<IdentifiableDataObject>) InstanceIdentifier.create(Collections
69                         .singleton(new InstanceIdentifier.IdentifiableItem<>(IdentifiableDataObject.class, keyAfter)));
70
71         writer.update(DATA_OBJECT_INSTANCE_IDENTIFIER, before, after, ctx);
72         verify(customizer).updateCurrentAttributes(keyedIdBefore, before, after, ctx);
73
74         writer.update(DATA_OBJECT_INSTANCE_IDENTIFIER, before, null, ctx);
75         verify(customizer).deleteCurrentAttributes(keyedIdBefore, before, ctx);
76
77         writer.update(DATA_OBJECT_INSTANCE_IDENTIFIER, null, after, ctx);
78         verify(customizer).writeCurrentAttributes(keyedIdAfter, after, ctx);
79     }
80
81     private abstract static class IdentifiableDataObject implements DataObject, Identifiable<DataObjectIdentifier> {}
82     private abstract static class DataObjectIdentifier implements Identifier<IdentifiableDataObject> {}
83 }