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 / NetworksImplTest.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 static org.mockito.Mockito.when;
24 import static org.mockito.MockitoAnnotations.openMocks;
25
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.UUID;
29 import javax.ws.rs.core.Response;
30 import org.apache.http.HttpStatus;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.ArgumentMatchers;
35 import org.mockito.Mock;
36 import org.openecomp.sdc.vendorsoftwareproduct.NetworkManager;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
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.Network;
42 import org.openecomp.sdc.versioning.dao.types.Version;
43 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NetworkDto;
44 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NetworkRequestDto;
45 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
46 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
47
48 public class NetworksImplTest {
49
50     @Mock
51     private NetworkManager mockedNetworkManager;
52
53     private final String vspId = UUID.randomUUID().toString();
54     private final String versionId = UUID.randomUUID().toString();
55     private final String networkId = "" + System.currentTimeMillis();
56     private final String user = "cs0008";
57
58     @Before
59     public void setUp() {
60         openMocks(this);
61
62         NetworkEntity e = new NetworkEntity();
63         e.setId(networkId);
64         e.setVspId(vspId);
65         e.setVersion(new Version(versionId));
66         e.setCompositionData("{\"name\":\"nm\",\"description\":\"d\"}");
67
68         Collection<NetworkEntity> lst = Collections.singletonList(e);
69         when(mockedNetworkManager.listNetworks(
70             ArgumentMatchers.eq(vspId),
71             ArgumentMatchers.any())).thenReturn(lst);
72
73         when(mockedNetworkManager.createNetwork(
74             ArgumentMatchers.any())).thenReturn(e);
75
76         CompositionEntityResponse<Network> r = new CompositionEntityResponse<>();
77         r.setId(vspId);
78         when(mockedNetworkManager.getNetwork(
79             ArgumentMatchers.eq(vspId),
80             ArgumentMatchers.any(),
81             ArgumentMatchers.eq(networkId))).thenReturn(r);
82
83         CompositionEntityType tpe = CompositionEntityType.component;
84         CompositionEntityValidationData data = new CompositionEntityValidationData(tpe, vspId);
85         when(mockedNetworkManager.updateNetwork(
86             ArgumentMatchers.any())).thenReturn(data);
87     }
88
89     @Test
90     public void testList() {
91         NetworksImpl bean = new NetworksImpl(mockedNetworkManager);
92
93         Response rsp = bean.list(vspId, versionId, user);
94         Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
95         Object e = rsp.getEntity();
96         Assert.assertNotNull(e);
97         @SuppressWarnings("unchecked")
98         GenericCollectionWrapper<NetworkDto> results = (GenericCollectionWrapper<NetworkDto>) e;
99         Assert.assertEquals("result length", 1, results.getListCount());
100     }
101
102
103     @Test
104     public void testCreate() {
105
106         NetworkRequestDto dto = new NetworkRequestDto();
107         dto.setName("name");
108         dto.setDhcp(true);
109
110         NetworksImpl bean = new NetworksImpl(mockedNetworkManager);
111         Response rsp = bean.create(dto, vspId, versionId, user);
112         Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
113         Object e = rsp.getEntity();
114         Assert.assertNotNull(e);
115         try {
116             StringWrapperResponse s = (StringWrapperResponse) e;
117             Assert.assertEquals(networkId, s.getValue());
118         } catch (ClassCastException ex) {
119             Assert.fail("unexpected class for DTO " + e.getClass().getName());
120         }
121     }
122
123
124     @Test
125     public void testDelete() {
126         NetworksImpl bean = new NetworksImpl(mockedNetworkManager);
127         Response rsp = bean.delete(vspId, versionId, networkId, user);
128         Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
129         Assert.assertNull(rsp.getEntity());
130     }
131
132
133     @Test
134     public void testGet() {
135         NetworksImpl bean = new NetworksImpl(mockedNetworkManager);
136         Response rsp = bean.get(vspId, versionId, networkId, user);
137         Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
138         Assert.assertNotNull(rsp.getEntity());
139     }
140
141     @Test
142     public void testUpdate() {
143         NetworksImpl bean = new NetworksImpl(mockedNetworkManager);
144         NetworkRequestDto dto = new NetworkRequestDto();
145         Response rsp = bean.update(dto, vspId, versionId, networkId, user);
146         Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
147         Assert.assertNull(rsp.getEntity());
148     }
149 }