2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
19 * Modifications copyright (c) 2019 Nokia
20 * ================================================================================
23 package org.openecomp.sdc.be.externalapi.servlet;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.doThrow;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
32 import fj.data.Either;
33 import java.util.HashMap;
34 import java.util.LinkedList;
35 import java.util.List;
37 import javax.servlet.ServletContext;
38 import javax.ws.rs.client.Entity;
39 import javax.ws.rs.core.Application;
40 import javax.ws.rs.core.GenericType;
41 import javax.ws.rs.core.MediaType;
42 import javax.ws.rs.core.Response;
43 import org.glassfish.jersey.server.ResourceConfig;
44 import org.glassfish.jersey.test.JerseyTest;
45 import org.glassfish.jersey.test.TestProperties;
46 import org.junit.jupiter.api.AfterEach;
47 import org.junit.jupiter.api.BeforeAll;
48 import org.junit.jupiter.api.BeforeEach;
49 import org.junit.jupiter.api.Test;
50 import org.openecomp.sdc.be.components.impl.ComponentLocker;
51 import org.openecomp.sdc.be.components.impl.ExternalRefsBusinessLogic;
52 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
53 import org.openecomp.sdc.be.components.path.utils.GraphTestUtils;
54 import org.openecomp.sdc.be.components.validation.AccessValidations;
55 import org.openecomp.sdc.be.config.Configuration.HeatDeploymentArtifactTimeout;
56 import org.openecomp.sdc.be.config.ConfigurationManager;
57 import org.openecomp.sdc.be.dao.DAOJanusGraphStrategy;
58 import org.openecomp.sdc.be.dao.JanusGraphClientStrategy;
59 import org.openecomp.sdc.be.dao.api.ActionStatus;
60 import org.openecomp.sdc.be.dao.impl.HealingPipelineDao;
61 import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphDao;
62 import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphGenericDao;
63 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphClient;
64 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
65 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
66 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
67 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
68 import org.openecomp.sdc.be.dto.ExternalRefDTO;
69 import org.openecomp.sdc.be.impl.ComponentsUtils;
70 import org.openecomp.sdc.be.impl.ServletUtils;
71 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
72 import org.openecomp.sdc.be.model.Component;
73 import org.openecomp.sdc.be.model.User;
74 import org.openecomp.sdc.be.model.jsonjanusgraph.config.ContainerInstanceTypesData;
75 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArchiveOperation;
76 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.CategoryOperation;
77 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ExternalReferencesOperation;
78 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.GroupsOperation;
79 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTemplateOperation;
80 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTypeOperation;
81 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
82 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
83 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.IdMapper;
84 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
85 import org.openecomp.sdc.be.model.operations.impl.ModelElementOperation;
86 import org.openecomp.sdc.be.model.operations.impl.ModelOperation;
87 import org.openecomp.sdc.be.model.operations.impl.OperationUtils;
88 import org.openecomp.sdc.be.servlets.exception.ComponentExceptionMapper;
89 import org.openecomp.sdc.be.servlets.exception.DefaultExceptionMapper;
90 import org.openecomp.sdc.be.servlets.exception.StorageExceptionMapper;
91 import org.openecomp.sdc.be.user.Role;
92 import org.openecomp.sdc.be.user.UserBusinessLogic;
93 import org.openecomp.sdc.common.api.ConfigurationSource;
94 import org.openecomp.sdc.common.api.Constants;
95 import org.openecomp.sdc.common.impl.ExternalConfiguration;
96 import org.openecomp.sdc.common.impl.FSConfigurationSource;
97 import org.openecomp.sdc.exception.ResponseFormat;
98 import org.springframework.context.ApplicationContext;
99 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
100 import org.springframework.context.annotation.Bean;
101 import org.springframework.context.annotation.Configuration;
102 import org.springframework.context.annotation.PropertySource;
103 import org.springframework.http.HttpStatus;
104 import org.springframework.web.context.WebApplicationContext;
106 class ExternalRefsServletTest extends JerseyTest {
108 private static final String COMPONENT_ID = "ci-MyComponentName";
109 private static final String FAKE_COMPONENT_ID = "ci-MyFAKEComponentName";
110 private static final String MONITORING_OBJECT_TYPE = "monitoring";
111 private static final String WORKFLOW_OBJECT_TYPE = "workflow";
112 private static final String VERSION = "0.1";
113 private static final String FAKE_VERSION = "0.5";
114 private static final String REF_1 = "ref1";
115 private static final String REF_2 = "ref2";
116 private static final String REF_3 = "ref3";
117 private static final String REF_4 = "ref4";
118 private static final String REF_5 = "ref5";
120 private static final String REF_6 = "ref6";
121 private static String serviceVertexUuid;
122 private static String resourceVertexUuid;
124 private static final User adminUser = new User("admin", "admin", "admin", "admin@email.com", Role.ADMIN.name(),
125 System.currentTimeMillis());
126 private static final User designerUser = new User("designer", "designer", "designer", "designer@email.com",
127 Role.DESIGNER.name(), System
128 .currentTimeMillis());
129 private static final User otherDesignerUser = new User("otherDesigner", "otherDesigner", "otherDesigner",
130 "otherDesigner@email.com", Role.DESIGNER
131 .name(), System.currentTimeMillis());
132 private static final User otherUser = new User("other", "other", "other", "other@email.com", Role.DESIGNER.name(),
133 System.currentTimeMillis());
135 private static final IdMapper idMapper = mock(IdMapper.class);
136 private static final WebAppContextWrapper webAppContextWrapper = mock(WebAppContextWrapper.class);
137 private static final ServletContext servletContext = mock(ServletContext.class);
138 private static final WebApplicationContext webApplicationContext = mock(WebApplicationContext.class);
139 private static final ServletUtils servletUtils = mock(ServletUtils.class);
140 private static final ComponentsUtils componentUtils = mock(ComponentsUtils.class);
141 private static final ResponseFormat responseFormat = mock(ResponseFormat.class);
142 private static final ResponseFormat notFoundResponseFormat = mock(ResponseFormat.class);
143 private static final ResponseFormat badRequestResponseFormat = mock(ResponseFormat.class);
144 private static final UserBusinessLogic userAdmin = mock(UserBusinessLogic.class);
145 private static final ToscaOperationFacade toscaOperationFacadeMock = mock(ToscaOperationFacade.class);
146 private static final AccessValidations accessValidationsMock = mock(AccessValidations.class);
147 private static final ComponentLocker componentLocker = mock(ComponentLocker.class);
148 private static final HealingJanusGraphGenericDao janusGraphGenericDao = mock(HealingJanusGraphGenericDao.class);
149 private static final IGraphLockOperation graphLockOperation = mock(IGraphLockOperation.class);
150 private static final ByResponseFormatComponentException ce = mock(ByResponseFormatComponentException.class);
151 private static final Component resourceComponentMock = mock(Component.class);
152 private static final Component serviceComponentMock = mock(Component.class);
153 private static final ModelOperation modelOperation = mock(ModelOperation.class);
154 private static final ModelElementOperation modelElementOperation = mock(ModelElementOperation.class);
157 public static void setup() {
159 //Needed for User Authorization
160 //========================================================================================================================
161 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
162 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
163 when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils);
164 when(servletUtils.getUserAdmin()).thenReturn(userAdmin);
165 when(servletUtils.getComponentsUtils()).thenReturn(componentUtils);
166 when(componentUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)).thenReturn(responseFormat);
167 when(responseFormat.getStatus()).thenReturn(HttpStatus.UNAUTHORIZED.value());
169 when(ce.getResponseFormat()).thenReturn(responseFormat);
170 doThrow(ce).when(accessValidationsMock).validateUserCanWorkOnComponent(any(), any(), eq(otherDesignerUser.getUserId()), any());
171 doThrow(ce).when(accessValidationsMock).validateUserCanWorkOnComponent(any(), any(), eq(otherUser.getUserId()), any());
173 //Needed for error configuration
174 when(notFoundResponseFormat.getStatus()).thenReturn(HttpStatus.NOT_FOUND.value());
175 when(badRequestResponseFormat.getStatus()).thenReturn(HttpStatus.BAD_REQUEST.value());
176 when(componentUtils.getResponseFormat(eq(ActionStatus.RESOURCE_NOT_FOUND), (String[]) any())).thenReturn(notFoundResponseFormat);
177 when(componentUtils.getResponseFormat(eq(ActionStatus.COMPONENT_VERSION_NOT_FOUND), (String[]) any())).thenReturn(notFoundResponseFormat);
178 when(componentUtils.getResponseFormat(eq(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND), (String[]) any())).thenReturn(notFoundResponseFormat);
179 when(componentUtils.getResponseFormat(eq(ActionStatus.EXT_REF_NOT_FOUND), (String[]) any())).thenReturn(notFoundResponseFormat);
180 when(componentUtils.getResponseFormat(eq(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID), (String[]) any())).thenReturn(badRequestResponseFormat);
181 when(userAdmin.getUser(adminUser.getUserId(), false)).thenReturn(adminUser);
182 when(userAdmin.getUser(designerUser.getUserId(), false)).thenReturn(designerUser);
183 when(userAdmin.getUser(otherUser.getUserId(), false)).thenReturn(otherUser);
184 //========================================================================================================================
186 String appConfigDir = "src/test/resources/config/catalog-be";
187 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
188 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
190 org.openecomp.sdc.be.config.Configuration configuration = new org.openecomp.sdc.be.config.Configuration();
191 configuration.setJanusGraphInMemoryGraph(true);
192 HeatDeploymentArtifactTimeout heatDeploymentArtifactTimeout = new HeatDeploymentArtifactTimeout();
193 heatDeploymentArtifactTimeout.setDefaultMinutes(30);
194 configuration.setAafAuthNeeded(false);
195 configuration.setHeatArtifactDeploymentTimeout(heatDeploymentArtifactTimeout);
196 configurationManager.setConfiguration(configuration);
197 ExternalConfiguration.setAppName("catalog-be");
201 public void before() throws Exception {
204 when(resourceComponentMock.getVersion()).thenReturn(VERSION);
205 when(resourceComponentMock.getUniqueId()).thenReturn(resourceVertexUuid);
207 when(serviceComponentMock.getVersion()).thenReturn(VERSION);
208 when(serviceComponentMock.getUniqueId()).thenReturn(serviceVertexUuid);
210 List<Component> listComponents = new LinkedList<>();
211 listComponents.add(serviceComponentMock);
213 when(toscaOperationFacadeMock.getComponentListByUuid(eq(serviceVertexUuid), any())).thenReturn(Either.left(listComponents));
214 when(toscaOperationFacadeMock.getComponentByUuidAndVersion(serviceVertexUuid, VERSION)).thenReturn(Either.left(serviceComponentMock));
215 when(toscaOperationFacadeMock.getComponentByUuidAndVersion(resourceVertexUuid, VERSION)).thenReturn(Either.left(resourceComponentMock));
216 when(toscaOperationFacadeMock.getLatestComponentByUuid(eq(serviceVertexUuid), any())).thenReturn(Either.left(listComponents.get(0)));
217 when(toscaOperationFacadeMock.getLatestComponentByUuid(eq(resourceVertexUuid), any())).thenReturn(Either.left(resourceComponentMock));
221 void after() throws Exception {
226 void testGetExternalRefsForExistingComponentInstance() {
228 .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid,
229 VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
231 Response response = target()
234 .accept(MediaType.APPLICATION_JSON)
235 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
236 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
239 List<String> dto = response.readEntity(new GenericType<List<String>>() {
241 assertThat(dto).containsExactly(REF_1, REF_2, REF_3, REF_5);
242 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
246 void testGetExternalRefsForAsset() {
248 .format("/v1/catalog/services/%s/version/%s/externalReferences/%s", serviceVertexUuid, VERSION,
249 MONITORING_OBJECT_TYPE);
251 Response response = target()
254 .accept(MediaType.APPLICATION_JSON)
255 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
256 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
259 Map<String, List<String>> dtoMap = response.readEntity(new GenericType<HashMap<String, List<String>>>() {
261 assertThat(dtoMap.get(COMPONENT_ID)).containsExactly(REF_1, REF_2, REF_3, REF_5);
262 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
266 void testGetExternalRefsForAssetWithMissingEcompHeader() {
268 .format("/v1/catalog/services/%s/version/%s/externalReferences/%s", serviceVertexUuid, VERSION,
269 MONITORING_OBJECT_TYPE);
271 //No X-Ecomp-Instance-ID header
272 Response response = target()
275 .accept(MediaType.APPLICATION_JSON)
276 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
279 assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
283 void testAddExternalRefForResource() {
285 .format("/v1/catalog/resources/%s/resourceInstances/%s/externalReferences/%s", resourceVertexUuid,
286 COMPONENT_ID, MONITORING_OBJECT_TYPE);
287 String getPath = String
288 .format("/v1/catalog/resources/%s/version/%s/externalReferences/%s", resourceVertexUuid, VERSION,
289 MONITORING_OBJECT_TYPE);
291 Response response = target()
293 .request(MediaType.APPLICATION_JSON)
294 .accept(MediaType.APPLICATION_JSON)
295 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
296 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
297 .post(Entity.json(new ExternalRefDTO(REF_1)));
299 assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value());
301 //Check that GET will include the new reference
305 .accept(MediaType.APPLICATION_JSON)
306 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
307 .header(Constants.USER_ID_HEADER, adminUser.getUserId())
310 Map<String, List<String>> dto = response.readEntity(new GenericType<Map<String, List<String>>>() {
312 assertThat(dto.get(COMPONENT_ID)).containsExactlyInAnyOrder(REF_1);
313 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
317 void testAddExternalRefForExistingComponentInstance() {
319 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid,
320 COMPONENT_ID, MONITORING_OBJECT_TYPE);
321 String getPath = String
322 .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid,
323 VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
324 Response response = target()
326 .request(MediaType.APPLICATION_JSON)
327 .accept(MediaType.APPLICATION_JSON)
328 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
329 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
330 .post(Entity.json(new ExternalRefDTO(REF_4)));
332 assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value());
334 //Check that GET will include the new reference
338 .accept(MediaType.APPLICATION_JSON)
339 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
340 .header(Constants.USER_ID_HEADER, adminUser.getUserId())
343 List<String> dto = response.readEntity(new GenericType<List<String>>() {
345 assertThat(dto).containsExactlyInAnyOrder(REF_1, REF_2, REF_3, REF_4, REF_5);
346 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
350 void testDeleteExternalRefForExistingComponentInstance() {
351 String deletePath = String
352 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid,
353 COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
354 String getPath = String
355 .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid,
356 VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
357 Response response = target()
359 .request(MediaType.APPLICATION_JSON)
360 .accept(MediaType.APPLICATION_JSON)
361 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
362 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
365 //Verify that the deleted reference is returned in body
366 ExternalRefDTO dto = response.readEntity(ExternalRefDTO.class);
367 assertThat(dto.getReferenceUUID()).isEqualTo(REF_5);
368 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
370 //Check that GET will NOT include the deleted reference
374 .accept(MediaType.APPLICATION_JSON)
375 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
376 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
379 List<String> getResponse = response.readEntity(new GenericType<List<String>>() {
381 assertThat(getResponse).containsExactlyInAnyOrder(REF_1, REF_2, REF_3);
382 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
386 void testUpdateExternalRefForExistingComponentInstance() {
387 String updatePath = String
388 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid,
389 COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
390 String getPath = String
391 .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid,
392 VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
393 Response response = target()
395 .request(MediaType.APPLICATION_JSON)
396 .accept(MediaType.APPLICATION_JSON)
397 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
398 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
399 .put(Entity.json(new ExternalRefDTO(REF_4)));
401 //Verify that the updated reference is returned in body
402 ExternalRefDTO putResponseBody = response.readEntity(ExternalRefDTO.class);
403 assertThat(putResponseBody.getReferenceUUID()).isEqualTo(REF_4);
404 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
406 //Check that GET will include the updated reference
410 .accept(MediaType.APPLICATION_JSON)
411 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
412 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
415 List<String> dto = response.readEntity(new GenericType<List<String>>() {
417 assertThat(dto).containsExactlyInAnyOrder(REF_1, REF_2, REF_3, REF_4);
418 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
425 void testAddExternalRefForNonExistingAssetId() {
427 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", "non-existing-uuid",
428 COMPONENT_ID, MONITORING_OBJECT_TYPE);
429 Response response = target()
431 .request(MediaType.APPLICATION_JSON)
432 .accept(MediaType.APPLICATION_JSON)
433 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
434 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
435 .post(Entity.json(new ExternalRefDTO(REF_4)));
437 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
441 void testAddExternalRefForNonExistingCompInstId() {
443 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid,
444 "FAKE_COM_ID", MONITORING_OBJECT_TYPE);
445 Response response = target()
447 .request(MediaType.APPLICATION_JSON)
448 .accept(MediaType.APPLICATION_JSON)
449 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
450 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
451 .post(Entity.json(new ExternalRefDTO(REF_4)));
453 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
457 void testAddExistingExternalRef() {
459 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid,
460 COMPONENT_ID, MONITORING_OBJECT_TYPE);
461 Response response = target()
463 .request(MediaType.APPLICATION_JSON)
464 .accept(MediaType.APPLICATION_JSON)
465 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
466 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
467 .post(Entity.json(new ExternalRefDTO(REF_1)));
469 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); //Ref not created but still OK is returned
473 void testUpdateExternalRefForNonExistingAssetId() {
474 String updatePath = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s",
475 "nonExistingServiceVertexUuid", COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
477 Response response = target()
479 .request(MediaType.APPLICATION_JSON)
480 .accept(MediaType.APPLICATION_JSON)
481 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
482 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
483 .put(Entity.json(new ExternalRefDTO(REF_4)));
485 //Verify that the 404 is returned
486 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
491 void testUpdateExternalRefForNonExistingObjectIdOrOldRef() {
492 String updatePath = String
493 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid,
494 COMPONENT_ID, "FAKE_OBJ_TYPE", REF_5);
496 Response response = target()
498 .request(MediaType.APPLICATION_JSON)
499 .accept(MediaType.APPLICATION_JSON)
500 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
501 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
502 .put(Entity.json(new ExternalRefDTO(REF_4)));
504 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
508 void testDeleteExternalRefForNonExistingAssetId() {
509 String deletePath = String
510 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", "non-existing-asset",
511 COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
512 Response response = target()
514 .request(MediaType.APPLICATION_JSON)
515 .accept(MediaType.APPLICATION_JSON)
516 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
517 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
520 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
524 void testDeleteExternalRefForNonExistingRef() {
525 String deletePath = String
526 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid,
527 COMPONENT_ID, MONITORING_OBJECT_TYPE, "FAKE_REF");
528 Response response = target()
530 .request(MediaType.APPLICATION_JSON)
531 .accept(MediaType.APPLICATION_JSON)
532 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
535 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
539 void testGetExternalRefsForNonExistingAsset() {
541 .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", "fake-asset-id",
542 VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
544 Response response = target()
547 .accept(MediaType.APPLICATION_JSON)
548 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
549 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
552 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
556 void testGetExternalRefsForNonExistingVersion() {
558 .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid,
559 FAKE_VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
561 Response response = target()
564 .accept(MediaType.APPLICATION_JSON)
565 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
566 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
569 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
573 void testDeleteExternalRefsForExistingComponentInstanceWithUnauthorizedUser() {
575 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid,
576 COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
578 Response response = target()
581 .accept(MediaType.APPLICATION_JSON)
582 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
583 .header(Constants.USER_ID_HEADER, otherUser.getUserId())
586 assertThat(response.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED.value());
590 void testDeleteExternalRefForUserWhichIsNotCurrentUpdater() {
591 String deletePath = String
592 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid,
593 COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
594 Response response = target()
596 .request(MediaType.APPLICATION_JSON)
597 .accept(MediaType.APPLICATION_JSON)
598 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
599 .header(Constants.USER_ID_HEADER, otherDesignerUser.getUserId())
602 assertThat(response.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED.value());
606 protected Application configure() {
607 ApplicationContext context = new AnnotationConfigApplicationContext(TestSpringConfig.class);
608 forceSet(TestProperties.CONTAINER_PORT, "0");
609 return new ResourceConfig(ExternalRefsServlet.class)
610 .register(DefaultExceptionMapper.class)
611 .register(ComponentExceptionMapper.class)
612 .register(StorageExceptionMapper.class)
613 .property("contextConfig", context);
617 @PropertySource("classpath:dao.properties")
618 static class TestSpringConfig {
620 private GraphVertex serviceVertex;
621 private GraphVertex resourceVertex;
622 private ExternalReferencesOperation externalReferenceOperation;
623 private HealingJanusGraphDao janusGraphDao;
626 ExternalRefsServlet externalRefsServlet() {
627 return new ExternalRefsServlet(componentUtils, externalRefsBusinessLogic());
631 OperationUtils operationUtils() {
632 return new OperationUtils(janusGraphDao());
636 ComponentExceptionMapper componentExceptionMapper() {
637 return new ComponentExceptionMapper(componentUtils);
641 StorageExceptionMapper storageExceptionMapper() {
642 return new StorageExceptionMapper(componentUtils);
646 DefaultExceptionMapper defaultExceptionMapper() {
647 return new DefaultExceptionMapper();
651 ExternalRefsBusinessLogic externalRefsBusinessLogic() {
652 return new ExternalRefsBusinessLogic(externalReferencesOperation(), toscaOperationFacade(),
653 accessValidations(), componentLocker());
657 AccessValidations accessValidations() {
658 return accessValidationsMock;
662 ExternalReferencesOperation externalReferencesOperation() {
663 this.externalReferenceOperation = new ExternalReferencesOperation(janusGraphDao(), nodeTypeOpertaion(),
664 topologyTemplateOperation(), idMapper());
665 this.externalReferenceOperation.setHealingPipelineDao(healingPipelineDao());
666 GraphTestUtils.clearGraph(janusGraphDao);
668 return this.externalReferenceOperation;
672 ToscaOperationFacade toscaOperationFacade() {
673 return toscaOperationFacadeMock;
677 IdMapper idMapper() {
678 when(idMapper.mapComponentNameToUniqueId(eq(COMPONENT_ID), any(GraphVertex.class))).thenReturn(COMPONENT_ID);
679 when(idMapper.mapUniqueIdToComponentNameTo(eq(COMPONENT_ID), any(GraphVertex.class))).thenReturn(COMPONENT_ID);
680 when(idMapper.mapComponentNameToUniqueId(eq(FAKE_COMPONENT_ID), any(GraphVertex.class))).thenReturn(null);
685 TopologyTemplateOperation topologyTemplateOperation() {
686 return new TopologyTemplateOperation();
690 ArchiveOperation archiveOperation() {
691 return new ArchiveOperation(janusGraphDao(), graphLockOperation());
695 IGraphLockOperation graphLockOperation() {
696 return graphLockOperation;
700 NodeTypeOperation nodeTypeOpertaion() {
701 return new NodeTypeOperation(null);
705 NodeTemplateOperation nodeTemplateOperation() {
706 return new NodeTemplateOperation();
710 GroupsOperation groupsOperation() {
711 return new GroupsOperation();
715 HealingJanusGraphDao janusGraphDao() {
716 this.janusGraphDao = new HealingJanusGraphDao(healingPipelineDao(), janusGraphClient());
717 return janusGraphDao;
721 JanusGraphClient janusGraphClient() {
722 return new JanusGraphClient(janusGraphClientStrategy());
726 JanusGraphClientStrategy janusGraphClientStrategy() {
727 return new DAOJanusGraphStrategy();
731 CategoryOperation categoryOperation() {
732 return new CategoryOperation();
736 ComponentLocker componentLocker() {
737 return componentLocker;
741 JanusGraphGenericDao janusGraphGenericDao() {
742 return janusGraphGenericDao;
746 ModelOperation modelOperation() {
747 return modelOperation;
751 ModelElementOperation modelElementOperation() {
752 return modelElementOperation;
755 @Bean("healingPipelineDao")
756 HealingPipelineDao healingPipelineDao() {
757 HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
758 healingPipelineDao.setHealVersion(1);
759 healingPipelineDao.initHealVersion();
760 return healingPipelineDao;
764 ContainerInstanceTypesData containerInstanceTypesData() {
765 return new ContainerInstanceTypesData();
768 private void initGraphForTest() {
769 resourceVertex = GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VF);
770 resourceVertexUuid = resourceVertex.getUniqueId();
772 //create a service and add ref
773 serviceVertex = GraphTestUtils.createServiceVertex(janusGraphDao, new HashMap<>());
774 serviceVertexUuid = this.serviceVertex.getUniqueId();
776 //monitoring references
777 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_1);
778 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_2);
779 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_3);
780 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
782 //workflow references
783 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE, REF_6);
785 final JanusGraphOperationStatus commit = this.janusGraphDao.commit();
786 assertThat(commit).isEqualTo(JanusGraphOperationStatus.OK);