Improve testing stability
[sdc.git] / openecomp-be / lib / openecomp-healing-lib / openecomp-sdc-healing-impl / src / test / java / org / openecomp / sdc / healing / healers / NetworkPackageHealerTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
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  * Modifications copyright (c) 2019 Nokia
17  * ================================================================================
18  */
19
20 package org.openecomp.sdc.healing.healers;
21
22 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
23 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
24 import com.amdocs.zusammen.datatypes.Id;
25 import com.amdocs.zusammen.datatypes.item.Info;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.Optional;
30 import org.apache.commons.io.IOUtils;
31 import org.junit.After;
32 import org.junit.Assert;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36
37 import static java.lang.Boolean.FALSE;
38 import static java.lang.Boolean.TRUE;
39 import static org.mockito.Mockito.any;
40 import static org.openecomp.sdc.datatypes.model.ElementType.OrchestrationTemplateCandidateValidationData;
41 import static org.openecomp.sdc.datatypes.model.ElementType.OrchestrationTemplateValidationData;
42
43 import org.mockito.Mockito;
44 import org.mockito.MockitoAnnotations;
45 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
46 import org.openecomp.sdc.common.session.SessionContextProviderFactory;
47 import org.openecomp.sdc.datatypes.model.ElementType;
48 import org.openecomp.sdc.healing.healers.NetworkPackageHealer;
49 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
50 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
51 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
52 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
53 import org.openecomp.sdc.vendorsoftwareproduct.services.utils.CandidateEntityBuilder;
54 import org.openecomp.sdc.versioning.dao.types.Version;
55 import java.util.ArrayList;
56 import java.util.Collection;
57
58 public class NetworkPackageHealerTest {
59
60     private static final String ITEM_ID = "ITEM_ID";
61     private static final String ANY = "ANY";
62     private static final String UTF_8 = "UTF-8";
63     private static final String FILE_SUFFIX = "fileSuffix";
64     private static final String FILE_NAME = "fileName";
65     private static final String OTHER_THAN_ANY = "OTHER_THAN_ANY";
66     private static final String NETWORK_PACKAGE = "NetworkPackage";
67
68     @Mock
69     private VendorSoftwareProductInfoDao vspInfoDaoMock;
70     @Mock
71     private ZusammenAdaptor zusammenAdaptorMock;
72     @Mock
73     private Element element;
74     @Mock
75     private Element subElement;
76     @Mock
77     private ElementInfo elementInfo;
78     @Mock
79     private Element subElement2;
80     @Mock
81     private CandidateEntityBuilder candidateEntityBuilder;
82     @Mock
83     private OrchestrationTemplateCandidateData orchestrationData;
84
85     private NetworkPackageHealer networkPackageHealer;
86     private static final String tenant = "dox";
87
88     @Before
89     public void init() {
90         SessionContextProviderFactory.getInstance().createInterface().create("test", tenant);
91         MockitoAnnotations.openMocks(this);
92         networkPackageHealer = new NetworkPackageHealer(vspInfoDaoMock, zusammenAdaptorMock, candidateEntityBuilder);
93     }
94
95     @After
96     public void tearDown() {
97         SessionContextProviderFactory.getInstance().createInterface().close();
98     }
99
100     @Test
101     public void testIsHealingNeeded_Positive() {
102         VspDetails vspDetails = new VspDetails(ITEM_ID, new Version());
103         vspDetails.setOnboardingMethod(NETWORK_PACKAGE);
104         Mockito.when(vspInfoDaoMock.get(any())).thenReturn(vspDetails);
105         Collection<ElementInfo> elementInfos = new ArrayList<>();
106         ElementInfo elementInfo = new ElementInfo();
107         Info info = new Info();
108         info.setName(ElementType.OrchestrationTemplateCandidate.name());
109         elementInfo.setInfo(info);
110         elementInfos.add(elementInfo);
111         Mockito.when(zusammenAdaptorMock.listElementsByName(any(), any(), any(), any())).thenReturn(elementInfos);
112         Assert.assertEquals(TRUE, networkPackageHealer.isHealingNeeded(ITEM_ID, new Version()));
113     }
114
115     @Test
116     public void testIsHealingNeeded_Negative() {
117         VspDetails vspDetails = new VspDetails(ITEM_ID, new Version());
118         vspDetails.setOnboardingMethod(NETWORK_PACKAGE);
119         Mockito.when(vspInfoDaoMock.get(any())).thenReturn(vspDetails);
120         Collection<ElementInfo> elementInfos = new ArrayList<>();
121
122         ElementInfo elementInfo = new ElementInfo();
123         Info info = new Info();
124         info.setName(ElementType.OrchestrationTemplateCandidate.name());
125         elementInfo.setInfo(info);
126         elementInfos.add(elementInfo);
127
128         ElementInfo elementInfo1 = new ElementInfo();
129         Info info1 = new Info();
130         info1.setName(OrchestrationTemplateCandidateValidationData.name());
131         elementInfo1.setInfo(info1);
132         elementInfos.add(elementInfo1);
133
134         Mockito.when(zusammenAdaptorMock.listElementsByName(any(), any(), any(), any())).thenReturn(elementInfos);
135         Assert.assertEquals(FALSE, networkPackageHealer.isHealingNeeded(ITEM_ID, new Version()));
136     }
137
138     @Test
139     public void testIsHealingNeeded_OnboardingMethod() {
140         VspDetails vspDetails = new VspDetails(ITEM_ID, new Version());
141         vspDetails.setOnboardingMethod("Manual");
142         Mockito.when(vspInfoDaoMock.get(any())).thenReturn(vspDetails);
143
144         Assert.assertEquals(FALSE, networkPackageHealer.isHealingNeeded(ITEM_ID, new Version()));
145     }
146
147     @Test
148     public void shouldHeal() throws Exception {
149         Mockito.when(zusammenAdaptorMock.getElementByName(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()))
150                 .thenReturn(Optional.of(element));
151         Mockito.when(zusammenAdaptorMock.saveElement(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()))
152                 .thenReturn(element);
153         Mockito.when(element.getSubElements()).thenReturn(Collections.singletonList(subElement));
154         Mockito.when(
155                 zusammenAdaptorMock.getElementInfoByName(Mockito.any(), Mockito.any(), Mockito.isNull(), Mockito.any())).thenReturn(Optional.of(elementInfo));
156         ArrayList<Element> subElements = new ArrayList<>();
157         Mockito.when(element.getData()).thenReturn(IOUtils.toInputStream(ANY, UTF_8));
158         Mockito.when(subElement2.getData()).thenReturn(IOUtils.toInputStream(ANY, UTF_8));
159         subElements.add(subElement);
160         subElements.add(subElement2);
161         Mockito.when(zusammenAdaptorMock.listElementData(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(subElements);
162         Info info = new Info();
163         info.setName(OrchestrationTemplateCandidateValidationData.name());
164         Mockito.when(element.getInfo()).thenReturn(info);
165         Id id = new Id();
166         Mockito.when(element.getElementId()).thenReturn(id);
167         Info subinfo = new Info();
168         Info subinfo2 = new Info();
169         Map<String, Object> props2 = new HashMap<>();
170         props2.put(FILE_SUFFIX, ANY);
171         props2.put(FILE_NAME, ANY);
172         subinfo2.setProperties(props2);
173         subinfo2.setName(ElementType.OrchestrationTemplateCandidateContent.name());
174         Map<String, Object> props = new HashMap<>();
175         props.put(FILE_SUFFIX, ANY);
176         props.put(FILE_NAME, OTHER_THAN_ANY);
177         subinfo.setProperties(props);
178         subinfo.setName(OrchestrationTemplateValidationData.name());
179         Mockito.when(subElement.getInfo()).thenReturn(subinfo);
180         Mockito.when(subElement2.getInfo()).thenReturn(subinfo2);
181         Mockito.when(candidateEntityBuilder.buildCandidateEntityFromZip(Mockito.isNull(), Mockito.any(), Mockito.any(), Mockito.isNull())).thenReturn(orchestrationData);
182         Mockito.when(orchestrationData.getFilesDataStructure()).thenReturn(ANY);
183
184         networkPackageHealer.heal(ITEM_ID, Version.valueOf("1.1"));
185
186         Mockito.verify(zusammenAdaptorMock, Mockito.times(3)).saveElement(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
187     }
188 }