summaryrefslogtreecommitdiffstats
path: root/infra/data-impl/src/test/java/io/fd/honeycomb/data/impl/ModifiableDataTreeDelegatorRevertTest.java
blob: 2787cdf1f729dea0d56fb876c88b73cb2ac7fa23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.honeycomb.data.impl;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import io.fd.honeycomb.data.DataModification;
import io.fd.honeycomb.translate.TranslationException;
import io.fd.honeycomb.translate.write.DataObjectUpdate;
import io.fd.honeycomb.translate.write.WriteContext;
import io.fd.honeycomb.translate.write.registry.UpdateFailedException;
import io.fd.honeycomb.translate.write.registry.WriterRegistry;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.MapNode;

public class ModifiableDataTreeDelegatorRevertTest extends ModifiableDataTreeDelegatorBaseTest {

    /**
     * Test scenario when commit fails, but there's nothing to revert because very first crud operation failed
     */
    @Test
    public void testCommitFailedNoRevert() throws Exception {
        final MapNode nestedList = getNestedList("listEntry", "listValue");

        // Fail on update:
        final TranslationException failedOnUpdateException = new TranslationException("update failed");
        doThrow(new UpdateFailedException(failedOnUpdateException, Collections.emptyList(), update))// fail on update
                .when(writer)
                .processModifications(any(WriterRegistry.DataObjectUpdates.class), any(WriteContext.class));

        try {
            // Run the test
            final DataModification dataModification = configDataTree.newModification();
            dataModification.write(NESTED_LIST_ID, nestedList);
            dataModification.validate();
            dataModification.commit();
            fail("UpdateFailedException was expected");
        } catch (UpdateFailedException e) {
            // writer was called only one for update, and it was only one operation so no revert needed
            // exception was just rethrown
            verify(writer).processModifications(any(WriterRegistry.DataObjectUpdates.class), any(WriteContext.class));
            assertEquals(e.getFailed().getId(), DEFAULT_ID);
            assertTrue(e.getProcessed().isEmpty());
        }
    }

    /**
     * Test whether
     *  - Correct operations were invoked(when creating and reverting)
     *  - Create and revert both failed
     *  - Correct exception has been thrown
     * Steps:
     * - Prepares state with nested list written
     * - Attempts to rewrite that list with new list with value with different key
     * - Simulates fail(both on modification and revert)
     * - Checks modifications
     * Asserts
     * - index 0 - Represents create of original data
     * - index 1 - Represents override with new list(fails)(include delete of data created by index 0 and create of new)
     * - index 2 - Represents revert of removal of original data
     */
    @Test
    public void testCommitWithRevertFailed() throws Exception {
        // configure initial state
        final MapNode originalList = getNestedList("listEntryOriginal", "listValueOriginal");

        final DataModification preModification = configDataTree.newModification();
        preModification.write(NESTED_LIST_ID, originalList);
        preModification.validate();
        preModification.commit();

        // then test
        final MapNode nestedList = getNestedList("listEntry", "listValueNew");

        // Fail on update:
        final TranslationException failedOnUpdateException = new TranslationException("update failed");
        final DataObjectUpdate.DataObjectDelete mockRevertData = mock(DataObjectUpdate.DataObjectDelete.class);
        final DataObjectUpdate.DataObjectDelete mockRevertDataReverted = mock(DataObjectUpdate.DataObjectDelete.class);
        when(mockRevertData.getId()).thenReturn((InstanceIdentifier) InstanceIdentifier.create(DataObject.class));
        when(mockRevertDataReverted.getId())
                .thenReturn((InstanceIdentifier) InstanceIdentifier.create(DataObject.class));
        when(mockRevertData.getDataBefore()).thenReturn(DEFAULT_DATA_OBJECT);// to simulate that delete of original data
        //should be reverted
        when(mockRevertDataReverted.getDataAfter())
                .thenReturn(DEFAULT_DATA_OBJECT);// to simulate that delete of original data
        //should be reverted
        when(mockRevertData.reverse()).thenReturn(mockRevertDataReverted);

        final UpdateFailedException cause =
                new UpdateFailedException(failedOnUpdateException,
                        Collections.singletonList(mockRevertData),//fail on new one
                        update);
        doThrow(cause)
                .when(writer)
                .processModifications(any(WriterRegistry.DataObjectUpdates.class), any(WriteContext.class));

        try {
            // Run the test
            final DataModification dataModification = configDataTree.newModification();
            dataModification.write(NESTED_LIST_ID, nestedList);
            dataModification.validate();
            dataModification.commit();
            fail("WriterRegistry.Reverter.RevertFailedException was expected");
        } catch (Reverter.RevertFailedException e) {
            assertRewriteModificationWithRevert(writer, updatesCaptor, DEFAULT_DATA_OBJECT);
            assertEquals(cause, e.getCause());
        }
    }

