fcc5b77e27950dbafb6546269fb201c0033a2fd5
[aai/search-data-service.git] / search-data-service / 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 static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
24 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
25
26 import java.io.File;
27 import java.io.IOException;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
32 import org.springframework.boot.test.context.SpringBootTest;
33 import org.springframework.http.MediaType;
34 import org.springframework.test.context.junit4.SpringRunner;
35 import org.springframework.test.web.servlet.MockMvc;
36
37
38 /**
39  * This suite of tests validates the behaviour of the bulk operations REST end point.
40  */
41 @RunWith(SpringRunner.class)
42 @SpringBootTest(classes = org.onap.aai.sa.Application.class)
43 @AutoConfigureMockMvc
44 public class BulkApiTest {
45
46     private final String TOP_URI = "/test/bulk";
47
48     @Autowired
49     private MockMvc mockMvc;
50
51     @Test
52     public void authenticationFailureTest() throws Exception {
53
54         this.mockMvc
55                 .perform(post(TOP_URI).contentType(MediaType.APPLICATION_JSON)
56                         .content(SearchServiceApiHarness.FAIL_AUTHENTICATION_TRIGGER))
57                 .andExpect(status().isForbidden());
58     }
59
60
61     /**
62      * This test validates that properly constructed json payloads are correctly validated and that improperly
63      * contructed payloads will be rejected with the appropriate response code returned to the client.
64      *
65      * @throws IOException
66      */
67     @Test
68     public void payloadValidationTest() throws Exception {
69
70         // Post a request to the bulk operations endpoint with a valid
71         // operations list payload.
72         File validBulkOpsFile = new File("src/test/resources/json/bulk-ops-valid.json");
73         String validPayloadStr = TestUtils.readFileToString(validBulkOpsFile);
74
75         // Validate that the payload is accepted as expected.
76         this.mockMvc.perform(post(TOP_URI).contentType(MediaType.APPLICATION_JSON).content(validPayloadStr))
77                 .andExpect(status().isOk());
78
79         // Post a request to the bulk operations endpoint with an invalid
80         // operations list payload.
81         File inValidBulkOpsFile = new File("src/test/resources/json/bulk-ops-invalid.json");
82         String inValidPayloadStr = TestUtils.readFileToString(inValidBulkOpsFile);
83         this.mockMvc.perform(post(TOP_URI).contentType(MediaType.APPLICATION_JSON).content(inValidPayloadStr))
84                 .andExpect(status().isBadRequest());
85     }
86 }