HONEYCOMB-431: add validation support to Writers
[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.doThrow;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23
24 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
25 import io.fd.honeycomb.translate.write.Validator;
26 import io.fd.honeycomb.translate.write.WriteContext;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import java.util.Collections;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.opendaylight.yangtools.yang.binding.Identifiable;
35 import org.opendaylight.yangtools.yang.binding.Identifier;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37
38 public class GenericListWriterTest {
39
40     private static final InstanceIdentifier<IdentifiableDataObject>
41             DATA_OBJECT_ID = InstanceIdentifier.create(IdentifiableDataObject.class);
42     @Mock
43     private ListWriterCustomizer<IdentifiableDataObject, DataObjectIdentifier> customizer;
44     @Mock
45     private WriteContext ctx;
46     @Mock
47     private IdentifiableDataObject before;
48     @Mock
49     private DataObjectIdentifier beforeKey;
50     @Mock
51     private IdentifiableDataObject after;
52     @Mock
53     private DataObjectIdentifier keyAfter;
54     @Mock
55     private Validator<IdentifiableDataObject> validator;
56
57     private GenericListWriter<IdentifiableDataObject, DataObjectIdentifier> writer;
58
59     @Before
60     public void setUp() throws Exception {
61         MockitoAnnotations.initMocks(this);
62         writer = new GenericListWriter<>(DATA_OBJECT_ID, customizer, validator);
63         when(before.getKey()).thenReturn(beforeKey);
64         when(after.getKey()).thenReturn(keyAfter);
65     }
66
67     @Test
68     public void testUpdate() throws Exception {
69         assertEquals(DATA_OBJECT_ID, writer.getManagedDataObjectType());
70
71         final InstanceIdentifier<IdentifiableDataObject> keyedIdBefore =
72                 (InstanceIdentifier<IdentifiableDataObject>) InstanceIdentifier.create(Collections
73                         .singleton(new InstanceIdentifier.IdentifiableItem<>(IdentifiableDataObject.class, beforeKey)));
74         final InstanceIdentifier<IdentifiableDataObject> keyedIdAfter =
75                 (InstanceIdentifier<IdentifiableDataObject>) InstanceIdentifier.create(Collections
76                         .singleton(new InstanceIdentifier.IdentifiableItem<>(IdentifiableDataObject.class, keyAfter)));
77
78         writer.processModification(DATA_OBJECT_ID, before, after, ctx);
79         verify(customizer).updateCurrentAttributes(keyedIdBefore, before, after, ctx);
80
81         writer.processModification(DATA_OBJECT_ID, before, null, ctx);
82         verify(customizer).deleteCurrentAttributes(keyedIdBefore, before, ctx);
83
84         writer.processModification(DATA_OBJECT_ID, null, after, ctx);
85         verify(customizer).writeCurrentAttributes(keyedIdAfter, after, ctx);
86     }
87
88     private abstract static class IdentifiableDataObject implements DataObject, Identifiable<DataObjectIdentifier> {}
89     private abstract static class DataObjectIdentifier implements Identifier<IdentifiableDataObject> {}
90
91     @Test(expected = WriteFailedException.CreateFailedException.class)
92     public void testWriteFail() throws Exception {
93         doThrow(new IllegalStateException("test")).when(customizer).writeCurrentAttributes(DATA_OBJECT_ID, after, ctx);
94         writer = new GenericListWriter<>(DATA_OBJECT_ID, customizer);
95         writer.writeCurrentAttributes(DATA_OBJECT_ID, after, ctx);
96     }
97
98     @Test(expected = WriteFailedException.UpdateFailedException.class)
99     public void testUpdateFail() throws Exception {
100         doThrow(new IllegalStateException("test")).when(customizer)
101                 .updateCurrentAttributes(DATA_OBJECT_ID, before, after, ctx);
102         writer = new GenericListWriter<>(DATA_OBJECT_ID, customizer);
103         writer.updateCurrentAttributes(DATA_OBJECT_ID, before, after, ctx);
104     }
105
106     @Test(expected = WriteFailedException.DeleteFailedException.class)
107     public void testDeleteFail() throws Exception {
108         doThrow(new IllegalStateException("test")).when(customizer)
109                 .deleteCurrentAttributes(DATA_OBJECT_ID, before, ctx);
110         writer = new GenericListWriter<>(DATA_OBJECT_ID, customizer);
111         writer.deleteCurrentAttributes(DATA_OBJECT_ID, before, ctx);
112     }
113
114     @Test
115     public void testValidate() throws Exception {
116         assertEquals(DATA_OBJECT_ID, writer.getManagedDataObjectType());
117
118         final InstanceIdentifier<IdentifiableDataObject> keyedIdBefore =
119             (InstanceIdentifier<IdentifiableDataObject>) InstanceIdentifier.create(Collections
120                 .singleton(new InstanceIdentifier.IdentifiableItem<>(IdentifiableDataObject.class, beforeKey)));
121         final InstanceIdentifier<IdentifiableDataObject> keyedIdAfter =
122             (InstanceIdentifier<IdentifiableDataObject>) InstanceIdentifier.create(Collections
123                 .singleton(new InstanceIdentifier.IdentifiableItem<>(IdentifiableDataObject.class, keyAfter)));
124
125         writer.validate(DATA_OBJECT_ID, before, after, ctx);
126         verify(validator).validateUpdate(keyedIdBefore, before, after, ctx);
127
128         writer.validate(DATA_OBJECT_ID, before, null, ctx);
129         verify(validator).validateDelete(keyedIdBefore, before, ctx);
130
131         writer.validate(DATA_OBJECT_ID, null, after, ctx);
132         verify(validator).validateWrite(keyedIdAfter, after, ctx);
133     }
134 }