    /**
     * Test whether
     *  - Correct operations were invoked(when creating and reverting)
     *  - Create failed and revert passed
     *  - Correct exception has been thrown
     * Steps:
     * - Prepares state with nested list written
     * - Attempts to rewrite that list with new list with value with different key
     * - Simulates fail on create
     * - Passes on revert
     * - Checks modifications
     * Asserts
     * - index 0 - Represents create of original data
     * - index 1 - Represents override with new list(fails)(include delete of data created by index 0 and create of new)
     * - index 2 - Represents revert of removal of original data
     */
    @Test
    public void testCommitWithRevertSuccessfull() throws Exception {
        // configure initial state
        final MapNode originalList = getNestedList("listEntryOriginal", "listValueOriginal");

        final DataModification preModification = configDataTree.newModification();
        preModification.write(NESTED_LIST_ID, originalList);
        preModification.validate();
        preModification.commit();

        // then test
        final MapNode nestedList = getNestedList("listEntry", "listValueNew");

        // Fail on update:
        final TranslationException failedOnUpdateException = new TranslationException("update failed");
        final DataObjectUpdate.DataObjectDelete mockRevertData = mock(DataObjectUpdate.DataObjectDelete.class);
        final DataObjectUpdate.DataObjectDelete mockRevertDataReverted = mock(DataObjectUpdate.DataObjectDelete.class);
        when(mockRevertData.getId()).thenReturn((InstanceIdentifier) InstanceIdentifier.create(DataObject.class));
        when(mockRevertDataReverted.getId())
                .thenReturn((InstanceIdentifier) InstanceIdentifier.create(DataObject.class));
        when(mockRevertData.getDataBefore()).thenReturn(DEFAULT_DATA_OBJECT);// to simulate that delete of original data
        //should be reverted
        when(mockRevertDataReverted.getDataAfter())
                .thenReturn(DEFAULT_DATA_OBJECT);// to simulate that delete of original data
        //should be reverted
        when(mockRevertData.reverse()).thenReturn(mockRevertDataReverted);

        final UpdateFailedException cause =
                new UpdateFailedException(failedOnUpdateException,
                        Collections.singletonList(mockRevertData),//fail on new one
                        update);
        doThrow(cause) // fails on create
                .doNothing()//to pass on revert
                .when(writer)
                .processModifications(any(WriterRegistry.DataObjectUpdates.class), any(WriteContext.class));

        try {
            // Run the test
            final DataModification dataModification = configDataTree.newModification();
            dataModification.write(NESTED_LIST_ID, nestedList);
            dataModification.validate();
            dataModification.commit();
            fail("WriterRegistry.Reverter.RevertFailedException was expected");
        } catch (Reverter.RevertSuccessException e) {
            assertRewriteModificationWithRevert(writer, updatesCaptor, DEFAULT_DATA_OBJECT);
            assertNull(e.getCause());
        }
    }

    private static void assertRewriteModificationWithRevert(final WriterRegistry writer,
                                                            final ArgumentCaptor<WriterRegistry.DataObjectUpdates> updatesCaptor,
                                                            final DataObject DEFAULT_DATA_OBJECT)
            throws TranslationException {
        verify(writer, times(3)).processModifications(updatesCaptor.capture(), any(WriteContext.class));
        final List<WriterRegistry.DataObjectUpdates> allUpdates = updatesCaptor.getAllValues();
        assertEquals(3, allUpdates.size());

        // represent create of original data
        final WriterRegistry.DataObjectUpdates originalCreate = allUpdates.get(0);
        assertContainsOnlySingleUpdate(originalCreate);

        final DataObjectUpdate createOriginalData = originalCreate.getUpdates().values().iterator().next();
        assertCreateWithData(createOriginalData, DEFAULT_DATA_OBJECT);

        // delete of original data was successful
        // create of new data - failed
        final WriterRegistry.DataObjectUpdates originalDelete = allUpdates.get(1);
        assertConstainsSingleUpdateAndDelete(originalDelete);

        final DataObjectUpdate.DataObjectDelete deleteOriginalData =
                originalDelete.getDeletes().values().iterator().next();
        assertDeleteWithData(deleteOriginalData, DEFAULT_DATA_OBJECT);

        final DataObjectUpdate newCreate = originalDelete.getUpdates().values().iterator().next();
        assertCreateWithData(newCreate, DEFAULT_DATA_OBJECT);

        final WriterRegistry.DataObjectUpdates revert = allUpdates.get(2);
        assertContainsOnlySingleUpdate(revert);
    }

    private static void assertDeleteWithData(final DataObjectUpdate.DataObjectDelete deleteOriginalData,
                                             final DataObject DEFAULT_DATA_OBJECT) {
        assertNull(deleteOriginalData.getDataAfter());
        assertEquals(DEFAULT_DATA_OBJECT, deleteOriginalData.getDataBefore());
    }

    private static void assertCreateWithData(final DataObjectUpdate newCreate, final DataObject DEFAULT_DATA_OBJECT) {
        assertNull(newCreate.getDataBefore());
        assertEquals(DEFAULT_DATA_OBJECT, newCreate.getDataAfter());
    }

    private static void assertContainsOnlySingleUpdate(final WriterRegistry.DataObjectUpdates originalCreate) {
        assertThat(originalCreate.getDeletes().size(), is(0));
        assertThat(originalCreate.getUpdates().size(), is(1));
    }

    private static void assertConstainsSingleUpdateAndDelete(final WriterRegistry.DataObjectUpdates originalDelete) {
        assertThat(originalDelete.getDeletes().size(), is(1));
        assertThat(originalDelete.getUpdates().size(), is(1));
    }

}