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.apache.http.HttpStatus;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.ArgumentMatchers;
29 import org.mockito.Mock;
30 import org.openecomp.sdc.logging.api.Logger;
31 import org.openecomp.sdc.logging.api.LoggerFactory;
32 import org.openecomp.sdc.vendorsoftwareproduct.DeploymentFlavorManager;
33 import org.openecomp.sdc.vendorsoftwareproduct.DeploymentFlavorManagerFactory;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
35 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
36 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.DeploymentFlavor;
39 import org.openecomp.sdc.versioning.dao.types.Version;
40 import org.openecomp.sdcrests.vendorsoftwareproducts.types.DeploymentFlavorCreationDto;
41 import org.openecomp.sdcrests.vendorsoftwareproducts.types.DeploymentFlavorRequestDto;
42 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
43 import org.powermock.core.classloader.annotations.PrepareForTest;
44 import org.powermock.modules.junit4.PowerMockRunner;
46 import javax.ws.rs.core.Response;
47 import java.util.Collection;
48 import java.util.Collections;
49 import java.util.UUID;
51 import static org.mockito.MockitoAnnotations.initMocks;
52 import static org.powermock.api.mockito.PowerMockito.mockStatic;
53 import static org.powermock.api.mockito.PowerMockito.when;
55 @RunWith(PowerMockRunner.class)
56 @PrepareForTest({DeploymentFlavorsImpl.class, DeploymentFlavorManagerFactory.class})
57 public class DeploymentFlavorsImplTest {
59 private Logger logger = LoggerFactory.getLogger(DeploymentFlavorsImplTest.class);
63 private DeploymentFlavorManagerFactory deploymentFlavorManagerFactory;
66 private DeploymentFlavorManager deploymentFlavorManager;
68 private final String vspId = UUID.randomUUID().toString();
69 private final String versionId = UUID.randomUUID().toString();
70 private final String deploymentFlavorId = "" + System.currentTimeMillis();
71 private final String user = "cs0008";
78 mockStatic(DeploymentFlavorManagerFactory.class);
79 when(DeploymentFlavorManagerFactory.getInstance()).thenReturn(deploymentFlavorManagerFactory);
80 when(deploymentFlavorManagerFactory.createInterface()).thenReturn(deploymentFlavorManager);
82 DeploymentFlavorEntity e = new DeploymentFlavorEntity();
83 e.setId(deploymentFlavorId);
85 e.setVersion(new Version(versionId));
87 Collection<DeploymentFlavorEntity> lst = Collections.singletonList(e);
88 when(deploymentFlavorManager.listDeploymentFlavors(
89 ArgumentMatchers.eq(vspId),
90 ArgumentMatchers.any())).thenReturn(lst);
92 when(deploymentFlavorManager.createDeploymentFlavor(
93 ArgumentMatchers.any())).thenReturn(e);
95 CompositionEntityResponse<DeploymentFlavor> r = new CompositionEntityResponse<>();
97 when(deploymentFlavorManager.getDeploymentFlavor(
98 ArgumentMatchers.eq(vspId),
99 ArgumentMatchers.any(),
100 ArgumentMatchers.eq(deploymentFlavorId))).thenReturn(r);
102 CompositionEntityType tpe = CompositionEntityType.component;
103 CompositionEntityValidationData data = new CompositionEntityValidationData(tpe, vspId);
104 when(deploymentFlavorManager.updateDeploymentFlavor(
105 ArgumentMatchers.any())).thenReturn(data);
109 when(deploymentFlavorManager.getDeploymentFlavorSchema(
110 ArgumentMatchers.eq(vspId),
111 ArgumentMatchers.any())).thenReturn(r);
114 } catch (Exception e) {
115 logger.error(e.getMessage(), e);
120 public void testList() {
121 DeploymentFlavorsImpl dfi = new DeploymentFlavorsImpl();
123 Response rsp = dfi.list(vspId, versionId, user);
124 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
125 Object e = rsp.getEntity();
126 Assert.assertNotNull(e);
127 @SuppressWarnings("unchecked")
128 GenericCollectionWrapper<DeploymentFlavorCreationDto> results = (GenericCollectionWrapper<DeploymentFlavorCreationDto>) e;
129 Assert.assertEquals("result length", 1, results.getListCount());
133 public void testCreate() {
135 DeploymentFlavorRequestDto dto = new DeploymentFlavorRequestDto();
136 dto.setDescription("hello");
137 dto.setModel("model");
138 dto.setFeatureGroupId("fgi");
140 DeploymentFlavorsImpl dfi = new DeploymentFlavorsImpl();
141 Response rsp = dfi.create(dto, vspId, versionId, user);
142 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
143 Object e = rsp.getEntity();
144 Assert.assertNotNull(e);
146 DeploymentFlavorCreationDto responseDto = (DeploymentFlavorCreationDto)e;
147 Assert.assertEquals(deploymentFlavorId, responseDto.getDeploymentFlavorId());
148 } catch (ClassCastException ex) {
149 Assert.fail("unexpected class for DTO " + e.getClass().getName());
155 public void testDelete() {
156 DeploymentFlavorsImpl dfi = new DeploymentFlavorsImpl();
157 Response rsp = dfi.delete(vspId, versionId, deploymentFlavorId, user);
158 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
159 Assert.assertNull(rsp.getEntity());
164 public void testGet() {
165 DeploymentFlavorsImpl dfi = new DeploymentFlavorsImpl();
166 Response rsp = dfi.get(vspId, versionId, deploymentFlavorId, user);
167 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
168 Assert.assertNotNull(rsp.getEntity());
172 public void testGetSchema() {
173 DeploymentFlavorsImpl dfi = new DeploymentFlavorsImpl();
174 Response rsp = dfi.get(vspId, versionId, deploymentFlavorId, user);
175 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
176 Assert.assertNotNull(rsp.getEntity());
180 public void testUpdate() {
181 DeploymentFlavorsImpl dfi = new DeploymentFlavorsImpl();
182 DeploymentFlavorRequestDto dto = new DeploymentFlavorRequestDto();
183 Response rsp = dfi.update(dto, vspId, versionId, deploymentFlavorId, user);
184 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
185 Assert.assertNull(rsp.getEntity());