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