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=========================================================
21 package org.openecomp.sdc.be.externalapi.servlet;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.when;
28 import java.util.HashMap;
29 import java.util.LinkedList;
30 import java.util.List;
33 import javax.servlet.ServletContext;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpSession;
36 import javax.ws.rs.client.Entity;
37 import javax.ws.rs.core.Application;
38 import javax.ws.rs.core.GenericType;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.Response;
42 import org.glassfish.hk2.utilities.binding.AbstractBinder;
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.Before;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 import org.mockito.Mockito;
50 import org.openecomp.sdc.be.components.impl.ExternalRefsBusinessLogic;
51 import org.openecomp.sdc.be.components.path.utils.GraphTestUtils;
52 import org.openecomp.sdc.be.config.ConfigurationManager;
53 import org.openecomp.sdc.be.dao.DAOTitanStrategy;
54 import org.openecomp.sdc.be.dao.TitanClientStrategy;
55 import org.openecomp.sdc.be.dao.api.ActionStatus;
56 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
57 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
58 import org.openecomp.sdc.be.dao.titan.TitanGenericDao;
59 import org.openecomp.sdc.be.dao.titan.TitanGraphClient;
60 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
61 import org.openecomp.sdc.be.dto.ExternalRefDTO;
62 import org.openecomp.sdc.be.impl.ComponentsUtils;
63 import org.openecomp.sdc.be.impl.ServletUtils;
64 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
65 import org.openecomp.sdc.be.model.Component;
66 import org.openecomp.sdc.be.model.User;
67 import org.openecomp.sdc.be.model.jsontitan.operations.CategoryOperation;
68 import org.openecomp.sdc.be.model.jsontitan.operations.ExternalReferencesOperation;
69 import org.openecomp.sdc.be.model.jsontitan.operations.GroupsOperation;
70 import org.openecomp.sdc.be.model.jsontitan.operations.NodeTemplateOperation;
71 import org.openecomp.sdc.be.model.jsontitan.operations.NodeTypeOperation;
72 import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation;
73 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
74 import org.openecomp.sdc.be.model.jsontitan.utils.IdMapper;
75 import org.openecomp.sdc.be.model.operations.api.ICacheMangerOperation;
76 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
77 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
78 import org.openecomp.sdc.be.user.Role;
79 import org.openecomp.sdc.be.user.UserBusinessLogic;
80 import org.openecomp.sdc.common.api.ConfigurationSource;
81 import org.openecomp.sdc.common.api.Constants;
82 import org.openecomp.sdc.common.impl.ExternalConfiguration;
83 import org.openecomp.sdc.common.impl.FSConfigurationSource;
84 import org.openecomp.sdc.exception.ResponseFormat;
85 import org.springframework.context.ApplicationContext;
86 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
87 import org.springframework.context.annotation.Bean;
88 import org.springframework.context.annotation.Configuration;
89 import org.springframework.http.HttpStatus;
90 import org.springframework.web.context.WebApplicationContext;
92 import fj.data.Either;
94 public class ExternalRefServletTest extends JerseyTest {
96 private static ConfigurationManager configurationManager;
97 static String serviceVertexUuid;
98 private static final HttpSession session = Mockito.mock(HttpSession.class);
99 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 GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class);
111 private static final TitanGenericDao titanGenericDao = Mockito.mock(TitanGenericDao.class);
112 private static final ICacheMangerOperation cacheManagerOperation = Mockito.mock(ICacheMangerOperation.class);
114 private static final String COMPONENT_ID = "ci-MyComponentName";
116 private static final String FAKE_COMPONENT_ID = "ci-MyFAKEComponentName";
117 private static final String MONITORING_OBJECT_TYPE = "monitoring";
118 private static final String WORKFLOW_OBJECT_TYPE = "workflow";
119 private static final String VERSION = "0.1";
120 private static final String FAKE_VERSION = "0.5";
121 private static final String REF_1 = "ref1";
122 private static final String REF_2 = "ref2";
123 private static final String REF_3 = "ref3";
124 private static final String REF_4 = "ref4";
125 private static final String REF_5 = "ref5";
127 private static final String REF_6 = "ref6";
130 static class TestSpringConfig {
132 private GraphVertex serviceVertex;
133 private ExternalReferencesOperation externalReferenceOperation;
134 private TitanDao titanDao;
137 ExternalRefsServlet externalRefsServlet(){
138 return new ExternalRefsServlet(externalRefsBusinessLogic(), componentUtils);
142 ExternalRefsBusinessLogic externalRefsBusinessLogic() {
143 return new ExternalRefsBusinessLogic(externalReferencesOperation(), toscaOperationFacade(), graphLockOperation());
147 ExternalReferencesOperation externalReferencesOperation() {
148 this.externalReferenceOperation = new ExternalReferencesOperation(titanDao(), nodeTypeOpertaion(), topologyTemplateOperation(), idMapper());
149 GraphTestUtils.clearGraph(titanDao);
151 return this.externalReferenceOperation;
155 ToscaOperationFacade toscaOperationFacade(){
156 Component componentMock = Mockito.mock(Component.class);
157 when(componentMock.getVersion()).thenReturn(VERSION);
158 when(componentMock.getUniqueId()).thenReturn(serviceVertexUuid);
159 List<Component> listComponents = new LinkedList<Component>();
160 listComponents.add(componentMock);
161 when(toscaOperationFacadeMock.getComponentListByUuid(eq(serviceVertexUuid), any())).thenReturn(Either.left(listComponents));
162 when(toscaOperationFacadeMock.getComponentByUuidAndVersion(eq(serviceVertexUuid), eq(VERSION))).thenReturn(Either.left(componentMock));
163 when(toscaOperationFacadeMock.getLatestComponentByUuid(eq(serviceVertexUuid), any())).thenReturn(Either.left(listComponents.get(0)));
164 return toscaOperationFacadeMock;
168 IdMapper idMapper() {
169 IdMapper idMapper = Mockito.mock(IdMapper.class);
170 when(idMapper.mapComponentNameToUniqueId(eq(COMPONENT_ID), any(GraphVertex.class))).thenReturn(COMPONENT_ID);
171 when(idMapper.mapUniqueIdToComponentNameTo(eq(COMPONENT_ID), any(GraphVertex.class))).thenReturn(COMPONENT_ID);
172 when(idMapper.mapComponentNameToUniqueId(eq(FAKE_COMPONENT_ID), any(GraphVertex.class))).thenReturn(null);
177 TopologyTemplateOperation topologyTemplateOperation() {
178 return new TopologyTemplateOperation();
182 NodeTypeOperation nodeTypeOpertaion() {
183 return new NodeTypeOperation(null);
187 NodeTemplateOperation nodeTemplateOperation(){
188 return new NodeTemplateOperation();
192 GroupsOperation groupsOperation(){
193 return new GroupsOperation();
197 ICacheMangerOperation cacheMangerOperation() { return cacheManagerOperation; }
200 TitanDao titanDao() {
201 this.titanDao = new TitanDao(titanGraphClient());
206 TitanGraphClient titanGraphClient() {
207 return new TitanGraphClient(titanClientStrategy());
211 TitanClientStrategy titanClientStrategy() {
212 return new DAOTitanStrategy();
216 CategoryOperation categoryOperation(){
217 return new CategoryOperation();
221 GraphLockOperation graphLockOperation() { return graphLockOperation; }
224 TitanGenericDao titanGenericDao() { return titanGenericDao; }
226 private void initGraphForTest() {
227 //create a service and add 1 ref
228 serviceVertex = GraphTestUtils.createServiceVertex(titanDao, new HashMap<>());
229 serviceVertexUuid = this.serviceVertex.getUniqueId();
231 //monitoring references
232 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_1);
233 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_2);
234 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_3);
235 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
237 //workflow references
238 externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE, REF_6);
240 final TitanOperationStatus commit = this.titanDao.commit();
241 assertThat(commit).isEqualTo(TitanOperationStatus.OK);
247 public static final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
250 private static User adminUser = new User("admin", "admin", "admin", "admin@email.com", Role.ADMIN.name(), System.currentTimeMillis());
251 private static User designerUser = new User("designer", "designer", "designer", "designer@email.com", Role.DESIGNER.name(), System.currentTimeMillis());
252 private static User otherUser = new User("other", "other", "other", "other@email.com", Role.OPS.name(), System.currentTimeMillis());
256 public static void setup() {
258 //Needed for User Authorization
259 //========================================================================================================================
260 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
261 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
262 when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils);
263 when(servletUtils.getUserAdmin()).thenReturn(userAdmin);
264 when(servletUtils.getComponentsUtils()).thenReturn(componentUtils);
265 when(componentUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)).thenReturn(responseFormat);
266 when(responseFormat.getStatus()).thenReturn(HttpStatus.UNAUTHORIZED.value());
268 //Needed for error configuration
269 when(notFoundResponseFormat.getStatus()).thenReturn(HttpStatus.NOT_FOUND.value());
270 when(badRequestResponseFormat.getStatus()).thenReturn(HttpStatus.BAD_REQUEST.value());
271 when(componentUtils.getResponseFormat(eq(ActionStatus.RESOURCE_NOT_FOUND), (String[]) any())).thenReturn(notFoundResponseFormat);
272 when(componentUtils.getResponseFormat(eq(ActionStatus.COMPONENT_VERSION_NOT_FOUND), (String[]) any())).thenReturn(notFoundResponseFormat);
273 when(componentUtils.getResponseFormat(eq(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND), (String[]) any())).thenReturn(notFoundResponseFormat);
274 when(componentUtils.getResponseFormat(eq(ActionStatus.EXT_REF_NOT_FOUND), (String[]) any())).thenReturn(notFoundResponseFormat);
275 when(componentUtils.getResponseFormat(eq(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID), (String[]) any())).thenReturn(badRequestResponseFormat);
277 when(graphLockOperation.lockComponent(any(), any())).thenReturn(StorageOperationStatus.OK);
279 Either<User, ActionStatus> adminEither = Either.left(adminUser);
280 Either<User, ActionStatus> designerEither = Either.left(designerUser);
281 Either<User, ActionStatus> otherEither = Either.left(otherUser);
283 when(userAdmin.getUser(adminUser.getUserId(), false)).thenReturn(adminEither);
284 when(userAdmin.getUser(designerUser.getUserId(), false)).thenReturn(designerEither);
285 when(userAdmin.getUser(otherUser.getUserId(), false)).thenReturn(otherEither);
286 //========================================================================================================================
288 //TODO DELETE after successfull merge
289 //if (ConfigurationManager.getConfigurationManager() == null) {
290 String appConfigDir = "src/test/resources/config";
291 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
292 configurationManager = new ConfigurationManager(configurationSource);
294 org.openecomp.sdc.be.config.Configuration configuration = new org.openecomp.sdc.be.config.Configuration();
295 configuration.setTitanInMemoryGraph(true);
297 configurationManager.setConfiguration(configuration);
299 ExternalConfiguration.setAppName("catalog-be");
303 public void beforeTest() {
308 public void testGetExternalRefsForExistingComponentInstance() {
309 String path = String.format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
311 Response response = target().
314 accept(MediaType.APPLICATION_JSON).
315 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
316 header(Constants.USER_ID_HEADER, designerUser.getUserId()).
319 List<String> dto = response.readEntity(new GenericType<List<String>>() { });
320 assertThat(dto).containsExactly(REF_1, REF_2, REF_3, REF_5);
321 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
325 public void testGetExternalRefsForAsset() {
326 String path = String.format("/v1/catalog/services/%s/version/%s/externalReferences/%s", serviceVertexUuid, VERSION, MONITORING_OBJECT_TYPE);
328 Response response = target().
331 accept(MediaType.APPLICATION_JSON).
332 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
333 header(Constants.USER_ID_HEADER, designerUser.getUserId()).
336 Map<String, List<String>> dtoMap = response.readEntity(new GenericType<HashMap<String, List<String>>>() { });
337 assertThat(dtoMap.get(COMPONENT_ID)).containsExactly(REF_1, REF_2, REF_3, REF_5);
338 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
342 public void testAddExternalRefForExistingComponentInstance(){
343 String path = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE);
344 String getPath = String.format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
345 Response response = target().
347 request(MediaType.APPLICATION_JSON).
348 accept(MediaType.APPLICATION_JSON).
349 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
350 header(Constants.USER_ID_HEADER, designerUser.getUserId()).
351 post(Entity.json(new ExternalRefDTO(REF_4)));
353 assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value());
355 //Check that GET will include the new reference
359 accept(MediaType.APPLICATION_JSON).
360 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
361 header(Constants.USER_ID_HEADER, adminUser.getUserId()).
364 List<String> dto = response.readEntity(new GenericType<List<String>>() { });
365 assertThat(dto).containsExactlyInAnyOrder(REF_1, REF_2, REF_3, REF_4, REF_5);
366 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
370 public void testDeleteExternalRefForExistingComponentInstance(){
371 String deletePath = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
372 String getPath = String.format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
373 Response response = target().
375 request(MediaType.APPLICATION_JSON).
376 accept(MediaType.APPLICATION_JSON).
377 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
378 header(Constants.USER_ID_HEADER, designerUser.getUserId()).
381 //Verify that the deleted reference is returned in body
382 ExternalRefDTO dto = response.readEntity(ExternalRefDTO.class);
383 assertThat(dto.getReferenceUUID()).isEqualTo(REF_5);
384 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
386 //Check that GET will NOT include the deleted reference
390 accept(MediaType.APPLICATION_JSON).
391 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
392 header(Constants.USER_ID_HEADER, designerUser.getUserId()).
395 List<String> getResponse = response.readEntity(new GenericType<List<String>>() { });
396 assertThat(getResponse).containsExactlyInAnyOrder(REF_1, REF_2, REF_3);
397 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
401 public void testUpdateExternalRefForExistingComponentInstance(){
402 String updatePath = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
403 String getPath = String.format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
404 Response response = target().
406 request(MediaType.APPLICATION_JSON).
407 accept(MediaType.APPLICATION_JSON).
408 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
409 header(Constants.USER_ID_HEADER, designerUser.getUserId()).
410 put(Entity.json(new ExternalRefDTO(REF_4)));
412 //Verify that the updated reference is returned in body
413 ExternalRefDTO putResponseBody = response.readEntity(ExternalRefDTO.class);
414 assertThat(putResponseBody.getReferenceUUID()).isEqualTo(REF_4);
415 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
417 //Check that GET will include the updated reference
421 accept(MediaType.APPLICATION_JSON).
422 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
423 header(Constants.USER_ID_HEADER, designerUser.getUserId()).
427 List<String> dto = response.readEntity(new GenericType<List<String>>() { });
428 assertThat(dto).containsExactlyInAnyOrder(REF_1, REF_2, REF_3, REF_4);
429 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
437 public void testAddExternalRefForNonExistingAssetId(){
438 String path = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", "non-existing-uuid", COMPONENT_ID, MONITORING_OBJECT_TYPE);
439 Response response = target().
441 request(MediaType.APPLICATION_JSON).
442 accept(MediaType.APPLICATION_JSON).
443 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
444 header(Constants.USER_ID_HEADER, designerUser.getUserId()).
445 post(Entity.json(new ExternalRefDTO(REF_4)));
447 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
451 public void testAddExternalRefForNonExistingCompInstId(){
452 String path = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, "FAKE_COM_ID", MONITORING_OBJECT_TYPE);
453 Response response = target().
455 request(MediaType.APPLICATION_JSON).
456 accept(MediaType.APPLICATION_JSON).
457 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
458 header(Constants.USER_ID_HEADER, designerUser.getUserId()).
459 post(Entity.json(new ExternalRefDTO(REF_4)));
461 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
465 public void testAddExistingExternalRef(){
466 String path = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE);
467 Response response = target().
469 request(MediaType.APPLICATION_JSON).
470 accept(MediaType.APPLICATION_JSON).
471 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
472 header(Constants.USER_ID_HEADER, designerUser.getUserId()).
473 post(Entity.json(new ExternalRefDTO(REF_1)));
475 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); //Ref not created but still OK is returned
479 public void testUpdateExternalRefForNonExistingAssetId(){
480 String updatePath = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", "nonExistingServiceVertexUuid", COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
482 Response response = target().
484 request(MediaType.APPLICATION_JSON).
485 accept(MediaType.APPLICATION_JSON).
486 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
487 header(Constants.USER_ID_HEADER, designerUser.getUserId()).
488 put(Entity.json(new ExternalRefDTO(REF_4)));
490 //Verify that the 404 is returned
491 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
496 public void testUpdateExternalRefForNonExistingObjectIdOrOldRef(){
497 String updatePath = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, COMPONENT_ID, "FAKE_OBJ_TYPE", REF_5);
499 Response response = target().
501 request(MediaType.APPLICATION_JSON).
502 accept(MediaType.APPLICATION_JSON).
503 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
504 header(Constants.USER_ID_HEADER, designerUser.getUserId()).
505 put(Entity.json(new ExternalRefDTO(REF_4)));
507 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
508 System.out.println(response.readEntity(String.class));
512 public void testDeleteExternalRefForNonExistingAssetId(){
513 String deletePath = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", "non-existing-asset", COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5);
514 Response response = target().
516 request(MediaType.APPLICATION_JSON).
517 accept(MediaType.APPLICATION_JSON).
518 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
519 header(Constants.USER_ID_HEADER, designerUser.getUserId()).
522 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
526 public void testDeleteExternalRefForNonExistingRef(){
527 String deletePath = String.format("/v1/catalog/services/%s/resourceInstances/%s/externalReferences/%s/%s", serviceVertexUuid, 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").
533 header(Constants.USER_ID_HEADER, designerUser.getUserId()).
536 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
537 System.out.println(response.readEntity(String.class));
541 public void testGetExternalRefsForNonExistingAsset() {
542 String path = String.format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", "fake-asset-id", 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 public void testGetExternalRefsForNonExistingVersion() {
557 String path = String.format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, FAKE_VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
559 Response response = target().
562 accept(MediaType.APPLICATION_JSON).
563 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
564 header(Constants.USER_ID_HEADER, designerUser.getUserId()).
567 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
571 public void testGetExternalRefsForExistingComponentInstanceWithUnauthorizedUser() {
572 String path = String.format("/v1/catalog/services/%s/version/%s/resourceInstances/%s/externalReferences/%s", serviceVertexUuid, VERSION, COMPONENT_ID, MONITORING_OBJECT_TYPE);
574 Response response = target().
577 accept(MediaType.APPLICATION_JSON).
578 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
579 header(Constants.USER_ID_HEADER, otherUser.getUserId()).
582 assertThat(response.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED.value());
586 protected Application configure() {
587 ApplicationContext context = new AnnotationConfigApplicationContext(TestSpringConfig.class);
588 forceSet(TestProperties.CONTAINER_PORT, "0");
589 return new ResourceConfig(ExternalRefsServlet.class)
590 .register(new AbstractBinder() {
593 protected void configure() {
594 bind(request).to(HttpServletRequest.class);
595 when(request.getSession()).thenReturn(session);
596 when(request.getHeader(eq(Constants.X_ECOMP_INSTANCE_ID_HEADER))).thenReturn("mockXEcompInstIdHeader");
597 when(session.getServletContext()).thenReturn(servletContext);
600 .property("contextConfig", context);