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