2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdcrests.vsp.rest.services;
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;
45 import javax.ws.rs.core.Response;
46 import java.util.Collection;
47 import java.util.Collections;
48 import java.util.UUID;
50 import static org.mockito.MockitoAnnotations.openMocks;
51 import static org.mockito.Mockito.when;
53 public class NicsImplTest {
55 private Logger logger = LoggerFactory.getLogger(NicsImplTest.class);
58 private NicManager nicManager;
61 private ComponentManager componentManager;
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";
69 private NicsImpl bean;
76 NicEntity e = new NicEntity();
77 e.setComponentId(componentId);
79 e.setCompositionData("{\"name\":\"nm\",\"description\":\"d\"}");
82 Collection<NicEntity> lst = Collections.singletonList(e);
83 when(nicManager.listNics(
84 ArgumentMatchers.eq(vspId),
85 ArgumentMatchers.any(),
86 ArgumentMatchers.eq(componentId))).thenReturn(lst);
88 when(nicManager.createNic(
89 ArgumentMatchers.any())).thenReturn(e);
91 CompositionEntityResponse<Nic> r = new CompositionEntityResponse<>();
93 when(nicManager.getNic(
94 ArgumentMatchers.eq(vspId),
95 ArgumentMatchers.any(),
96 ArgumentMatchers.eq(componentId),
97 ArgumentMatchers.eq(nicId))).thenReturn(r);
99 CompositionEntityType tpe = CompositionEntityType.component;
100 CompositionEntityValidationData data = new CompositionEntityValidationData(tpe, vspId);
101 when(nicManager.updateNic(
102 ArgumentMatchers.any())).thenReturn(data);
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);
113 bean = new NicsImpl(nicManager, componentManager);
115 } catch (Exception e) {
116 logger.error(e.getMessage(), e);
121 public void testList() {
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());
134 public void testCreate() {
136 NicRequestDto dto = new NicRequestDto();
137 dto.setDescription("hello");
139 dto.setNetworkDescription("nd");
140 dto.setNetworkId(nicId);
141 dto.setNetworkType("External");
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);
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());
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());
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());
172 public void testUpdate() {
173 NicRequestDto dto = new NicRequestDto();
174 dto.setDescription("hello");
176 dto.setNetworkDescription("nd");
177 dto.setNetworkId(nicId);
178 dto.setNetworkType("External");
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());
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());
190 QuestionnaireResponseDto dto = (QuestionnaireResponseDto)rsp.getEntity();
191 Assert.assertEquals("helloworld", dto.getData());
193 catch (Exception ex) {
194 logger.error("caught exception", ex);
195 Assert.fail(ex.getMessage());
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());