Sync Integ to Master
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / externalapi / servlet / ExternalRefServletTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  */
20
21 package org.openecomp.sdc.be.externalapi.servlet;
22
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;
27
28 import java.util.HashMap;
29 import java.util.LinkedList;
30 import java.util.List;
31 import java.util.Map;
32
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;
41
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;
91
92 import fj.data.Either;
93
94 public class ExternalRefServletTest extends JerseyTest {
95
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);
100
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);
113
114     private static final String COMPONENT_ID = "ci-MyComponentName";
115
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";
126     //workflow
127     private static final String REF_6 = "ref6";
128
129     @Configuration
130     static class TestSpringConfig {
131
132         private GraphVertex serviceVertex;
133         private ExternalReferencesOperation externalReferenceOperation;
134         private TitanDao titanDao;
135
136         @Bean
137         ExternalRefsServlet externalRefsServlet(){
138             return new ExternalRefsServlet(externalRefsBusinessLogic(), componentUtils);
139         }
140
141         @Bean
142         ExternalRefsBusinessLogic externalRefsBusinessLogic() {
143             return new ExternalRefsBusinessLogic(externalReferencesOperation(), toscaOperationFacade(), graphLockOperation());
144         }
145
146         @Bean
147         ExternalReferencesOperation externalReferencesOperation() {
148             this.externalReferenceOperation = new ExternalReferencesOperation(titanDao(), nodeTypeOpertaion(), topologyTemplateOperation(), idMapper());
149             GraphTestUtils.clearGraph(titanDao);
150             initGraphForTest();
151             return this.externalReferenceOperation;
152         }
153
154         @Bean
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;
165         }
166
167         @Bean
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);
173             return idMapper;
174         }
175
176         @Bean
177         TopologyTemplateOperation topologyTemplateOperation() {
178             return new TopologyTemplateOperation();
179         }
180
181         @Bean
182         NodeTypeOperation nodeTypeOpertaion() {
183             return new NodeTypeOperation(null);
184         }
185
186         @Bean
187         NodeTemplateOperation nodeTemplateOperation(){
188             return new NodeTemplateOperation();
189         }
190
191         @Bean
192         GroupsOperation groupsOperation(){
193             return new GroupsOperation();
194         }
195
196         @Bean
197         ICacheMangerOperation cacheMangerOperation() { return cacheManagerOperation; }
198
199         @Bean
200         TitanDao titanDao() {
201             this.titanDao = new TitanDao(titanGraphClient());
202             return titanDao;
203         }
204
205         @Bean
206         TitanGraphClient titanGraphClient() {
207             return new TitanGraphClient(titanClientStrategy());
208         }
209
210         @Bean
211         TitanClientStrategy titanClientStrategy() {
212             return new DAOTitanStrategy();
213         }
214
215         @Bean
216         CategoryOperation categoryOperation(){
217             return new CategoryOperation();
218         }
219
220         @Bean
221         GraphLockOperation graphLockOperation() { return graphLockOperation; }
222
223         @Bean
224         TitanGenericDao titanGenericDao() { return titanGenericDao; }
225
226         private void initGraphForTest() {
227             //create a service and add 1 ref
228             serviceVertex = GraphTestUtils.createServiceVertex(titanDao, new HashMap<>());
229             serviceVertexUuid = this.serviceVertex.getUniqueId();
230
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);
236
237             //workflow references
238             externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE, REF_6);
239
240             final TitanOperationStatus commit = this.titanDao.commit();
241             assertThat(commit).isEqualTo(TitanOperationStatus.OK);
242         }
243
244
245     }
246
247     public static final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
248
249     /* Users */
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());
253
254
255     @BeforeClass
256     public static void setup() {
257
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());
267
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);
276
277         when(graphLockOperation.lockComponent(any(), any())).thenReturn(StorageOperationStatus.OK);
278
279         Either<User, ActionStatus> adminEither = Either.left(adminUser);
280         Either<User, ActionStatus> designerEither = Either.left(designerUser);
281         Either<User, ActionStatus> otherEither = Either.left(otherUser);
282
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         //========================================================================================================================
287         
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);
293
294             org.openecomp.sdc.be.config.Configuration configuration = new org.openecomp.sdc.be.config.Configuration();
295             configuration.setTitanInMemoryGraph(true);
296
297             configurationManager.setConfiguration(configuration);
298         //}
299         ExternalConfiguration.setAppName("catalog-be");
300     }
301
302     @Before
303     public void beforeTest() {
304
305     }
306
307     @Test
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);
310
311         Response response = target().
312                             path(path).
313                             request().
314                             accept(MediaType.APPLICATION_JSON).
315                             header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
316                             header(Constants.USER_ID_HEADER, designerUser.getUserId()).
317                             get();
318
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());
322     }
323
324     @Test
325     public void testGetExternalRefsForAsset() {
326         String path = String.format("/v1/catalog/services/%s/version/%s/externalReferences/%s", serviceVertexUuid, VERSION, MONITORING_OBJECT_TYPE);
327
328         Response response = target().
329                 path(path).
330                 request().
331                 accept(MediaType.APPLICATION_JSON).
332                 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
333                 header(Constants.USER_ID_HEADER, designerUser.getUserId()).
334                 get();
335
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());
339     }
340
341     @Test
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().
346                             path(path).
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)));
352
353         assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value());
354
355         //Check that GET will include the new reference
356         response = target().
357                         path(getPath).
358                         request().
359                         accept(MediaType.APPLICATION_JSON).
360                         header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
361                         header(Constants.USER_ID_HEADER, adminUser.getUserId()).
362                         get();
363
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());
367     }
368
369     @Test
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().
374                 path(deletePath).
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()).
379                 delete();
380
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());
385
386         //Check that GET will NOT include the deleted reference
387         response = target().
388                 path(getPath).
389                 request().
390                 accept(MediaType.APPLICATION_JSON).
391                 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
392                 header(Constants.USER_ID_HEADER, designerUser.getUserId()).
393                 get();
394
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());
398     }
399
400     @Test
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().
405                 path(updatePath).
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)));
411
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());
416
417         //Check that GET will include the updated reference
418         response = target().
419                 path(getPath).
420                 request().
421                 accept(MediaType.APPLICATION_JSON).
422                 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
423                 header(Constants.USER_ID_HEADER, designerUser.getUserId()).
424                 get();
425
426
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());
430     }
431
432
433     /*
434     * Rainy Scenarios
435     */
436     @Test
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().
440                 path(path).
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)));
446
447         assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
448     }
449
450     @Test
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().
454                 path(path).
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)));
460
461         assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
462     }
463
464     @Test
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().
468                 path(path).
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)));
474
475         assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); //Ref not created but still OK is returned
476     }
477
478     @Test
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);
481
482         Response response = target().
483                 path(updatePath).
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)));
489
490         //Verify that the 404 is returned
491         assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
492
493     }
494
495     @Test
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);
498
499         Response response = target().
500                 path(updatePath).
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)));
506
507         assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
508         System.out.println(response.readEntity(String.class));
509     }
510
511     @Test
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().
515                 path(deletePath).
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()).
520                 delete();
521
522         assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
523     }
524
525     @Test
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().
529                 path(deletePath).
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()).
534                 delete();
535
536         assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
537         System.out.println(response.readEntity(String.class));
538     }
539
540     @Test
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);
543
544         Response response = target().
545                         path(path).
546                         request().
547                         accept(MediaType.APPLICATION_JSON).
548                         header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
549                         header(Constants.USER_ID_HEADER, designerUser.getUserId()).
550                         get();
551
552         assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
553     }
554
555     @Test
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);
558
559         Response response = target().
560                 path(path).
561                 request().
562                 accept(MediaType.APPLICATION_JSON).
563                 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
564                 header(Constants.USER_ID_HEADER, designerUser.getUserId()).
565                 get();
566
567         assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
568     }
569
570     @Test
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);
573
574         Response response = target().
575                 path(path).
576                 request().
577                 accept(MediaType.APPLICATION_JSON).
578                 header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").
579                 header(Constants.USER_ID_HEADER, otherUser.getUserId()).
580                 get();
581
582         assertThat(response.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED.value());
583     }
584
585     @Override
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() {
591
592                     @Override
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);
598                     }
599                 })
600                 .property("contextConfig", context);
601     }
602
603 }