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 fj.data.Either;
26 import org.glassfish.jersey.server.ResourceConfig;
27 import org.glassfish.jersey.test.JerseyTest;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.mockito.Mockito;
32 import org.openecomp.sdc.be.components.impl.ComponentLocker;
33 import org.openecomp.sdc.be.components.impl.ExternalRefsBusinessLogic;
34 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
35 import org.openecomp.sdc.be.components.path.utils.GraphTestUtils;
36 import org.openecomp.sdc.be.components.validation.AccessValidations;
37 import org.openecomp.sdc.be.config.ConfigurationManager;
38 import org.openecomp.sdc.be.dao.DAOJanusGraphStrategy;
39 import org.openecomp.sdc.be.dao.JanusGraphClientStrategy;
40 import org.openecomp.sdc.be.dao.api.ActionStatus;
41 import org.openecomp.sdc.be.dao.impl.HealingPipelineDao;
42 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
43 import org.openecomp.sdc.be.dao.jsongraph.HealingJanusGraphDao;
44 import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphGenericDao;
45 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
46 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphClient;
47 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
48 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
49 import org.openecomp.sdc.be.dto.ExternalRefDTO;
50 import org.openecomp.sdc.be.impl.ComponentsUtils;
51 import org.openecomp.sdc.be.impl.ServletUtils;
52 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
53 import org.openecomp.sdc.be.model.Component;
54 import org.openecomp.sdc.be.model.User;
55 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.*;
56 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.IdMapper;
57 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
58 import org.openecomp.sdc.be.model.operations.impl.OperationUtils;
59 import org.openecomp.sdc.be.servlets.exception.ComponentExceptionMapper;
60 import org.openecomp.sdc.be.servlets.exception.DefaultExceptionMapper;
61 import org.openecomp.sdc.be.servlets.exception.StorageExceptionMapper;
62 import org.openecomp.sdc.be.user.Role;
63 import org.openecomp.sdc.be.user.UserBusinessLogic;
64 import org.openecomp.sdc.common.api.ConfigurationSource;
65 import org.openecomp.sdc.common.api.Constants;
66 import org.openecomp.sdc.common.impl.ExternalConfiguration;
67 import org.openecomp.sdc.common.impl.FSConfigurationSource;
68 import org.openecomp.sdc.exception.ResponseFormat;
69 import org.springframework.context.ApplicationContext;
70 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
71 import org.springframework.context.annotation.Bean;
72 import org.springframework.context.annotation.Configuration;
73 import org.springframework.context.annotation.PropertySource;
74 import org.springframework.http.HttpStatus;
75 import org.springframework.web.context.WebApplicationContext;
77 import javax.servlet.ServletContext;
78 import javax.servlet.http.HttpServletRequest;
79 import javax.ws.rs.client.Entity;
80 import javax.ws.rs.core.Application;
81 import javax.ws.rs.core.GenericType;
82 import javax.ws.rs.core.MediaType;
83 import javax.ws.rs.core.Response;
84 import java.util.HashMap;
85 import java.util.LinkedList;
86 import java.util.List;
89 import static org.assertj.core.api.Assertions.assertThat;
90 import static org.mockito.ArgumentMatchers.any;
91 import static org.mockito.ArgumentMatchers.eq;
92 import static org.mockito.Mockito.doThrow;
93 import static org.mockito.Mockito.when;
95 public class ExternalRefServletTest extends JerseyTest {
97 private static boolean setupDone = false;
98 private static String serviceVertexUuid;
99 private static String resourceVertexUuid;
100 private static final ServletContext servletContext = Mockito.mock(ServletContext.class);
101 public static final WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
102 private static final WebApplicationContext webApplicationContext = Mockito.mock(WebApplicationContext.class);
103 private static final ServletUtils servletUtils = Mockito.mock(ServletUtils.class);
104 private static final UserBusinessLogic userAdmin = Mockito.mock(UserBusinessLogic.class);
105 private static final ComponentsUtils componentUtils = Mockito.mock(ComponentsUtils.class);
106 private static final ResponseFormat responseFormat = Mockito.mock(ResponseFormat.class);
107 private static final ResponseFormat notFoundResponseFormat = Mockito.mock(ResponseFormat.class);
108 private static final ResponseFormat badRequestResponseFormat = Mockito.mock(ResponseFormat.class);
109 private static final ToscaOperationFacade toscaOperationFacadeMock = Mockito.mock(ToscaOperationFacade.class);
110 private static final AccessValidations accessValidationsMock = Mockito.mock(AccessValidations.class);
111 private static final ComponentLocker componentLocker = Mockito.mock(ComponentLocker.class);
112 private static final HealingJanusGraphGenericDao janusGraphGenericDao = Mockito.mock(HealingJanusGraphGenericDao.class);
113 private static final IGraphLockOperation graphLockOperation = Mockito.mock(IGraphLockOperation.class);
115 private static final String COMPONENT_ID = "ci-MyComponentName";
117 private static final String FAKE_COMPONENT_ID = "ci-MyFAKEComponentName";
118 private static final String MONITORING_OBJECT_TYPE = "monitoring";
119 private static final String WORKFLOW_OBJECT_TYPE = "workflow";
120 private static final String VERSION = "0.1";
121 private static final String FAKE_VERSION = "0.5";
122 private static final String REF_1 = "ref1";
123 private static final String REF_2 = "ref2";
124 private static final String REF_3 = "ref3";
125 private static final String REF_4 = "ref4";
126 private static final String REF_5 = "ref5";
128 private static final String REF_6 = "ref6";
131 @PropertySource("classpath:dao.properties")
132 static class TestSpringConfig {
134 private GraphVertex serviceVertex;
135 private GraphVertex resourceVertex;
136 private ExternalReferencesOperation externalReferenceOperation;
137 private HealingJanusGraphDao janusGraphDao;
138 private OperationUtils operationUtils;
141 ExternalRefsServlet externalRefsServlet() {
142 return new ExternalRefsServlet(externalRefsBusinessLogic(), componentUtils);
146 OperationUtils operationUtils() {return new OperationUtils(janusGraphDao());}
149 ComponentExceptionMapper componentExceptionMapper() {
150 return new ComponentExceptionMapper(componentUtils);
154 StorageExceptionMapper storageExceptionMapper() {
155 return new StorageExceptionMapper(componentUtils);
159 DefaultExceptionMapper defaultExceptionMapper() {
160 return new DefaultExceptionMapper();
164 ExternalRefsBusinessLogic externalRefsBusinessLogic() {
165 return new ExternalRefsBusinessLogic(externalReferencesOperation(), toscaOperationFacade(), accessValidations(), componentLocker());
169 AccessValidations accessValidations() {
170 return accessValidationsMock;
174 ExternalReferencesOperation externalReferencesOperation() {
175 this.externalReferenceOperation = new ExternalReferencesOperation(janusGraphDao(), nodeTypeOpertaion(), topologyTemplateOperation(), idMapper());
176 this.externalReferenceOperation.setHealingPipelineDao(healingPipelineDao());
177 GraphTestUtils.clearGraph(janusGraphDao);
179 return this.externalReferenceOperation;
183 ToscaOperationFacade toscaOperationFacade() {
184 return toscaOperationFacadeMock;
188 IdMapper idMapper() {
189 IdMapper idMapper = Mockito.mock(IdMapper.class);
190 when(idMapper.mapComponentNameToUniqueId(eq(COMPONENT_ID), any(GraphVertex.class))).thenReturn(COMPONENT_ID);
191 when(idMapper.mapUniqueIdToComponentNameTo(eq(COMPONENT_ID), any(GraphVertex.class))).thenReturn(COMPONENT_ID);
192 when(idMapper.mapComponentNameToUniqueId(eq(FAKE_COMPONENT_ID), any(GraphVertex.class))).thenReturn(null);
197 TopologyTemplateOperation topologyTemplateOperation() {
198 return new TopologyTemplateOperation();
202 ArchiveOperation archiveOperation() {
203 return new ArchiveOperation(janusGraphDao(), graphLockOperation());
207 IGraphLockOperation graphLockOperation() {
208 return graphLockOperation;
212 NodeTypeOperation nodeTypeOpertaion() {
213 return new NodeTypeOperation(null);
217 NodeTemplateOperation nodeTemplateOperation() {
218 return new NodeTemplateOperation();
222 GroupsOperation groupsOperation() {
223 return new GroupsOperation();
227 HealingJanusGraphDao janusGraphDao() {
228 this.janusGraphDao = new HealingJanusGraphDao(healingPipelineDao(),janusGraphClient());
229 return janusGraphDao;
233 JanusGraphClient janusGraphClient() {
234 return new JanusGraphClient(janusGraphClientStrategy());
238 JanusGraphClientStrategy janusGraphClientStrategy() {
239 return new DAOJanusGraphStrategy();
243 CategoryOperation categoryOperation() {
244 return new CategoryOperation();
248 ComponentLocker componentLocker() {
249 return componentLocker;
253 JanusGraphGenericDao janusGraphGenericDao() {
254 return janusGraphGenericDao;
257 @Bean("healingPipelineDao")
258 HealingPipelineDao healingPipelineDao() {
259 HealingPipelineDao healingPipelineDao = new HealingPipelineDao() ;
260 healingPipelineDao.setHealVersion(1);
261 healingPipelineDao.initHealVersion();
262 return healingPipelineDao;
266 private void initGraphForTest() {
269 resourceVertex = GraphTestUtils.createResourceVertex(janusGraphDao, new HashMap<>(), ResourceTypeEnum.VF);
270 resourceVertexUuid = resourceVertex.getUniqueId();
272 //create a service and add ref
273 serviceVertex = GraphTestUtils.createServiceVertex(janusGraphDao, new HashMap<>());
274 serviceVertexUuid = this.serviceVertex.getUniqueId();
276 //monitoring references
277 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_1);
278 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_2);
279 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_3);
280 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
282 //workflow references
283 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE, REF_6);
285 final JanusGraphOperationStatus commit = this.janusGraphDao.commit();
286 assertThat(commit).isEqualTo(JanusGraphOperationStatus.OK);
293 public static final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
296 private static User adminUser = new User("admin", "admin", "admin", "admin@email.com", Role.ADMIN.name(), System.currentTimeMillis());
297 private static User designerUser = new User("designer", "designer", "designer", "designer@email.com", Role.DESIGNER.name(), System
298 .currentTimeMillis());
299 private static User otherDesignerUser = new User("otherDesigner", "otherDesigner", "otherDesigner", "otherDesigner@email.com", Role.DESIGNER
300 .name(), System.currentTimeMillis());
301 private static User otherUser = new User("other", "other", "other", "other@email.com", Role.OPS.name(), System.currentTimeMillis());
305 public static void setup() {
307 //Needed for User Authorization
308 //========================================================================================================================
309 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
310 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
311 when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils);
312 when(servletUtils.getUserAdmin()).thenReturn(userAdmin);
313 when(servletUtils.getComponentsUtils()).thenReturn(componentUtils);
314 when(componentUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)).thenReturn(responseFormat);
315 when(responseFormat.getStatus()).thenReturn(HttpStatus.UNAUTHORIZED.value());
317 ByResponseFormatComponentException ce = Mockito.mock(ByResponseFormatComponentException.class);
318 String[] params = {otherDesignerUser.getUserId()};
319 when(ce.getResponseFormat()).thenReturn(responseFormat);
320 doThrow(ce).when(accessValidationsMock)
321 .validateUserCanWorkOnComponent(any(), eq(otherDesignerUser.getUserId()), any());
322 doThrow(ce).when(accessValidationsMock)
323 .validateUserCanWorkOnComponent(any(), eq(otherUser.getUserId()), any());
325 //Needed for error configuration
326 when(notFoundResponseFormat.getStatus()).thenReturn(HttpStatus.NOT_FOUND.value());
327 when(badRequestResponseFormat.getStatus()).thenReturn(HttpStatus.BAD_REQUEST.value());
328 when(componentUtils.getResponseFormat(eq(ActionStatus.RESOURCE_NOT_FOUND), (String[]) any())).thenReturn(notFoundResponseFormat);
329 when(componentUtils.getResponseFormat(eq(ActionStatus.COMPONENT_VERSION_NOT_FOUND), (String[]) any())).thenReturn(notFoundResponseFormat);
330 when(componentUtils.getResponseFormat(eq(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND), (String[]) any())).thenReturn(notFoundResponseFormat);
331 when(componentUtils.getResponseFormat(eq(ActionStatus.EXT_REF_NOT_FOUND), (String[]) any())).thenReturn(notFoundResponseFormat);
332 when(componentUtils.getResponseFormat(eq(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID), (String[]) any())).thenReturn(badRequestResponseFormat);
335 Either<User, ActionStatus> adminEither = Either.left(adminUser);
336 Either<User, ActionStatus> designerEither = Either.left(designerUser);
337 Either<User, ActionStatus> otherEither = Either.left(otherUser);
339 when(userAdmin.getUser(adminUser.getUserId(), false)).thenReturn(adminEither);
340 when(userAdmin.getUser(designerUser.getUserId(), false)).thenReturn(designerEither);
341 when(userAdmin.getUser(otherUser.getUserId(), false)).thenReturn(otherEither);
342 //========================================================================================================================
344 String appConfigDir = "src/test/resources/config";
345 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
346 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
348 org.openecomp.sdc.be.config.Configuration configuration = new org.openecomp.sdc.be.config.Configuration();
349 configuration.setJanusGraphInMemoryGraph(true);
351 configurationManager.setConfiguration(configuration);
352 ExternalConfiguration.setAppName("catalog-be");
356 public void before(){
358 Component resourceComponentMock = Mockito.mock(Component.class);
359 when(resourceComponentMock.getVersion()).thenReturn(VERSION);
360 when(resourceComponentMock.getUniqueId()).thenReturn(resourceVertexUuid);
362 Component serviceComponentMock = Mockito.mock(Component.class);
363 when(serviceComponentMock.getVersion()).thenReturn(VERSION);
364 when(serviceComponentMock.getUniqueId()).thenReturn(serviceVertexUuid);
366 List<Component> listComponents = new LinkedList<>();
367 listComponents.add(serviceComponentMock);
369 when(toscaOperationFacadeMock.getComponentListByUuid(eq(serviceVertexUuid), any())).thenReturn(Either.left(listComponents));
370 when(toscaOperationFacadeMock.getComponentByUuidAndVersion(eq(serviceVertexUuid), eq(VERSION))).thenReturn(Either.left(serviceComponentMock));
371 when(toscaOperationFacadeMock.getComponentByUuidAndVersion(eq(resourceVertexUuid), eq(VERSION))).thenReturn(Either.left(resourceComponentMock));
372 when(toscaOperationFacadeMock.getLatestComponentByUuid(eq(serviceVertexUuid), any())).thenReturn(Either.left(listComponents.get(0)));
373 when(toscaOperationFacadeMock.getLatestComponentByUuid(eq(resourceVertexUuid), any())).thenReturn(Either.left(resourceComponentMock));
377 public void testGetExternalRefsForExistingComponentInstance() {
378 String path = String.format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
380 Response response = target()
383 .accept(MediaType.APPLICATION_JSON)
384 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
385 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
388 List<String> dto = response.readEntity(new GenericType<List<String>>() {
390 assertThat(dto).containsExactly(REF_1, REF_2, REF_3, REF_5);
391 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
395 public void testGetExternalRefsForAsset() {
396 String path = String.format("/v1/catalog/services/%s/version/%s/externalReferences/%s", serviceVertexUuid, VERSION, MONITORING_OBJECT_TYPE);
398 Response response = target()
401 .accept(MediaType.APPLICATION_JSON)
402 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
403 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
406 Map<String, List<String>> dtoMap = response.readEntity(new GenericType<HashMap<String, List<String>>>() {
408 assertThat(dtoMap.get(COMPONENT_ID)).containsExactly(REF_1, REF_2, REF_3, REF_5);
409 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
413 public void testGetExternalRefsForAssetWithMissingEcompHeader() {
414 String path = String.format("/v1/catalog/services/%s/version/%s/externalReferences/%s", serviceVertexUuid, VERSION, MONITORING_OBJECT_TYPE);
416 //No X-Ecomp-Instance-ID header
417 Response response = target()
420 .accept(MediaType.APPLICATION_JSON)
421 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
424 assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
429 public void testAddExternalRefForResource(){
430 String path = String.format("/v1/catalog/resources/%s/resourceInstances/%s/externalReferences/%s", resourceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE);
431 String getPath = String.format("/v1/catalog/resources/%s/version/%s/externalReferences/%s", resourceVertexUuid, VERSION, MONITORING_OBJECT_TYPE);
433 Response response = target()
435 .request(MediaType.APPLICATION_JSON)
436 .accept(MediaType.APPLICATION_JSON)
437 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
438 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
439 .post(Entity.json(new ExternalRefDTO(REF_1)));
441 assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value());
443 //Check that GET will include the new reference
447 .accept(MediaType.APPLICATION_JSON)
448 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
449 .header(Constants.USER_ID_HEADER, adminUser.getUserId())
452 Map<String, List<String>> dto = response.readEntity(new GenericType<Map<String, List<String>>>(){});
453 assertThat(dto.get(COMPONENT_ID)).containsExactlyInAnyOrder(REF_1);
454 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
458 public void testAddExternalRefForExistingComponentInstance() {
459 String path = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE);
460 String getPath = String.format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, VERSION, 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_4)));
469 assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value());
471 //Check that GET will include the new reference
475 .accept(MediaType.APPLICATION_JSON)
476 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
477 .header(Constants.USER_ID_HEADER, adminUser.getUserId())
480 List<String> dto = response.readEntity(new GenericType<List<String>>() {
482 assertThat(dto).containsExactlyInAnyOrder(REF_1, REF_2, REF_3, REF_4, REF_5);
483 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
487 public void testDeleteExternalRefForExistingComponentInstance() {
488 String deletePath = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
489 String getPath = String.format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
490 Response response = target()
492 .request(MediaType.APPLICATION_JSON)
493 .accept(MediaType.APPLICATION_JSON)
494 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
495 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
498 //Verify that the deleted reference is returned in body
499 ExternalRefDTO dto = response.readEntity(ExternalRefDTO.class);
500 assertThat(dto.getReferenceUUID()).isEqualTo(REF_5);
501 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
503 //Check that GET will NOT include the deleted reference
507 .accept(MediaType.APPLICATION_JSON)
508 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
509 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
512 List<String> getResponse = response.readEntity(new GenericType<List<String>>() {
514 assertThat(getResponse).containsExactlyInAnyOrder(REF_1, REF_2, REF_3);
515 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
519 public void testUpdateExternalRefForExistingComponentInstance() {
520 String updatePath = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
521 String getPath = String.format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
522 Response response = target()
524 .request(MediaType.APPLICATION_JSON)
525 .accept(MediaType.APPLICATION_JSON)
526 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
527 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
528 .put(Entity.json(new ExternalRefDTO(REF_4)));
530 //Verify that the updated reference is returned in body
531 ExternalRefDTO putResponseBody = response.readEntity(ExternalRefDTO.class);
532 assertThat(putResponseBody.getReferenceUUID()).isEqualTo(REF_4);
533 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
535 //Check that GET will include the updated reference
539 .accept(MediaType.APPLICATION_JSON)
540 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
541 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
545 List<String> dto = response.readEntity(new GenericType<List<String>>() {
547 assertThat(dto).containsExactlyInAnyOrder(REF_1, REF_2, REF_3, REF_4);
548 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
556 public void testAddExternalRefForNonExistingAssetId() {
557 String path = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", "non-existing-uuid", COMPONENT_ID, MONITORING_OBJECT_TYPE);
558 Response response = target()
560 .request(MediaType.APPLICATION_JSON)
561 .accept(MediaType.APPLICATION_JSON)
562 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
563 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
564 .post(Entity.json(new ExternalRefDTO(REF_4)));
566 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
570 public void testAddExternalRefForNonExistingCompInstId() {
571 String path = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, "FAKE_COM_ID", MONITORING_OBJECT_TYPE);
572 Response response = target()
574 .request(MediaType.APPLICATION_JSON)
575 .accept(MediaType.APPLICATION_JSON)
576 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
577 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
578 .post(Entity.json(new ExternalRefDTO(REF_4)));
580 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
584 public void testAddExistingExternalRef() {
585 String path = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE);
586 Response response = target()
588 .request(MediaType.APPLICATION_JSON)
589 .accept(MediaType.APPLICATION_JSON)
590 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
591 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
592 .post(Entity.json(new ExternalRefDTO(REF_1)));
594 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); //Ref not created but still OK is returned
598 public void testUpdateExternalRefForNonExistingAssetId() {
599 String updatePath = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", "nonExistingServiceVertexUuid", COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
601 Response response = target()
603 .request(MediaType.APPLICATION_JSON)
604 .accept(MediaType.APPLICATION_JSON)
605 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
606 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
607 .put(Entity.json(new ExternalRefDTO(REF_4)));
609 //Verify that the 404 is returned
610 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
615 public void testUpdateExternalRefForNonExistingObjectIdOrOldRef() {
616 String updatePath = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, COMPONENT_ID, "FAKE_OBJ_TYPE", REF_5);
618 Response response = target()
620 .request(MediaType.APPLICATION_JSON)
621 .accept(MediaType.APPLICATION_JSON)
622 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
623 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
624 .put(Entity.json(new ExternalRefDTO(REF_4)));
626 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
630 public void testDeleteExternalRefForNonExistingAssetId() {
631 String deletePath = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", "non-existing-asset", COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
632 Response response = target()
634 .request(MediaType.APPLICATION_JSON)
635 .accept(MediaType.APPLICATION_JSON)
636 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
637 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
640 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
644 public void testDeleteExternalRefForNonExistingRef() {
645 String deletePath = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, "FAKE_REF");
646 Response response = target()
648 .request(MediaType.APPLICATION_JSON)
649 .accept(MediaType.APPLICATION_JSON)
650 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
653 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
657 public void testGetExternalRefsForNonExistingAsset() {
658 String path = String.format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", "fake-asset-id", VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
660 Response response = target()
663 .accept(MediaType.APPLICATION_JSON)
664 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
665 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
668 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
672 public void testGetExternalRefsForNonExistingVersion() {
673 String path = String.format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, FAKE_VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
675 Response response = target()
678 .accept(MediaType.APPLICATION_JSON)
679 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
680 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
683 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
687 public void testDeleteExternalRefsForExistingComponentInstanceWithUnauthorizedUser() {
688 String path = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
690 Response response = target()
693 .accept(MediaType.APPLICATION_JSON)
694 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
695 .header(Constants.USER_ID_HEADER, otherUser.getUserId())
698 assertThat(response.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED.value());
702 public void testDeleteExternalRefForUserWhichIsNotCurrentUpdater() {
703 String deletePath = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
704 Response response = target()
706 .request(MediaType.APPLICATION_JSON)
707 .accept(MediaType.APPLICATION_JSON)
708 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId")
709 .header(Constants.USER_ID_HEADER, otherDesignerUser.getUserId())
712 assertThat(response.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED.value());
716 protected Application configure() {
717 ApplicationContext context = new AnnotationConfigApplicationContext(TestSpringConfig.class);
718 return new ResourceConfig(ExternalRefsServlet.class)
719 .register(DefaultExceptionMapper.class)
720 .register(ComponentExceptionMapper.class)
721 .register(StorageExceptionMapper.class)
722 .property("contextConfig", context);