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