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