Initial search service commit
[aai/search-data-service.git] / src / test / java / org / openecomp / sa / rest / BulkApiTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Search Data Service
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License ati
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.openecomp.sa.rest;
26
27 import org.glassfish.jersey.server.ResourceConfig;
28 import org.glassfish.jersey.test.JerseyTest;
29 import org.junit.Test;
30
31 import javax.ws.rs.client.Entity;
32 import javax.ws.rs.core.Application;
33 import javax.ws.rs.core.Response;
34 import java.io.BufferedReader;
35 import java.io.File;
36 import java.io.FileReader;
37 import java.io.IOException;
38
39 import static org.junit.Assert.assertEquals;
40
41
42 /**
43  * This suite of tests validates the behaviour of the bulk operations REST
44  * end point.
45  */
46 public class BulkApiTest extends JerseyTest {
47
48   private final String TOP_URI = "/test/bulk/";
49
50
51   @Override
52   protected Application configure() {
53
54     // Make sure that our test endpoint is on the resource path
55     // for Jersey Test.
56     return new ResourceConfig(SearchServiceApiHarness.class);
57   }
58
59
60   /**
61    * This test validates that the expected response codes are returned
62    * to the client in the event of an authentication failure.
63    */
64   @Test
65   public void authenticationFailureTest() {
66
67     // Send a request to the end point, with a special trigger in the
68     // payload that tells our test harness to force the authentication
69     // to fail.
70     Response result = target(TOP_URI).request().post(Entity.json(SearchServiceApiHarness.FAIL_AUTHENTICATION_TRIGGER), Response.class);
71
72     // Validate that a failure to authenticate results in the expected
73     // response code returned to the client.
74     assertEquals(Response.Status.FORBIDDEN.getStatusCode(), result.getStatus());
75   }
76
77
78   /**
79    * This test validates that properly constructed json payloads are
80    * correctly validated and that improperly contructed payloads will
81    * be rejected with the appropriate response code returned to the
82    * client.
83    *
84    * @throws IOException
85    */
86   @Test
87   public void payloadValidationTest() throws IOException {
88
89     // Post a request to the bulk operations endpoint with a valid
90     // operations list payload.
91     File validBulkOpsFile = new File("src/test/resources/json/bulk-ops-valid.json");
92     String validPayloadStr = TestUtils.readFileToString(validBulkOpsFile);
93     Response validResult = target(TOP_URI).request().post(Entity.json(validPayloadStr), Response.class);
94
95     // Validate that the payload is accepted as expected.
96     assertEquals("Valid operations payload was rejected",
97         Response.Status.OK.getStatusCode(), validResult.getStatus());
98
99     // Post a request to the bulk operations endpoint with an invalid
100     // operations list payload.
101     File inValidBulkOpsFile = new File("src/test/resources/json/bulk-ops-invalid.json");
102     String inValidPayloadStr = TestUtils.readFileToString(inValidBulkOpsFile);
103     Response invalidResult = target(TOP_URI).request().post(Entity.json(inValidPayloadStr), Response.class);
104
105     // Validate that the payload is rejected as expected.
106     assertEquals("Invalid operations payload was not rejected",
107         Response.Status.BAD_REQUEST.getStatusCode(), invalidResult.getStatus());
108   }
109 }