Added oparent to sdc main
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / vendor-software-products-rest / vendor-software-products-rest-services / src / test / java / org / openecomp / sdcrests / vsp / rest / services / NicsImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdcrests.vsp.rest.services;
22
23 import org.apache.http.HttpStatus;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.ArgumentMatchers;
29 import org.mockito.Mock;
30 import org.openecomp.sdc.logging.api.Logger;
31 import org.openecomp.sdc.logging.api.LoggerFactory;
32 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
33 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManagerFactory;
34 import org.openecomp.sdc.vendorsoftwareproduct.NicManager;
35 import org.openecomp.sdc.vendorsoftwareproduct.NicManagerFactory;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
39 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
40 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
41 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
42 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NicCreationResponseDto;
43 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NicDto;
44 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NicRequestDto;
45 import org.openecomp.sdcrests.vendorsoftwareproducts.types.QuestionnaireResponseDto;
46 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
47 import org.powermock.core.classloader.annotations.PrepareForTest;
48 import org.powermock.modules.junit4.PowerMockRunner;
49
50 import javax.ws.rs.core.Response;
51 import java.util.Collection;
52 import java.util.Collections;
53 import java.util.UUID;
54
55 import static org.mockito.MockitoAnnotations.initMocks;
56 import static org.powermock.api.mockito.PowerMockito.mockStatic;
57 import static org.powermock.api.mockito.PowerMockito.when;
58
59 @RunWith(PowerMockRunner.class)
60 @PrepareForTest({NicsImpl.class, ComponentManagerFactory.class, NicManagerFactory.class})
61 public class NicsImplTest {
62
63   private Logger logger = LoggerFactory.getLogger(NicsImplTest.class);
64
65   @Mock
66   private NicManagerFactory nicManagerFactory;
67
68   @Mock
69   private NicManager nicManager;
70
71   @Mock
72   private ComponentManagerFactory componentManagerFactory;
73
74   @Mock
75   private ComponentManager componentManager;
76
77   private final String vspId = UUID.randomUUID().toString();
78   private final String versionId = UUID.randomUUID().toString();
79   private final String componentId = "" + System.currentTimeMillis();
80   private final String nicId = "" + System.currentTimeMillis();
81   private final String user = "cs0008";
82
83   @Before
84   public void setUp() {
85     try {
86       initMocks(this);
87
88       mockStatic(ComponentManagerFactory.class);
89       when(ComponentManagerFactory.getInstance()).thenReturn(componentManagerFactory);
90       when(componentManagerFactory.createInterface()).thenReturn(componentManager);
91
92       mockStatic(NicManagerFactory.class);
93       when(NicManagerFactory.getInstance()).thenReturn(nicManagerFactory);
94       when(nicManagerFactory.createInterface()).thenReturn(nicManager);
95
96
97
98       NicEntity e = new NicEntity();
99       e.setComponentId(componentId);
100       e.setId(nicId);
101       e.setCompositionData("{\"name\":\"nm\",\"description\":\"d\"}");
102
103
104       Collection<NicEntity> lst = Collections.singletonList(e);
105       when(nicManager.listNics(
106               ArgumentMatchers.eq(vspId),
107               ArgumentMatchers.any(),
108               ArgumentMatchers.eq(componentId))).thenReturn(lst);
109
110       when(nicManager.createNic(
111               ArgumentMatchers.any())).thenReturn(e);
112
113       CompositionEntityResponse<Nic> r = new CompositionEntityResponse<>();
114       r.setId(vspId);
115       when(nicManager.getNic(
116               ArgumentMatchers.eq(vspId),
117               ArgumentMatchers.any(),
118               ArgumentMatchers.eq(componentId),
119               ArgumentMatchers.eq(nicId))).thenReturn(r);
120
121       CompositionEntityType tpe = CompositionEntityType.component;
122       CompositionEntityValidationData data = new CompositionEntityValidationData(tpe, vspId);
123       when(nicManager.updateNic(
124               ArgumentMatchers.any())).thenReturn(data);
125
126
127       QuestionnaireResponse qr = new QuestionnaireResponse();
128       qr.setData("helloworld");
129       when(nicManager.getNicQuestionnaire(
130               ArgumentMatchers.eq(vspId),
131               ArgumentMatchers.any(),
132               ArgumentMatchers.eq(componentId),
133               ArgumentMatchers.eq(nicId))).thenReturn(qr);
134
135
136     } catch (Exception e) {
137       logger.error(e.getMessage(), e);
138     }
139   }
140
141   @Test
142   public void testList() {
143     NicsImpl bean = new NicsImpl();
144
145     Response rsp = bean.list(vspId, versionId, componentId, user);
146     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
147     Object e = rsp.getEntity();
148     Assert.assertNotNull(e);
149     @SuppressWarnings("unchecked")
150     GenericCollectionWrapper<NicDto> results = (GenericCollectionWrapper<NicDto>)e;
151     Assert.assertEquals("result length", 1, results.getListCount());
152   }
153
154
155   @Test
156   public void testCreate() {
157
158     NicRequestDto dto = new NicRequestDto();
159     dto.setDescription("hello");
160     dto.setName("name");
161     dto.setNetworkDescription("nd");
162     dto.setNetworkId(nicId);
163     dto.setNetworkType("External");
164
165     NicsImpl bean = new NicsImpl();
166     Response rsp = bean.create(dto, vspId, versionId, componentId, user);
167     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
168     Object e = rsp.getEntity();
169     Assert.assertNotNull(e);
170     try {
171       NicCreationResponseDto creationDto = (NicCreationResponseDto)e;
172       Assert.assertEquals(nicId, creationDto.getNicId());
173     } catch (ClassCastException ex) {
174       Assert.fail("unexpected class for DTO " + e.getClass().getName());
175     }
176   }
177
178
179   @Test
180   public void testDelete() {
181     NicsImpl bean = new NicsImpl();
182     Response rsp = bean.delete(vspId, versionId, componentId, nicId, user);
183     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
184     Assert.assertNull(rsp.getEntity());
185   }
186
187
188   @Test
189   public void testGet() {
190     NicsImpl bean = new NicsImpl();
191     Response rsp = bean.get(vspId, versionId, componentId, nicId, user);
192     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
193     Assert.assertNotNull(rsp.getEntity());
194   }
195
196   @Test
197   public void testUpdate() {
198     NicsImpl bean = new NicsImpl();
199     NicRequestDto dto = new NicRequestDto();
200     dto.setDescription("hello");
201     dto.setName("name");
202     dto.setNetworkDescription("nd");
203     dto.setNetworkId(nicId);
204     dto.setNetworkType("External");
205
206     Response rsp = bean.update(dto, vspId, versionId, componentId, nicId, user);
207     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
208     Assert.assertNull(rsp.getEntity());
209   }
210
211   @Test
212   public void testGetQuestionaire() {
213     NicsImpl bean = new NicsImpl();
214     Response rsp = bean.getQuestionnaire(vspId, versionId, componentId, nicId, user);
215     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
216     try {
217       QuestionnaireResponseDto dto = (QuestionnaireResponseDto)rsp.getEntity();
218       Assert.assertEquals("helloworld", dto.getData());
219     }
220     catch (Exception ex) {
221       logger.error("caught exception", ex);
222       Assert.fail(ex.getMessage());
223     }
224   }
225
226
227   @Test
228   public void testUpdateQuestionaire() {
229     NicsImpl bean = new NicsImpl();
230     Response rsp = bean.updateQuestionnaire("helloworld", vspId, versionId, componentId, nicId, user);
231     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
232     Assert.assertNull(rsp.getEntity());
233   }
234 }