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.translate.impl.write;
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;
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;
36 public class GenericListWriterTest {
38 private static final InstanceIdentifier<IdentifiableDataObject>
39 DATA_OBJECT_INSTANCE_IDENTIFIER = InstanceIdentifier.create(IdentifiableDataObject.class);
41 private ListWriterCustomizer<IdentifiableDataObject, DataObjectIdentifier> customizer;
43 private WriteContext ctx;
46 public void setUp() throws Exception {
47 MockitoAnnotations.initMocks(this);
51 public void testUpdate() throws Exception {
52 final GenericListWriter<IdentifiableDataObject, DataObjectIdentifier> writer =
53 new GenericListWriter<>(DATA_OBJECT_INSTANCE_IDENTIFIER, customizer);
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);
62 assertEquals(DATA_OBJECT_INSTANCE_IDENTIFIER, writer.getManagedDataObjectType());
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)));
71 writer.update(DATA_OBJECT_INSTANCE_IDENTIFIER, before, after, ctx);
72 verify(customizer).updateCurrentAttributes(keyedIdBefore, before, after, ctx);
74 writer.update(DATA_OBJECT_INSTANCE_IDENTIFIER, before, null, ctx);
75 verify(customizer).deleteCurrentAttributes(keyedIdBefore, before, ctx);
77 writer.update(DATA_OBJECT_INSTANCE_IDENTIFIER, null, after, ctx);
78 verify(customizer).writeCurrentAttributes(keyedIdAfter, after, ctx);
81 private abstract static class IdentifiableDataObject implements DataObject, Identifiable<DataObjectIdentifier> {}
82 private abstract static class DataObjectIdentifier implements Identifier<IdentifiableDataObject> {}