2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.rest;
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.core.StringContains.containsString;
25 import static org.junit.Assert.assertEquals;
27 import java.util.Base64;
28 import java.util.Collections;
29 import java.util.HashMap;
31 import java.util.UUID;
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;
42 public class PserverMissingTest extends AbstractSpringRestTest {
45 public void testWhenContentTypeMissingItWillFunctionalAndCreateObjectWithPayloadInJson() throws Exception {
47 String id = "test-" + UUID.randomUUID().toString();
48 String endpoint = "/aai/v11/cloud-infrastructure/pservers/pserver/" + id;
49 HttpHeaders headers = new HttpHeaders();
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);
58 Map<String, String> templateMap = new HashMap<>();
60 templateMap.put("hostname", id);
61 String body = PayloadUtil.getTemplatePayload("pserver.json", templateMap);
63 httpEntity = new HttpEntity<String>(body, headers);
64 baseUrl = "http://localhost:" + randomPort;
66 ResponseEntity<String> responseEntity;
67 responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class);
69 assertEquals(HttpStatus.CREATED, responseEntity.getStatusCode());
73 public void testWhenAcceptMissingItWillReturnThePayloadInXml() throws Exception {
75 String id = "test-" + UUID.randomUUID().toString();
76 String endpoint = "/aai/v11/cloud-infrastructure/pservers/pserver/" + id;
77 HttpHeaders headers = new HttpHeaders();
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);
85 httpEntity = new HttpEntity<String>(headers);
86 baseUrl = "http://localhost:" + randomPort;
88 ResponseEntity<String> responseEntity;
89 responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.GET, httpEntity, String.class);
91 String body = responseEntity.getBody().toString();
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"));