HONEYCOMB-431: add validation support to Writers
[honeycomb.git] / infra / translate-impl / src / test / java / io / fd / honeycomb / translate / impl / write / GenericWriterTest.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.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 import static org.mockito.Mockito.doThrow;
23 import static org.mockito.Mockito.verify;
24
25 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
26 import io.fd.honeycomb.translate.write.Validator;
27 import io.fd.honeycomb.translate.write.WriteContext;
28 import io.fd.honeycomb.translate.write.WriteFailedException;
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.InstanceIdentifier;
35
36 public class GenericWriterTest {
37
38     private static final InstanceIdentifier<DataObject>
39             DATA_OBJECT_ID = InstanceIdentifier.create(DataObject.class);
40     @Mock
41     private WriterCustomizer<DataObject> customizer;
42     @Mock
43     private WriteContext ctx;
44     @Mock
45     private DataObject before;
46     @Mock
47     private DataObject after;
48     @Mock
49     private Validator<DataObject> validator;
50
51     private GenericWriter<DataObject> writer;
52
53     @Before
54     public void setUp() throws Exception {
55         MockitoAnnotations.initMocks(this);
56         writer = new GenericWriter<>(DATA_OBJECT_ID, customizer, validator);
57     }
58
59     @Test
60     public void testUpdate() throws Exception {
61         assertEquals(DATA_OBJECT_ID, writer.getManagedDataObjectType());
62         writer.processModification(DATA_OBJECT_ID, before, after, ctx);
63         verify(customizer).updateCurrentAttributes(DATA_OBJECT_ID, before, after, ctx);
64
65         writer.processModification(DATA_OBJECT_ID, before, null, ctx);
66         verify(customizer).deleteCurrentAttributes(DATA_OBJECT_ID, before, ctx);
67
68         writer.processModification(DATA_OBJECT_ID, null, after, ctx);
69         verify(customizer).writeCurrentAttributes(DATA_OBJECT_ID, after, ctx);
70     }
71
72     @Test(expected = WriteFailedException.CreateFailedException.class)
73     public void testWriteFail() throws Exception {
74         doThrow(new IllegalStateException("test")).when(customizer).writeCurrentAttributes(DATA_OBJECT_ID, after, ctx);
75         writer = new GenericWriter<>(DATA_OBJECT_ID, customizer);
76         writer.writeCurrentAttributes(DATA_OBJECT_ID, after, ctx);
77     }
78
79     @Test(expected = WriteFailedException.UpdateFailedException.class)
80     public void testUpdateFail() throws Exception {
81         doThrow(new IllegalStateException("test")).when(customizer)
82                 .updateCurrentAttributes(DATA_OBJECT_ID, before, after, ctx);
83         writer = new GenericWriter<>(DATA_OBJECT_ID, customizer);
84         writer.updateCurrentAttributes(DATA_OBJECT_ID, before, after, ctx);
85     }
86
87     @Test(expected = WriteFailedException.DeleteFailedException.class)
88     public void testDeleteFail() throws Exception {
89         doThrow(new IllegalStateException("test")).when(customizer)
90                 .deleteCurrentAttributes(DATA_OBJECT_ID, before, ctx);
91         writer = new GenericWriter<>(DATA_OBJECT_ID, customizer);
92         writer.deleteCurrentAttributes(DATA_OBJECT_ID, before, ctx);
93     }
94
95     @Test
96     public void testUpdateSupported() {
97         assertFalse(GenericWriter.isUpdateSupported(new NoopWriters.NonDirectUpdateWriterCustomizer()));
98         assertTrue(GenericWriter.isUpdateSupported(new NoopWriters.DirectUpdateWriterCustomizer()));
99         assertTrue(GenericWriter.isUpdateSupported(new NoopWriters.ParentImplDirectUpdateWriterCustomizer()));
100     }
101
102     @Test
103     public void testValidate() throws Exception {
104         assertEquals(DATA_OBJECT_ID, writer.getManagedDataObjectType());
105         writer.validate(DATA_OBJECT_ID, before, after, ctx);
106         verify(validator).validateUpdate(DATA_OBJECT_ID, before, after, ctx);
107
108         writer.validate(DATA_OBJECT_ID, before, null, ctx);
109         verify(validator).validateDelete(DATA_OBJECT_ID, before, ctx);
110
111         writer.validate(DATA_OBJECT_ID, null, after, ctx);
112         verify(validator).validateWrite(DATA_OBJECT_ID, after, ctx);
113     }
114 }