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