2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.crud.service;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27 import java.security.cert.X509Certificate;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Map.Entry;
31 import javax.security.auth.x500.X500Principal;
32 import javax.ws.rs.core.EntityTag;
33 import javax.ws.rs.core.HttpHeaders;
34 import javax.ws.rs.core.MultivaluedHashMap;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.core.UriInfo;
37 import org.apache.commons.lang3.tuple.ImmutablePair;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.mockito.Mockito;
42 import org.onap.crud.exception.CrudException;
43 import org.onap.crud.util.TestUtil;
44 import org.onap.schema.OxmModelLoader;
45 import org.springframework.mock.web.MockHttpServletRequest;
46 import com.att.aft.dme2.internal.jersey.api.client.ClientResponse.Status;
48 public class TestResourceServiceEdgeOperations {
50 private MockHttpServletRequest servletRequest;
51 private UriInfo uriInfo;
52 private HttpHeaders headers;
53 private AbstractGraphDataService graphDataService;
56 public static void setUpBeforeClass() throws Exception {
57 System.setProperty("CONFIG_HOME", "src/test/resources");
58 System.setProperty("AJSC_HOME", ".");
59 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
61 OxmModelLoader.loadModels();
65 public void setUp() throws Exception {
66 graphDataService = mock(CrudGraphDataService.class);
67 uriInfo = mock(UriInfo.class);
69 mockHeaderRequests(getCreateHeaders());
71 servletRequest = new MockHttpServletRequest();
72 servletRequest.setSecure(true);
73 servletRequest.setScheme("https");
74 servletRequest.setServerPort(9520);
75 servletRequest.setServerName("localhost");
76 servletRequest.setRequestURI("/services/inventory/relationships/");
78 setUser("CN=ONAP, OU=ONAP, O=ONAP, L=Ottawa, ST=Ontario, C=CA");
80 servletRequest.setAttribute("javax.servlet.request.cipher_suite", "");
83 private MultivaluedHashMap<String, String> getCreateHeaders() {
84 MultivaluedHashMap<String, String> headersMap = new MultivaluedHashMap<>();
85 headersMap.put("X-TransactionId", createSingletonList("transaction-id"));
86 headersMap.put("X-FromAppId", createSingletonList("app-id"));
87 headersMap.put("Host", createSingletonList("hostname"));
91 private void mockHeaderRequests(MultivaluedHashMap<String, String> headersMap) {
92 headers = Mockito.mock(HttpHeaders.class);
93 for (Entry<String, List<String>> entry : headersMap.entrySet()) {
94 when(headers.getRequestHeader(entry.getKey())).thenReturn(entry.getValue());
96 when(headers.getRequestHeaders()).thenReturn(headersMap);
100 public void testCreateRelationship() throws Exception {
101 String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-auto-props.json");
103 Response response = callCreateRelationship(postEdgeBody);
105 assertThat(response.getStatus()).isEqualTo(Status.CREATED.getStatusCode());
109 public void testCreateRelationshipWithMatchingType() throws Exception {
110 String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-auto-props.json");
111 String type = "tosca.relationships.HostedOn";
113 Response response = createRelationshipWithType(postEdgeBody, type);
115 assertThat(response.getStatus()).isEqualTo(Status.CREATED.getStatusCode());
119 public void testCreateRelationshipNoMatchingType() throws Exception {
120 String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-auto-props.json");
121 String type = "type.does.not.match";
123 Response response = createRelationshipWithType(postEdgeBody, type);
125 assertThat(response.getStatus()).isEqualTo(Status.BAD_REQUEST.getStatusCode());
129 public void testCreateRelationshipWithTypeNullPropsIsBadRequest() throws Exception {
130 String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-null-props.json");
131 String type = "tosca.relationships.HostedOn";
133 Response response = createRelationshipWithType(postEdgeBody, type);
135 assertThat(response.getStatus()).isEqualTo(Status.BAD_REQUEST.getStatusCode());
139 public void testCreateRelationshipWithTypeWithIdIsBadRequest() throws Exception {
140 String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-with-id.json");
141 String type = "tosca.relationships.HostedOn";
143 Response response = createRelationshipWithType(postEdgeBody, type);
145 assertThat(response.getStatus()).isEqualTo(Status.BAD_REQUEST.getStatusCode());
149 public void testInvalidUser() throws Exception {
150 String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-auto-props.json");
152 setUser("CN=INVALID, OU=INVALID, O=INVALID, L=Ottawa, ST=Ontario, C=CA");
154 Response response = callCreateRelationship(postEdgeBody);
156 assertThat(response.getStatus()).isEqualTo(Status.FORBIDDEN.getStatusCode());
160 public void testNullPropertiesIsBadRequest() throws Exception {
161 String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-null-props.json");
163 Response response = callCreateRelationship(postEdgeBody);
165 assertThat(response.getStatus()).isEqualTo(Status.BAD_REQUEST.getStatusCode());
169 public void testNoPropertiesIsBadRequest() throws Exception {
170 String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-no-props.json");
172 Response response = callCreateRelationship(postEdgeBody);
174 assertThat(response.getStatus()).isEqualTo(Status.BAD_REQUEST.getStatusCode());
178 public void testCreateWithIdIsBadRequest() throws Exception {
179 String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-with-id.json");
181 Response response = callCreateRelationship(postEdgeBody);
183 assertThat(response.getStatus()).isEqualTo(Status.BAD_REQUEST.getStatusCode());
187 public void testNoTypeIsBadRequest() throws Exception {
188 String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-no-type.json");
190 Response response = callCreateRelationship(postEdgeBody);
192 assertThat(response.getStatus()).isEqualTo(Status.BAD_REQUEST.getStatusCode());
196 public void testPatchEdge() throws Exception {
197 MultivaluedHashMap<String, String> headersMap = getCreateHeaders();
198 headersMap.put(AaiResourceService.HTTP_PATCH_METHOD_OVERRIDE, createSingletonList("PATCH"));
199 mockHeaderRequests(headersMap);
201 String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-upsert.json");
202 String type = "tosca.relationships.HostedOn";
204 EntityTag entityTag = new EntityTag("1234");
206 when(graphDataService.patchEdge(any(), any(), any(), any())).thenReturn(new ImmutablePair<EntityTag, String>(entityTag, "dummy output"));
207 AaiResourceService aaiResourceService = new AaiResourceService(graphDataService);
209 aaiResourceService.upsertEdge(postEdgeBody, type, id, "uri", headers, uriInfo, servletRequest);
210 assertThat(response.getStatus()).isEqualTo(Status.OK.getStatusCode());
214 public void testUpdateEdge() throws Exception {
215 String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-upsert.json");
216 String type = "tosca.relationships.HostedOn";
218 EntityTag entityTag = new EntityTag("1234");
220 when(graphDataService.updateEdge(any(), any(), any(), any())).thenReturn(new ImmutablePair<EntityTag, String>(entityTag, "dummy output"));
221 AaiResourceService aaiResourceService = new AaiResourceService(graphDataService);
223 aaiResourceService.upsertEdge(postEdgeBody, type, id, "uri", headers, uriInfo, servletRequest);
224 assertThat(response.getStatus()).isEqualTo(Status.OK.getStatusCode());
228 public void testUpdateEdgeNullPropsIsBadRequest() throws Exception {
229 String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-null-props.json");
230 String type = "tosca.relationships.HostedOn";
232 EntityTag entityTag = new EntityTag("1234");
234 when(graphDataService.updateEdge(any(), any(), any(), any())).thenReturn(new ImmutablePair<EntityTag, String>(entityTag, "dummy output"));
235 AaiResourceService aaiResourceService = new AaiResourceService(graphDataService);
237 aaiResourceService.upsertEdge(postEdgeBody, type, id, "uri", headers, uriInfo, servletRequest);
238 assertThat(response.getStatus()).isEqualTo(Status.BAD_REQUEST.getStatusCode());
242 public void testUpdateEdgeWithMismatchIdIsBadRequest() throws Exception {
243 String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-upsert.json");
244 String type = "tosca.relationships.HostedOn";
245 String id = "mismatch";
246 EntityTag entityTag = new EntityTag("1234");
248 when(graphDataService.updateEdge(any(), any(), any(), any())).thenReturn(new ImmutablePair<EntityTag, String>(entityTag, "dummy output"));
249 AaiResourceService aaiResourceService = new AaiResourceService(graphDataService);
251 aaiResourceService.upsertEdge(postEdgeBody, type, id, "uri", headers, uriInfo, servletRequest);
252 assertThat(response.getStatus()).isEqualTo(Status.BAD_REQUEST.getStatusCode());
255 private Response createRelationshipWithType(String postEdgeBody, String type) throws CrudException, Exception {
256 EntityTag entityTag = new EntityTag("1234");
258 when(graphDataService.addEdge(any(), any(), any())).thenReturn(new ImmutablePair<EntityTag, String>(entityTag, "dummy output"));
259 AaiResourceService aaiResourceService = new AaiResourceService(graphDataService);
261 aaiResourceService.createRelationship(postEdgeBody, type, "uri", headers, uriInfo, servletRequest);
265 private Response callCreateRelationship(String postEdgeBody) throws CrudException, Exception {
266 EntityTag entityTag = new EntityTag("1234");
268 when(graphDataService.addEdge(any(), any(), any())).thenReturn(new ImmutablePair<EntityTag, String>(entityTag, "dummy output"));
269 AaiResourceService aaiResourceService = new AaiResourceService(graphDataService);
271 aaiResourceService.createRelationship(postEdgeBody, "uri", headers, uriInfo, servletRequest);
275 private void setUser(String user) {
276 X509Certificate mockCertificate = Mockito.mock(X509Certificate.class);
277 when(mockCertificate.getSubjectX500Principal()).thenReturn(new X500Principal(user));
278 servletRequest.setAttribute("javax.servlet.request.X509Certificate", new X509Certificate[] {mockCertificate});
281 private List<String> createSingletonList(String listItem) {
282 return Collections.<String>singletonList(listItem);