ebd933797d17bc896ef05944727327ac2f41c849
[aai/search-data-service.git] / src / test / java / org / onap / aai / sa / rest / BulkApiTest.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.aai.sa.rest;
22
23 import org.glassfish.jersey.server.ResourceConfig;
24 import org.glassfish.jersey.test.JerseyTest;
25 import org.junit.Test;
26
27 import javax.ws.rs.client.Entity;
28 import javax.ws.rs.core.Application;
29 import javax.ws.rs.core.Response;
30 import java.io.BufferedReader;
31 import java.io.File;
32 import java.io.FileReader;
33 import java.io.IOException;
34
35 import static org.junit.Assert.assertEquals;
36
37
38 /**
39  * This suite of tests validates the behaviour of the bulk operations REST
40  * end point.
41  */
42 public class BulkApiTest extends JerseyTest {
43
44   private final String TOP_URI = "/test/bulk/";
45
46
47   @Override
48   protected Application configure() {
49
50     // Make sure that our test endpoint is on the resource path
51     // for Jersey Test.
52     return new ResourceConfig(SearchServiceApiHarness.class);
53   }
54
55
56   /**
57    * This test validates that the expected response codes are returned
58    * to the client in the event of an authentication failure.
59    */
60   @Test
61   public void authenticationFailureTest() {
62
63     // Send a request to the end point, with a special trigger in the
64     // payload that tells our test harness to force the authentication
65     // to fail.
66     Response result = target(TOP_URI).request().post(Entity.json(SearchServiceApiHarness.FAIL_AUTHENTICATION_TRIGGER), Response.class);
67
68     // Validate that a failure to authenticate results in the expected
69     // response code returned to the client.
70     assertEquals(Response.Status.FORBIDDEN.getStatusCode(), result.getStatus());
71   }
72
73
74   /**
75    * This test validates that properly constructed json payloads are
76    * correctly validated and that improperly contructed payloads will
77    * be rejected with the appropriate response code returned to the
78    * client.
79    *
80    * @throws IOException
81    */
82   @Test
83   public void payloadValidationTest() throws IOException {
84
85     // Post a request to the bulk operations endpoint with a valid
86     // operations list payload.
87     File validBulkOpsFile = new File("src/test/resources/json/bulk-ops-valid.json");
88     String validPayloadStr = TestUtils.readFileToString(validBulkOpsFile);
89     Response validResult = target(TOP_URI).request().post(Entity.json(validPayloadStr), Response.class);
90
91     // Validate that the payload is accepted as expected.
92     assertEquals("Valid operations payload was rejected",
93         Response.Status.OK.getStatusCode(), validResult.getStatus());
94
95     // Post a request to the bulk operations endpoint with an invalid
96     // operations list payload.
97     File inValidBulkOpsFile = new File("src/test/resources/json/bulk-ops-invalid.json");
98     String inValidPayloadStr = TestUtils.readFileToString(inValidBulkOpsFile);
99     Response invalidResult = target(TOP_URI).request().post(Entity.json(inValidPayloadStr), Response.class);
100
101     // Validate that the payload is rejected as expected.
102     assertEquals("Invalid operations payload was not rejected",
103         Response.Status.BAD_REQUEST.getStatusCode(), invalidResult.getStatus());
104   }
105 }