summaryrefslogtreecommitdiffstats
path: root/zusammen-lib/src/test/java/org/onap/sdc/common/zusammen/services/impl/ZusammenAdaptorImplTest.java
blob: 4fc4b97b1e01e9fcf69790a25d4fec9726c2dec1 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * Copyright © 2019 European Support Limited
 *
 * 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 org.onap.sdc.common.zusammen.services.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;

import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
import com.amdocs.zusammen.datatypes.Id;
import com.amdocs.zusammen.datatypes.SessionContext;
import com.amdocs.zusammen.datatypes.item.Action;
import com.amdocs.zusammen.datatypes.item.ElementContext;
import com.amdocs.zusammen.datatypes.item.Info;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.onap.sdc.common.zusammen.persistence.ZusammenConnector;

public class ZusammenAdaptorImplTest {

    private static final SessionContext CONTEXT = new SessionContext();
    private static final ElementContext ELEMENT_CONTEXT = new ElementContext();
    private static final Id ELEMENT_ID = new Id("elementId 0");
    private static final List<ElementInfo> ELEMENTS =
        Arrays.asList(createElementInfo("elementId1", "element1"), createElementInfo("elementId2", "element2"),
            createElementInfo("elementId3", "element3"));

    private final ZusammenConnector connector = Mockito.mock(ZusammenConnector.class);
    @InjectMocks
    private ZusammenAdaptorImpl zusammenAdaptor;
    @Captor
    private ArgumentCaptor<Element> savedElementArg;

    @BeforeEach
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void getEmptyWhenElementNameNotExist() {
        doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID);

        Optional<Element> element =
            zusammenAdaptor.getElementByName(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID, "nonExistingName");

