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.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;
47 import javax.ws.rs.core.Response;
48 import java.util.Collection;
49 import java.util.Collections;
50 import java.util.UUID;
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;
56 @RunWith(PowerMockRunner.class)
57 @PrepareForTest({ComponentDependenciesImpl.class, ComponentDependencyModelManagerFactory.class})
58 public class ComponentDependenciesImplTest {
60 private Logger logger = LoggerFactory.getLogger(org.openecomp.sdcrests.vsp.rest.services.ComponentDependenciesImplTest.class);
63 private ComponentDependencyModelManagerFactory componentDependencyModelManagerFactory;
66 private ComponentDependencyModelManager componentDependencyModelManager;
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";
78 mockStatic(ComponentDependencyModelManagerFactory.class);
79 when(ComponentDependencyModelManagerFactory.getInstance()).thenReturn(componentDependencyModelManagerFactory);
80 when(componentDependencyModelManagerFactory.createInterface()).thenReturn(componentDependencyModelManager);
82 ComponentDependencyModelEntity e = new ComponentDependencyModelEntity();
83 e.setSourceComponentId("sourceid");
84 e.setTargetComponentId("targetid");
86 e.setVersion(new Version(versionId));
87 e.setRelation(ComponentRelationType.dependsOn.name());
91 when(componentDependencyModelManager.createComponentDependency(
92 ArgumentMatchers.any(),
93 ArgumentMatchers.eq(vspId),
94 ArgumentMatchers.any())).thenReturn(e);
97 Collection<ComponentDependencyModelEntity> entities =
98 Collections.singletonList(e);
99 when(componentDependencyModelManager.list(
100 ArgumentMatchers.eq(vspId),
101 ArgumentMatchers.any())).thenReturn(entities);
104 when(componentDependencyModelManager.get(
105 ArgumentMatchers.eq(vspId),
106 ArgumentMatchers.any(),
107 ArgumentMatchers.eq(entityId)
110 } catch (Exception e) {
111 logger.error(e.getMessage(), e);
116 public void testCreate() {
117 ComponentDependencyModel model = new ComponentDependencyModel();
118 model.setRelationType(ComponentRelationType.dependsOn.name());
119 model.setSourceId("sourceid");
120 model.setTargetId("targetid");
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);
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());
137 public void testList() {
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);
145 @SuppressWarnings("unchecked")
146 GenericCollectionWrapper<ComponentDependencyResponseDto> results =
147 (GenericCollectionWrapper<ComponentDependencyResponseDto>) e;
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());
158 public void testDelete() {
160 ComponentDependencies componentDependencies = new ComponentDependenciesImpl();
162 Response rsp = componentDependencies.delete(vspId, versionId, entityId, user);
163 Assert.assertEquals("Response should be 200", 200, rsp.getStatus());
164 Assert.assertNull(rsp.getEntity());
169 public void testUpdate() {
171 ComponentDependencies componentDependencies = new ComponentDependenciesImpl();
173 ComponentDependencyModel model = new ComponentDependencyModel();
174 model.setRelationType(ComponentRelationType.dependsOn.name());
175 model.setSourceId("sourceid");
176 model.setTargetId("targetid");
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());
184 public void testGet() {
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());
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());