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 / ComponentDependenciesImplTest.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.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.ArgumentMatchers;
27 import org.mockito.Mock;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30 import org.openecomp.sdc.vendorsoftwareproduct.ComponentDependencyModelManager;
31 import org.openecomp.sdc.vendorsoftwareproduct.ComponentDependencyModelManagerFactory;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
33 import org.openecomp.sdc.versioning.dao.types.Version;
34 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentDependencyCreationDto;
35 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentDependencyModel;
36 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentDependencyResponseDto;
37 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentRelationType;
38 import org.openecomp.sdcrests.vsp.rest.ComponentDependencies;
39 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
40
41 import javax.ws.rs.core.Response;
42 import java.util.Collection;
43 import java.util.Collections;
44 import java.util.UUID;
45
46 import static org.mockito.MockitoAnnotations.openMocks;
47 import static org.mockito.Mockito.when;
48
49 public class ComponentDependenciesImplTest {
50
51   private Logger logger = LoggerFactory.getLogger(org.openecomp.sdcrests.vsp.rest.services.ComponentDependenciesImplTest.class);
52
53   @Mock
54   private ComponentDependencyModelManagerFactory componentDependencyModelManagerFactory;
55
56   @Mock
57   private ComponentDependencyModelManager componentDependencyModelManager;
58
59   private final String vspId = UUID.randomUUID().toString();
60   private final String versionId = UUID.randomUUID().toString();
61   private final String entityId = "" + System.currentTimeMillis();
62   private final String user = "cs0008";
63
64   private ComponentDependencies componentDependencies;
65
66   @Before
67   public void setUp() {
68     try {
69       openMocks(this);
70
71       ComponentDependencyModelEntity e = new ComponentDependencyModelEntity();
72       e.setSourceComponentId("sourceid");
73       e.setTargetComponentId("targetid");
74       e.setVspId(vspId);
75       e.setVersion(new Version(versionId));
76       e.setRelation(ComponentRelationType.dependsOn.name());
77       e.setId(entityId);
78
79       // create
80       when(componentDependencyModelManager.createComponentDependency(
81           ArgumentMatchers.any(),
82           ArgumentMatchers.eq(vspId),
83           ArgumentMatchers.any())).thenReturn(e);
84
85       // list
86       Collection<ComponentDependencyModelEntity> entities =
87           Collections.singletonList(e);
88       when(componentDependencyModelManager.list(
89           ArgumentMatchers.eq(vspId),
90           ArgumentMatchers.any())).thenReturn(entities);
91
92       // get
93       when(componentDependencyModelManager.get(
94           ArgumentMatchers.eq(vspId),
95           ArgumentMatchers.any(),
96           ArgumentMatchers.eq(entityId)
97           )).thenReturn(e);
98
99       componentDependencies = new ComponentDependenciesImpl(componentDependencyModelManager);
100
101     } catch (Exception e) {
102       logger.error(e.getMessage(), e);
103     }
104   }
105
106   @Test
107   public void testCreate() {
108     ComponentDependencyModel model = new ComponentDependencyModel();
109     model.setRelationType(ComponentRelationType.dependsOn.name());
110     model.setSourceId("sourceid");
111     model.setTargetId("targetid");
112
113     Response rsp = componentDependencies.create(model, vspId, versionId, user);
114     Assert.assertEquals("Response should be 200", 200, rsp.getStatus());
115     Object e = rsp.getEntity();
116     Assert.assertNotNull(e);
117     try {
118       ComponentDependencyCreationDto dto = (ComponentDependencyCreationDto) e;
119       Assert.assertEquals("resulting entityId must match", dto.getId(), entityId);
120     } catch (ClassCastException ex) {
121       Assert.fail("unexpected class for DTO " + e.getClass().getName());
122     }
123   }
124
125   @Test
126   public void testList() {
127
128     Response rsp = componentDependencies.list(vspId, versionId, user);
129     Assert.assertEquals("Response should be 200", 200, rsp.getStatus());
130     Object e = rsp.getEntity();
131     Assert.assertNotNull(e);
132     try {
133       @SuppressWarnings("unchecked")
134       GenericCollectionWrapper<ComponentDependencyResponseDto> results =
135           (GenericCollectionWrapper<ComponentDependencyResponseDto>) e;
136
137       Assert.assertEquals("result length", 1, results.getListCount());
138       Assert.assertEquals("resulting entityId must match", results.getResults().get(0).getId(), entityId);
139     } catch (ClassCastException ex) {
140       Assert.fail("unexpected class for DTO " + e.getClass().getName());
141     }
142   }
143
144
145   @Test
146   public void testDelete() {
147
148     Response rsp = componentDependencies.delete(vspId, versionId, entityId, user);
149     Assert.assertEquals("Response should be 200", 200, rsp.getStatus());
150     Assert.assertNull(rsp.getEntity());
151   }
152
153
154   @Test
155   public void testUpdate() {
156
157     ComponentDependencyModel model = new ComponentDependencyModel();
158     model.setRelationType(ComponentRelationType.dependsOn.name());
159     model.setSourceId("sourceid");
160     model.setTargetId("targetid");
161
162     Response rsp = componentDependencies.update(model, vspId, versionId, entityId, user);
163     Assert.assertEquals("Response should be 200", 200, rsp.getStatus());
164     Assert.assertNull(rsp.getEntity());
165   }
166
167   @Test
168   public void testGet() {
169
170     Response rsp = componentDependencies.get(vspId, versionId, entityId, user);
171     Assert.assertEquals("Response should be 200", 200, rsp.getStatus());
172     Assert.assertNotNull(rsp.getEntity());
173     try {
174       ComponentDependencyResponseDto dto = (ComponentDependencyResponseDto) rsp.getEntity();
175       Assert.assertEquals("resulting entityId must match", dto.getId(), entityId);
176     } catch (ClassCastException ex) {
177       Assert.fail("unexpected class for DTO " + rsp.getEntity().getClass().getName());
178     }
179   }
180 }