        assertFalse(element.isPresent());
    }

    @Test
    public void getEmptyInfoWhenElementNameNotExist() {
        doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID);

        Optional<ElementInfo> elementInfo =
            zusammenAdaptor.getElementInfoByName(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID, "nonExistingName");

        assertFalse(elementInfo.isPresent());
    }

    @Test
    public void getElementWhenItsNameExist() {
        doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID);
        ZusammenElement returnedElement = new ZusammenElement();
        doReturn(returnedElement).when(connector).getElement(CONTEXT, ELEMENT_CONTEXT, ELEMENTS.get(1).getId());

        Optional<Element> element = zusammenAdaptor.getElementByName(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID, "element2");

        assertTrue(element.isPresent());
        assertEquals(returnedElement, element.get());
    }

    @Test
    public void getElementInfoWhenItsNameExist() {
        doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID);

        Optional<ElementInfo> elementInfo =
            zusammenAdaptor.getElementInfoByName(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID, "element2");

        assertTrue(elementInfo.isPresent());
        assertEquals(ELEMENTS.get(1), elementInfo.get());

    }

    @Test
    public void listElementsWhenTheirParentIdExist() {
        doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID);

        List<ZusammenElement> returnedElements =
            Arrays.asList(new ZusammenElement(), new ZusammenElement(), new ZusammenElement());
        doReturn(returnedElements.get(0)).when(connector).getElement(CONTEXT, ELEMENT_CONTEXT, ELEMENTS.get(0).getId());
        doReturn(returnedElements.get(1)).when(connector).getElement(CONTEXT, ELEMENT_CONTEXT, ELEMENTS.get(1).getId());
        doReturn(returnedElements.get(2)).when(connector).getElement(CONTEXT, ELEMENT_CONTEXT, ELEMENTS.get(2).getId());

        Collection<Element> elements = zusammenAdaptor.listElementData(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID);

        assertEquals(returnedElements, elements);
    }

    @Test
    public void getEmptyListWhenParentElementNameNotExist() {
        doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID);

        Collection<ElementInfo> elements =
            zusammenAdaptor.listElementsByName(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID, "nonExistingName");

        assertTrue(elements.isEmpty());
    }

    @Test
    public void listElementsInfoWhenTheirParentElementNameExist() {
        doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID);

        List<ElementInfo> returnedElements = Arrays.asList(new ElementInfo(), new ElementInfo());
        doReturn(returnedElements).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENTS.get(1).getId());

        Collection<ElementInfo> elements =
            zusammenAdaptor.listElementsByName(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID, "element2");

        assertEquals(returnedElements, elements);
    }

    @Test
    public void failWhenSavingElementWithoutIdNameOrAction() {
        assertThrows(
            IllegalArgumentException.class,
            () -> zusammenAdaptor.saveElement(CONTEXT, ELEMENT_CONTEXT, new ZusammenElement(), "Illegal element save")
        );
    }

    @Test
    public void saveElementAsRootWhenParentIdNotSupplied() {
        String message = "Create new element tree";
        ZusammenElement element = new ZusammenElement();
        element.setAction(Action.CREATE);

        ZusammenElement subElement = new ZusammenElement();
        subElement.setAction(Action.CREATE);
        element.addSubElement(subElement);

        testSaveElement(message, element);

        verify(connector).saveElement(CONTEXT, ELEMENT_CONTEXT, element, message);
    }

    @Test
    public void saveElementAsSubWhenParentIdSupplied() {
        String message = "Create sub element";
        ZusammenElement element = new ZusammenElement();
        element.setAction(Action.IGNORE);
        element.setElementId(ELEMENT_ID);

        ZusammenElement subElement = new ZusammenElement();
        subElement.setAction(Action.CREATE);
        element.addSubElement(subElement);

        testSaveElement(message, element);

        verify(connector).saveElement(CONTEXT, ELEMENT_CONTEXT, element, message);
    }

    @Test
    public void saveElementWhenItsIdSupplied() {
        String message = "Update element";
        ZusammenElement element = new ZusammenElement();
        element.setAction(Action.UPDATE);
        element.setElementId(new Id("id"));

        testSaveElement(message, element);

        verify(connector).saveElement(CONTEXT, ELEMENT_CONTEXT, element, message);
    }

    @Test
    public void saveRootElementWhenItsNameSupplied() {
        String message = "Update element";
        ZusammenElement element = new ZusammenElement();
        element.setAction(Action.UPDATE);
        element.setInfo(ELEMENTS.get(1).getInfo());

        doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, null);

        testSaveElement(message, element);

        verify(connector).saveElement(eq(CONTEXT), eq(ELEMENT_CONTEXT), savedElementArg.capture(), eq(message));

        Element savedElement = savedElementArg.getValue();
        assertEquals(element, savedElement);
        assertNotNull(savedElement.getElementId());
    }

    @Test
    public void saveElementWhenItsNameAndParentIdSupplied() {
        String message = "Update existing element";
        ZusammenElement element = new ZusammenElement();
        element.setAction(Action.IGNORE);
        element.setElementId(ELEMENT_ID);

        ZusammenElement existingSub = new ZusammenElement();
        existingSub.setAction(Action.UPDATE);
        existingSub.setInfo(ELEMENTS.get(2).getInfo());
        element.addSubElement(existingSub);

        doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID);

        testSaveElement(message, element);

        verify(connector).saveElement(eq(CONTEXT), eq(ELEMENT_CONTEXT), savedElementArg.capture(), eq(message));

        Element savedElement = savedElementArg.getValue();
        assertEquals(element, savedElement);

        Element updated = savedElement.getSubElements().iterator().next();
        assertNotNull(updated.getElementId());
        assertEquals(Action.UPDATE, updated.getAction());
    }

    @Test
    public void saveElementWithCreateActionInsteadOfUpdateWhenItDoesNotExist() {
        String message = "Create new element";
        ZusammenElement element = new ZusammenElement();
        element.setAction(Action.IGNORE);
        element.setElementId(ELEMENT_ID);

        ZusammenElement nonExistingSub = new ZusammenElement();
        nonExistingSub.setAction(Action.UPDATE);
        Info info = new Info();
        info.setName("nonExistingName");
        nonExistingSub.setInfo(info);

        element.addSubElement(nonExistingSub);

        doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID);

        testSaveElement(message, element);

        verify(connector).saveElement(eq(CONTEXT), eq(ELEMENT_CONTEXT), savedElementArg.capture(), eq(message));

        Element savedElement = savedElementArg.getValue();
        assertEquals(element, savedElement);

        Element created = savedElement.getSubElements().iterator().next();
        assertNull(created.getElementId());
        assertEquals(Action.CREATE, created.getAction());
    }

    @Test
    public void saveElementWithIgnoreActionWhenItExistAndActionNotSupplied() {
        String message = "save existing element";
        ZusammenElement element = new ZusammenElement();
        element.setAction(Action.IGNORE);
        element.setElementId(ELEMENT_ID);

        ZusammenElement existingSub = new ZusammenElement();
        existingSub.setInfo(ELEMENTS.get(2).getInfo());
        element.addSubElement(existingSub);

        doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID);

        testSaveElement(message, element);

        verify(connector).saveElement(eq(CONTEXT), eq(ELEMENT_CONTEXT), savedElementArg.capture(), eq(message));

        Element savedElement = savedElementArg.getValue();
        assertEquals(element, savedElement);

        Element ignored = savedElement.getSubElements().iterator().next();
        assertNotNull(ignored.getElementId());
        assertEquals(Action.IGNORE, ignored.getAction());
    }

    private void testSaveElement(String message, ZusammenElement element) {
        ZusammenElement returnedElement = new ZusammenElement();
        doReturn(returnedElement).when(connector).saveElement(CONTEXT, ELEMENT_CONTEXT, element, message);

        Element saved = zusammenAdaptor.saveElement(CONTEXT, ELEMENT_CONTEXT, element, message);

        assertEquals(returnedElement, saved);
    }

    private static ElementInfo createElementInfo(String id, String name) {
        ElementInfo elementInfo = new ElementInfo();
        elementInfo.setId(new Id(id));
        Info info = new Info();
        info.setName(name);
        elementInfo.setInfo(info);
        return elementInfo;
    }
}