Improve testing stability
[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.mockito.ArgumentMatchers;
28 import org.mockito.Mock;
29 import org.openecomp.sdc.logging.api.Logger;
30 import org.openecomp.sdc.logging.api.LoggerFactory;
31 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
32 import org.openecomp.sdc.vendorsoftwareproduct.NicManager;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
34 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
35 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
36 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
39 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NicCreationResponseDto;
40 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NicDto;
41 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NicRequestDto;
42 import org.openecomp.sdcrests.vendorsoftwareproducts.types.QuestionnaireResponseDto;
43 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
44
45 import javax.ws.rs.core.Response;
46 import java.util.Collection;
47 import java.util.Collections;
48 import java.util.UUID;
49
50 import static org.mockito.MockitoAnnotations.openMocks;
51 import static org.mockito.Mockito.when;
52
53 public class NicsImplTest {
54
55   private Logger logger = LoggerFactory.getLogger(NicsImplTest.class);
56
57   @Mock
58   private NicManager nicManager;
59
60   @Mock
61   private ComponentManager componentManager;
62
63   private final String vspId = UUID.randomUUID().toString();
64   private final String versionId = UUID.randomUUID().toString();
65   private final String componentId = "" + System.currentTimeMillis();
66   private final String nicId = "" + System.currentTimeMillis();
67   private final String user = "cs0008";
68
69   private NicsImpl bean;
70
71   @Before
72   public void setUp() {
73     try {
74       openMocks(this);
75
76       NicEntity e = new NicEntity();
77       e.setComponentId(componentId);
78       e.setId(nicId);
79       e.setCompositionData("{\"name\":\"nm\",\"description\":\"d\"}");
80
81
82       Collection<NicEntity> lst = Collections.singletonList(e);
83       when(nicManager.listNics(
84               ArgumentMatchers.eq(vspId),
85               ArgumentMatchers.any(),
86               ArgumentMatchers.eq(componentId))).thenReturn(lst);
87
88       when(nicManager.createNic(
89               ArgumentMatchers.any())).thenReturn(e);
90
91       CompositionEntityResponse<Nic> r = new CompositionEntityResponse<>();
92       r.setId(vspId);
93       when(nicManager.getNic(
94               ArgumentMatchers.eq(vspId),
95               ArgumentMatchers.any(),
96               ArgumentMatchers.eq(componentId),
97               ArgumentMatchers.eq(nicId))).thenReturn(r);
98
99       CompositionEntityType tpe = CompositionEntityType.component;
100       CompositionEntityValidationData data = new CompositionEntityValidationData(tpe, vspId);
101       when(nicManager.updateNic(
102               ArgumentMatchers.any())).thenReturn(data);
103
104
105       QuestionnaireResponse qr = new QuestionnaireResponse();
106       qr.setData("helloworld");
107       when(nicManager.getNicQuestionnaire(
108               ArgumentMatchers.eq(vspId),
109               ArgumentMatchers.any(),
110               ArgumentMatchers.eq(componentId),
111               ArgumentMatchers.eq(nicId))).thenReturn(qr);
112
113       bean = new NicsImpl(nicManager, componentManager);
114
115     } catch (Exception e) {
116       logger.error(e.getMessage(), e);
117     }
118   }
119
120   @Test
121   public void testList() {
122
123     Response rsp = bean.list(vspId, versionId, componentId, user);
124     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
125     Object e = rsp.getEntity();
126     Assert.assertNotNull(e);
127     @SuppressWarnings("unchecked")
128     GenericCollectionWrapper<NicDto> results = (GenericCollectionWrapper<NicDto>)e;
129     Assert.assertEquals("result length", 1, results.getListCount());
130   }
131
132
133   @Test
134   public void testCreate() {
135
136     NicRequestDto dto = new NicRequestDto();
137     dto.setDescription("hello");
138     dto.setName("name");
139     dto.setNetworkDescription("nd");
140     dto.setNetworkId(nicId);
141     dto.setNetworkType("External");
142
143     Response rsp = bean.create(dto, vspId, versionId, componentId, user);
144     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
145     Object e = rsp.getEntity();
146     Assert.assertNotNull(e);
147     try {
148       NicCreationResponseDto creationDto = (NicCreationResponseDto)e;
149       Assert.assertEquals(nicId, creationDto.getNicId());
150     } catch (ClassCastException ex) {
151       Assert.fail("unexpected class for DTO " + e.getClass().getName());
152     }
153   }
154
155
156   @Test
157   public void testDelete() {
158     Response rsp = bean.delete(vspId, versionId, componentId, nicId, user);
159     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
160     Assert.assertNull(rsp.getEntity());
161   }
162
163
164   @Test
165   public void testGet() {
166     Response rsp = bean.get(vspId, versionId, componentId, nicId, user);
167     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
168     Assert.assertNotNull(rsp.getEntity());
169   }
170
171   @Test
172   public void testUpdate() {
173     NicRequestDto dto = new NicRequestDto();
174     dto.setDescription("hello");
175     dto.setName("name");
176     dto.setNetworkDescription("nd");
177     dto.setNetworkId(nicId);
178     dto.setNetworkType("External");
179
180     Response rsp = bean.update(dto, vspId, versionId, componentId, nicId, user);
181     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
182     Assert.assertNull(rsp.getEntity());
183   }
184
185   @Test
186   public void testGetQuestionaire() {
187     Response rsp = bean.getQuestionnaire(vspId, versionId, componentId, nicId, user);
188     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
189     try {
190       QuestionnaireResponseDto dto = (QuestionnaireResponseDto)rsp.getEntity();
191       Assert.assertEquals("helloworld", dto.getData());
192     }
193     catch (Exception ex) {
194       logger.error("caught exception", ex);
195       Assert.fail(ex.getMessage());
196     }
197   }
198
199
200   @Test
201   public void testUpdateQuestionaire() {
202     Response rsp = bean.updateQuestionnaire("helloworld", vspId, versionId, componentId, nicId, user);
203     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
204     Assert.assertNull(rsp.getEntity());
205   }
206 }