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.setHeatArtifactDeploymentTimeout(heatDeploymentArtifactTimeout);
195 configurationManager.setConfiguration(configuration);
196 ExternalConfiguration.setAppName("catalog-be");
200 public void before() throws Exception {
203 when(resourceComponentMock.getVersion()).thenReturn(VERSION);
204 when(resourceComponentMock.getUniqueId()).thenReturn(resourceVertexUuid);
206 when(serviceComponentMock.getVersion()).thenReturn(VERSION);
207 when(serviceComponentMock.getUniqueId()).thenReturn(serviceVertexUuid);
209 List<Component> listComponents = new LinkedList<>();
210 listComponents.add(serviceComponentMock);
212 when(toscaOperationFacadeMock.getComponentListByUuid(eq(serviceVertexUuid), any())).thenReturn(Either.left(listComponents));
213 when(toscaOperationFacadeMock.getComponentByUuidAndVersion(serviceVertexUuid, VERSION)).thenReturn(Either.left(serviceComponentMock));
214 when(toscaOperationFacadeMock.getComponentByUuidAndVersion(resourceVertexUuid, VERSION)).thenReturn(Either.left(resourceComponentMock));
215 when(toscaOperationFacadeMock.getLatestComponentByUuid(eq(serviceVertexUuid), any())).thenReturn(Either.left(listComponents.get(0)));
216 when(toscaOperationFacadeMock.getLatestComponentByUuid(eq(resourceVertexUuid), any())).thenReturn(Either.left(resourceComponentMock));
220 void after() throws Exception {
225 void testGetExternalRefsForExistingComponentInstance() {
227 .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid,
228 VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
230 Response response = target()
233 .accept(MediaType.APPLICATION_JSON)
234 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
235 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
238 List<String> dto = response.readEntity(new GenericType<List<String>>() {
240 assertThat(dto).containsExactly(REF_1, REF_2, REF_3, REF_5);
241 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
245 void testGetExternalRefsForAsset() {
247 .format("/v1/catalog/services/%s/version/%s/externalReferences/%s", serviceVertexUuid, VERSION,
248 MONITORING_OBJECT_TYPE);
250 Response response = target()
253 .accept(MediaType.APPLICATION_JSON)
254 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
255 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
258 Map<String, List<String>> dtoMap = response.readEntity(new GenericType<HashMap<String, List<String>>>() {
260 assertThat(dtoMap.get(COMPONENT_ID)).containsExactly(REF_1, REF_2, REF_3, REF_5);
261 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
265 void testGetExternalRefsForAssetWithMissingEcompHeader() {
267 .format("/v1/catalog/services/%s/version/%s/externalReferences/%s", serviceVertexUuid, VERSION,
268 MONITORING_OBJECT_TYPE);
270 //No X-Ecomp-Instance-ID header
271 Response response = target()
274 .accept(MediaType.APPLICATION_JSON)
275 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
278 assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
282 void testAddExternalRefForResource() {
284 .format("/v1/catalog/resources/%s/resourceInstances/%s/externalReferences/%s", resourceVertexUuid,
285 COMPONENT_ID, MONITORING_OBJECT_TYPE);
286 String getPath = String
287 .format("/v1/catalog/resources/%s/version/%s/externalReferences/%s", resourceVertexUuid, VERSION,
288 MONITORING_OBJECT_TYPE);
290 Response response = target()
292 .request(MediaType.APPLICATION_JSON)
293 .accept(MediaType.APPLICATION_JSON)
294 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
295 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
296 .post(Entity.json(new ExternalRefDTO(REF_1)));
298 assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value());
300 //Check that GET will include the new reference
304 .accept(MediaType.APPLICATION_JSON)
305 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
306 .header(Constants.USER_ID_HEADER, adminUser.getUserId())
309 Map<String, List<String>> dto = response.readEntity(new GenericType<Map<String, List<String>>>() {
311 assertThat(dto.get(COMPONENT_ID)).containsExactlyInAnyOrder(REF_1);
312 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
316 void testAddExternalRefForExistingComponentInstance() {
318 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid,
319 COMPONENT_ID, MONITORING_OBJECT_TYPE);
320 String getPath = String
321 .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid,
322 VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
323 Response response = target()
325 .request(MediaType.APPLICATION_JSON)
326 .accept(MediaType.APPLICATION_JSON)
327 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
328 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
329 .post(Entity.json(new ExternalRefDTO(REF_4)));
331 assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value());
333 //Check that GET will include the new reference
337 .accept(MediaType.APPLICATION_JSON)
338 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
339 .header(Constants.USER_ID_HEADER, adminUser.getUserId())
342 List<String> dto = response.readEntity(new GenericType<List<String>>() {
344 assertThat(dto).containsExactlyInAnyOrder(REF_1, REF_2, REF_3, REF_4, REF_5);
345 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
349 void testDeleteExternalRefForExistingComponentInstance() {
350 String deletePath = String
351 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid,
352 COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
353 String getPath = String
354 .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid,
355 VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
356 Response response = target()
358 .request(MediaType.APPLICATION_JSON)
359 .accept(MediaType.APPLICATION_JSON)
360 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
361 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
364 //Verify that the deleted reference is returned in body
365 ExternalRefDTO dto = response.readEntity(ExternalRefDTO.class);
366 assertThat(dto.getReferenceUUID()).isEqualTo(REF_5);
367 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
369 //Check that GET will NOT include the deleted reference
373 .accept(MediaType.APPLICATION_JSON)
374 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
375 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
378 List<String> getResponse = response.readEntity(new GenericType<List<String>>() {
380 assertThat(getResponse).containsExactlyInAnyOrder(REF_1, REF_2, REF_3);
381 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
385 void testUpdateExternalRefForExistingComponentInstance() {
386 String updatePath = String
387 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid,
388 COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
389 String getPath = String
390 .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid,
391 VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
392 Response response = target()
394 .request(MediaType.APPLICATION_JSON)
395 .accept(MediaType.APPLICATION_JSON)
396 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
397 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
398 .put(Entity.json(new ExternalRefDTO(REF_4)));
400 //Verify that the updated reference is returned in body
401 ExternalRefDTO putResponseBody = response.readEntity(ExternalRefDTO.class);
402 assertThat(putResponseBody.getReferenceUUID()).isEqualTo(REF_4);
403 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
405 //Check that GET will include the updated reference
409 .accept(MediaType.APPLICATION_JSON)
410 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
411 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
414 List<String> dto = response.readEntity(new GenericType<List<String>>() {
416 assertThat(dto).containsExactlyInAnyOrder(REF_1, REF_2, REF_3, REF_4);
417 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
424 void testAddExternalRefForNonExistingAssetId() {
426 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", "non-existing-uuid",
427 COMPONENT_ID, MONITORING_OBJECT_TYPE);
428 Response response = target()
430 .request(MediaType.APPLICATION_JSON)
431 .accept(MediaType.APPLICATION_JSON)
432 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
433 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
434 .post(Entity.json(new ExternalRefDTO(REF_4)));
436 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
440 void testAddExternalRefForNonExistingCompInstId() {
442 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid,
443 "FAKE_COM_ID", MONITORING_OBJECT_TYPE);
444 Response response = target()
446 .request(MediaType.APPLICATION_JSON)
447 .accept(MediaType.APPLICATION_JSON)
448 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
449 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
450 .post(Entity.json(new ExternalRefDTO(REF_4)));
452 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
456 void testAddExistingExternalRef() {
458 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid,
459 COMPONENT_ID, MONITORING_OBJECT_TYPE);
460 Response response = target()
462 .request(MediaType.APPLICATION_JSON)
463 .accept(MediaType.APPLICATION_JSON)
464 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
465 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
466 .post(Entity.json(new ExternalRefDTO(REF_1)));
468 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); //Ref not created but still OK is returned
472 void testUpdateExternalRefForNonExistingAssetId() {
473 String updatePath = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s",
474 "nonExistingServiceVertexUuid", COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
476 Response response = target()
478 .request(MediaType.APPLICATION_JSON)
479 .accept(MediaType.APPLICATION_JSON)
480 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
481 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
482 .put(Entity.json(new ExternalRefDTO(REF_4)));
484 //Verify that the 404 is returned
485 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
490 void testUpdateExternalRefForNonExistingObjectIdOrOldRef() {
491 String updatePath = String
492 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid,
493 COMPONENT_ID, "FAKE_OBJ_TYPE", REF_5);
495 Response response = target()
497 .request(MediaType.APPLICATION_JSON)
498 .accept(MediaType.APPLICATION_JSON)
499 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
500 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
501 .put(Entity.json(new ExternalRefDTO(REF_4)));
503 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
507 void testDeleteExternalRefForNonExistingAssetId() {
508 String deletePath = String
509 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", "non-existing-asset",
510 COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
511 Response response = target()
513 .request(MediaType.APPLICATION_JSON)
514 .accept(MediaType.APPLICATION_JSON)
515 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
516 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
519 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
523 void testDeleteExternalRefForNonExistingRef() {
524 String deletePath = String
525 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid,
526 COMPONENT_ID, MONITORING_OBJECT_TYPE, "FAKE_REF");
527 Response response = target()
529 .request(MediaType.APPLICATION_JSON)
530 .accept(MediaType.APPLICATION_JSON)
531 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
534 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
538 void testGetExternalRefsForNonExistingAsset() {
540 .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", "fake-asset-id",
541 VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
543 Response response = target()
546 .accept(MediaType.APPLICATION_JSON)
547 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
548 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
551 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
555 void testGetExternalRefsForNonExistingVersion() {
557 .format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid,
558 FAKE_VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
560 Response response = target()
563 .accept(MediaType.APPLICATION_JSON)
564 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
565 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
568 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
572 void testDeleteExternalRefsForExistingComponentInstanceWithUnauthorizedUser() {
574 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid,
575 COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
577 Response response = target()
580 .accept(MediaType.APPLICATION_JSON)
581 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
582 .header(Constants.USER_ID_HEADER, otherUser.getUserId())
585 assertThat(response.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED.value());
589 void testDeleteExternalRefForUserWhichIsNotCurrentUpdater() {
590 String deletePath = String
591 .format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid,
592 COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
593 Response response = target()
595 .request(MediaType.APPLICATION_JSON)
596 .accept(MediaType.APPLICATION_JSON)
597 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
598 .header(Constants.USER_ID_HEADER, otherDesignerUser.getUserId())
601 assertThat(response.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED.value());
605 protected Application configure() {
606 ApplicationContext context = new AnnotationConfigApplicationContext(TestSpringConfig.class);
607 forceSet(TestProperties.CONTAINER_PORT, "0");
608 return new ResourceConfig(ExternalRefsServlet.class)
609 .register(DefaultExceptionMapper.class)
610 .register(ComponentExceptionMapper.class)
611 .register(StorageExceptionMapper.class)
612 .property("contextConfig", context);
616 @PropertySource("classpath:dao.properties")
617 static class TestSpringConfig {
619 private GraphVertex serviceVertex;
620 private GraphVertex resourceVertex;
621 private ExternalReferencesOperation externalReferenceOperation;
622 private HealingJanusGraphDao janusGraphDao;
625 ExternalRefsServlet externalRefsServlet() {
626 return new ExternalRefsServlet(componentUtils, externalRefsBusinessLogic());
630 OperationUtils operationUtils() {
631 return new OperationUtils(janusGraphDao());
635 ComponentExceptionMapper componentExceptionMapper() {
636 return new ComponentExceptionMapper(componentUtils);
640 StorageExceptionMapper storageExceptionMapper() {
641 return new StorageExceptionMapper(componentUtils);
645 DefaultExceptionMapper defaultExceptionMapper() {
646 return new DefaultExceptionMapper();
650 ExternalRefsBusinessLogic externalRefsBusinessLogic() {
651 return new ExternalRefsBusinessLogic(externalReferencesOperation(), toscaOperationFacade(),
652 accessValidations(), componentLocker());
656 AccessValidations accessValidations() {
657 return accessValidationsMock;
661 ExternalReferencesOperation externalReferencesOperation() {
662 this.externalReferenceOperation = new ExternalReferencesOperation(janusGraphDao(), nodeTypeOpertaion(),
663 topologyTemplateOperation(), idMapper());
664 this.externalReferenceOperation.setHealingPipelineDao(healingPipelineDao());
665 GraphTestUtils.clearGraph(janusGraphDao);
667 return this.externalReferenceOperation;
671 ToscaOperationFacade toscaOperationFacade() {
672 return toscaOperationFacadeMock;
676 IdMapper idMapper() {
677 when(idMapper.mapComponentNameToUniqueId(eq(COMPONENT_ID), any(GraphVertex.class))).thenReturn(COMPONENT_ID);
678 when(idMapper.mapUniqueIdToComponentNameTo(eq(COMPONENT_ID), any(GraphVertex.class))).thenReturn(COMPONENT_ID);
679 when(idMapper.mapComponentNameToUniqueId(eq(FAKE_COMPONENT_ID), any(GraphVertex.class))).thenReturn(null);
684 TopologyTemplateOperation topologyTemplateOperation() {
685 return new TopologyTemplateOperation();
689 ArchiveOperation archiveOperation() {
690 return new ArchiveOperation(janusGraphDao(), graphLockOperation());
694 IGraphLockOperation graphLockOperation() {
695 return graphLockOperation;
699 NodeTypeOperation nodeTypeOpertaion() {
700 return new NodeTypeOperation(null);
704 NodeTemplateOperation nodeTemplateOperation() {
705 return new NodeTemplateOperation();
709 GroupsOperation groupsOperation() {
710 return new GroupsOperation();
714 HealingJanusGraphDao janusGraphDao() {
715 this.janusGraphDao = new HealingJanusGraphDao(healingPipelineDao(), janusGraphClient());
716 return janusGraphDao;
720 JanusGraphClient janusGraphClient() {
721 return new JanusGraphClient(janusGraphClientStrategy());
725 JanusGraphClientStrategy janusGraphClientStrategy() {
726 return new DAOJanusGraphStrategy();
730 CategoryOperation categoryOperation() {
731 return new CategoryOperation();
735 ComponentLocker componentLocker() {
736 return componentLocker;
740 JanusGraphGenericDao janusGraphGenericDao() {
741 return janusGraphGenericDao;
745 ModelOperation modelOperation() {
746 return modelOperation;
750 ModelElementOperation modelElementOperation() {
751 return modelElementOperation;
754 @Bean("healingPipelineDao")
755 HealingPipelineDao healingPipelineDao() {
756 HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
757 healingPipelineDao.setHealVersion(1);
758 healingPipelineDao.initHealVersion();
759 return healingPipelineDao;
763 ContainerInstanceTypesData containerInstanceTypesData() {
764 return new ContainerInstanceTypesData();
767 private void initGraphForTest() {
768 resourceVertex = GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VF);
769 resourceVertexUuid = resourceVertex.getUniqueId();
771 //create a service and add ref
772 serviceVertex = GraphTestUtils.createServiceVertex(janusGraphDao, new HashMap<>());
773 serviceVertexUuid = this.serviceVertex.getUniqueId();
775 //monitoring references
776 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_1);
777 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_2);
778 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_3);
779 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
781 //workflow references
782 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE, REF_6);
784 final JanusGraphOperationStatus commit = this.janusGraphDao.commit();
785 assertThat(commit).isEqualTo(JanusGraphOperationStatus.OK);