Increase code coverage
[aai/gizmo.git] / src / test / java / org / onap / crud / service / TestResourceServiceEdgeOperations.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.crud.service;
22
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;
47
48 public class TestResourceServiceEdgeOperations {
49
50     private MockHttpServletRequest servletRequest;
51     private UriInfo uriInfo;
52     private HttpHeaders headers;
53     private AbstractGraphDataService graphDataService;
54
55     @BeforeClass
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");
60
61         OxmModelLoader.loadModels();
62     }
63
64     @Before
65     public void setUp() throws Exception {
66         graphDataService = mock(CrudGraphDataService.class);
67         uriInfo = mock(UriInfo.class);
68
69         mockHeaderRequests(getCreateHeaders());
70
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/");
77
78         setUser("CN=ONAP, OU=ONAP, O=ONAP, L=Ottawa, ST=Ontario, C=CA");
79
80         servletRequest.setAttribute("javax.servlet.request.cipher_suite", "");
81     }
82
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"));
88         return headersMap;
89     }
90
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());
95         }
96         when(headers.getRequestHeaders()).thenReturn(headersMap);
97     }
98
99     @Test
100     public void testCreateRelationship() throws Exception {
101         String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-auto-props.json");
102
103         Response response = callCreateRelationship(postEdgeBody);
104
105         assertThat(response.getStatus()).isEqualTo(Status.CREATED.getStatusCode());
106     }
107
108     @Test
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";
112
113         Response response = createRelationshipWithType(postEdgeBody, type);
114
115         assertThat(response.getStatus()).isEqualTo(Status.CREATED.getStatusCode());
116     }
117
118     @Test
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";
122
123         Response response = createRelationshipWithType(postEdgeBody, type);
124
125         assertThat(response.getStatus()).isEqualTo(Status.BAD_REQUEST.getStatusCode());
126     }
127
128     @Test
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";
132
133         Response response = createRelationshipWithType(postEdgeBody, type);
134
135         assertThat(response.getStatus()).isEqualTo(Status.BAD_REQUEST.getStatusCode());
136     }
137
138     @Test
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";
142
143         Response response = createRelationshipWithType(postEdgeBody, type);
144
145         assertThat(response.getStatus()).isEqualTo(Status.BAD_REQUEST.getStatusCode());
146     }
147
148     @Test
149     public void testInvalidUser() throws Exception {
150         String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-auto-props.json");
151
152         setUser("CN=INVALID, OU=INVALID, O=INVALID, L=Ottawa, ST=Ontario, C=CA");
153
154         Response response = callCreateRelationship(postEdgeBody);
155
156         assertThat(response.getStatus()).isEqualTo(Status.FORBIDDEN.getStatusCode());
157     }
158
159     @Test
160     public void testNullPropertiesIsBadRequest() throws Exception {
161         String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-null-props.json");
162
163         Response response = callCreateRelationship(postEdgeBody);
164
165         assertThat(response.getStatus()).isEqualTo(Status.BAD_REQUEST.getStatusCode());
166     }
167
168     @Test
169     public void testNoPropertiesIsBadRequest() throws Exception {
170         String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-no-props.json");
171
172         Response response = callCreateRelationship(postEdgeBody);
173
174         assertThat(response.getStatus()).isEqualTo(Status.BAD_REQUEST.getStatusCode());
175     }
176
177     @Test
178     public void testCreateWithIdIsBadRequest() throws Exception {
179         String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-with-id.json");
180
181         Response response = callCreateRelationship(postEdgeBody);
182
183         assertThat(response.getStatus()).isEqualTo(Status.BAD_REQUEST.getStatusCode());
184     }
185
186     @Test
187     public void testNoTypeIsBadRequest() throws Exception {
188         String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-no-type.json");
189
190         Response response = callCreateRelationship(postEdgeBody);
191
192         assertThat(response.getStatus()).isEqualTo(Status.BAD_REQUEST.getStatusCode());
193     }
194
195     @Test
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);
200
201         String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-upsert.json");
202         String type = "tosca.relationships.HostedOn";
203         String id = "12345";
204         EntityTag entityTag = new EntityTag("1234");
205         
206         when(graphDataService.patchEdge(any(), any(), any(), any())).thenReturn(new ImmutablePair<EntityTag, String>(entityTag, "dummy output"));
207         AaiResourceService aaiResourceService = new AaiResourceService(graphDataService);
208         Response response =
209                 aaiResourceService.upsertEdge(postEdgeBody, type, id, "uri", headers, uriInfo, servletRequest);
210         assertThat(response.getStatus()).isEqualTo(Status.OK.getStatusCode());
211     }
212
213     @Test
214     public void testUpdateEdge() throws Exception {
215         String postEdgeBody = TestUtil.getFileAsString("aai-resource-service/post-edge-upsert.json");
216         String type = "tosca.relationships.HostedOn";
217         String id = "12345";
218         EntityTag entityTag = new EntityTag("1234");
219
220         when(graphDataService.updateEdge(any(), any(), any(), any())).thenReturn(new ImmutablePair<EntityTag, String>(entityTag, "dummy output"));
221         AaiResourceService aaiResourceService = new AaiResourceService(graphDataService);
222         Response response =
223                 aaiResourceService.upsertEdge(postEdgeBody, type, id, "uri", headers, uriInfo, servletRequest);
224         assertThat(response.getStatus()).isEqualTo(Status.OK.getStatusCode());
225     }
226
227     @Test
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";
231         String id = "12345";
232         EntityTag entityTag = new EntityTag("1234");
233
234         when(graphDataService.updateEdge(any(), any(), any(), any())).thenReturn(new ImmutablePair<EntityTag, String>(entityTag, "dummy output"));
235         AaiResourceService aaiResourceService = new AaiResourceService(graphDataService);
236         Response response =
237                 aaiResourceService.upsertEdge(postEdgeBody, type, id, "uri", headers, uriInfo, servletRequest);
238         assertThat(response.getStatus()).isEqualTo(Status.BAD_REQUEST.getStatusCode());
239     }
240
241     @Test
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");
247
248         when(graphDataService.updateEdge(any(), any(), any(), any())).thenReturn(new ImmutablePair<EntityTag, String>(entityTag, "dummy output"));
249         AaiResourceService aaiResourceService = new AaiResourceService(graphDataService);
250         Response response =
251                 aaiResourceService.upsertEdge(postEdgeBody, type, id, "uri", headers, uriInfo, servletRequest);
252         assertThat(response.getStatus()).isEqualTo(Status.BAD_REQUEST.getStatusCode());
253     }
254
255     private Response createRelationshipWithType(String postEdgeBody, String type) throws CrudException, Exception {
256         EntityTag entityTag = new EntityTag("1234");
257         
258         when(graphDataService.addEdge(any(), any(), any())).thenReturn(new ImmutablePair<EntityTag, String>(entityTag, "dummy output"));
259         AaiResourceService aaiResourceService = new AaiResourceService(graphDataService);
260         Response response =
261                 aaiResourceService.createRelationship(postEdgeBody, type, "uri", headers, uriInfo, servletRequest);
262         return response;
263     }
264
265     private Response callCreateRelationship(String postEdgeBody) throws CrudException, Exception {
266         EntityTag entityTag = new EntityTag("1234");
267         
268         when(graphDataService.addEdge(any(), any(), any())).thenReturn(new ImmutablePair<EntityTag, String>(entityTag, "dummy output"));
269         AaiResourceService aaiResourceService = new AaiResourceService(graphDataService);
270         Response response =
271                 aaiResourceService.createRelationship(postEdgeBody, "uri", headers, uriInfo, servletRequest);
272         return response;
273     }
274
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});
279     }
280
281     private List<String> createSingletonList(String listItem) {
282         return Collections.<String>singletonList(listItem);
283     }
284 }