Remove raw-type related warnings in resources
[aai/resources.git] / aai-resources / src / test / java / org / onap / aai / rest / PserverMissingTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.rest;
22
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.core.StringContains.containsString;
25 import static org.junit.Assert.assertEquals;
26
27 import java.util.Base64;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.UUID;
32
33 import org.junit.Test;
34 import org.onap.aai.PayloadUtil;
35 import org.springframework.http.HttpEntity;
36 import org.springframework.http.HttpHeaders;
37 import org.springframework.http.HttpMethod;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.MediaType;
40 import org.springframework.http.ResponseEntity;
41
42 public class PserverMissingTest extends AbstractSpringRestTest {
43
44     @Test
45     public void testWhenContentTypeMissingItWillFunctionalAndCreateObjectWithPayloadInJson() throws Exception {
46
47         String id = "test-" + UUID.randomUUID().toString();
48         String endpoint = "/aai/v11/cloud-infrastructure/pservers/pserver/" + id;
49         HttpHeaders headers = new HttpHeaders();
50
51         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
52         headers.add("Real-Time", "true");
53         headers.add("X-FromAppId", "JUNIT");
54         headers.add("X-TransactionId", "JUNIT");
55         String authorization = Base64.getEncoder().encodeToString("AAI:AAI".getBytes("UTF-8"));
56         headers.add("Authorization", "Basic " + authorization);
57
58         Map<String, String> templateMap = new HashMap<>();
59
60         templateMap.put("hostname", id);
61         String body = PayloadUtil.getTemplatePayload("pserver.json", templateMap);
62
63         httpEntity = new HttpEntity<String>(body, headers);
64         baseUrl = "http://localhost:" + randomPort;
65
66         ResponseEntity<String> responseEntity;
67         responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class);
68
69         assertEquals(HttpStatus.CREATED, responseEntity.getStatusCode());
70     }
71
72     @Test
73     public void testWhenAcceptMissingItWillReturnThePayloadInXml() throws Exception {
74
75         String id = "test-" + UUID.randomUUID().toString();
76         String endpoint = "/aai/v11/cloud-infrastructure/pservers/pserver/" + id;
77         HttpHeaders headers = new HttpHeaders();
78
79         headers.add("Real-Time", "true");
80         headers.add("X-FromAppId", "JUNIT");
81         headers.add("X-TransactionId", "JUNIT");
82         String authorization = Base64.getEncoder().encodeToString("AAI:AAI".getBytes("UTF-8"));
83         headers.add("Authorization", "Basic " + authorization);
84
85         httpEntity = new HttpEntity<String>(headers);
86         baseUrl = "http://localhost:" + randomPort;
87
88         ResponseEntity<String> responseEntity;
89         responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.GET, httpEntity, String.class);
90
91         String body = responseEntity.getBody().toString();
92
93         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
94         assertThat(body, containsString("<Fault>"));
95         assertThat(body, containsString("Resource not found for"));
96         assertThat(body, containsString("Node Not Found"));
97     }
98 }