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.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 import static org.mockito.Mockito.doThrow;
23 import static org.mockito.Mockito.verify;
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;
36 public class GenericWriterTest {
38 private static final InstanceIdentifier<DataObject>
39 DATA_OBJECT_ID = InstanceIdentifier.create(DataObject.class);
41 private WriterCustomizer<DataObject> customizer;
43 private WriteContext ctx;
45 private DataObject before;
47 private DataObject after;
49 private Validator<DataObject> validator;
51 private GenericWriter<DataObject> writer;
54 public void setUp() throws Exception {
55 MockitoAnnotations.initMocks(this);
56 writer = new GenericWriter<>(DATA_OBJECT_ID, customizer, validator);
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);
65 writer.processModification(DATA_OBJECT_ID, before, null, ctx);
66 verify(customizer).deleteCurrentAttributes(DATA_OBJECT_ID, before, ctx);
68 writer.processModification(DATA_OBJECT_ID, null, after, ctx);
69 verify(customizer).writeCurrentAttributes(DATA_OBJECT_ID, after, ctx);
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);
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);
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);
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()));
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);
108 writer.validate(DATA_OBJECT_ID, before, null, ctx);
109 verify(validator).validateDelete(DATA_OBJECT_ID, before, ctx);
111 writer.validate(DATA_OBJECT_ID, null, after, ctx);
112 verify(validator).validateWrite(DATA_OBJECT_ID, after, ctx);