2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
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===================================
20 package org.oransc.ric.portal.dashboard.controller;
22 import java.lang.invoke.MethodHandles;
25 import org.junit.jupiter.api.Assertions;
26 import org.junit.jupiter.api.Test;
27 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
28 import org.onap.portalsdk.core.restful.domain.EcompUser;
29 import org.oransc.ric.portal.dashboard.config.PortalApIMockConfiguration;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.http.HttpEntity;
33 import org.springframework.http.HttpHeaders;
34 import org.springframework.http.HttpMethod;
35 import org.springframework.http.ResponseEntity;
37 public class PortalRestCentralServiceTest extends AbstractControllerTest {
39 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
42 public void getAnalyticsTest() {
43 // paths are hardcoded here exactly like the EPSDK-FW library :(
44 URI uri = buildUri(null, PortalApiConstants.API_PREFIX, "/analytics");
45 logger.info("Invoking {}", uri);
46 ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, null, String.class);
47 // No Portal is available so this always fails
48 Assertions.assertTrue(response.getStatusCode().is4xxClientError());
52 public void getErrorPageTest() {
53 // Send unauthorized request
54 URI uri = buildUri(null, "/favicon.ico");
55 logger.info("Invoking {}", uri);
56 ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, null, String.class);
57 Assertions.assertTrue(response.getStatusCode().is4xxClientError());
58 Assertions.assertTrue(response.getBody().contains("Static error page"));
61 private HttpEntity<Object> getEntityWithHeaders(Object body) {
62 HttpHeaders headers = new HttpHeaders();
63 headers.set(PortalApIMockConfiguration.PORTAL_USERNAME_HEADER_KEY,
64 PortalApIMockConfiguration.PORTAL_USERNAME_HEADER_KEY);
65 headers.set(PortalApIMockConfiguration.PORTAL_PASSWORD_HEADER_KEY,
66 PortalApIMockConfiguration.PORTAL_PASSWORD_HEADER_KEY);
67 HttpEntity<Object> entity = new HttpEntity<>(body, headers);
72 public void createUserTest() {
73 final String loginId = "login1";
74 URI create = buildUri(null, PortalApiConstants.API_PREFIX, "user");
75 logger.info("Invoking {}", create);
76 EcompUser user = new EcompUser();
77 user.setLoginId(loginId);
78 HttpEntity<Object> requestEntity = getEntityWithHeaders(user);
79 ResponseEntity<String> response = restTemplate.exchange(create, HttpMethod.POST, requestEntity, String.class);
80 Assertions.assertTrue(response.getStatusCode().is2xxSuccessful());
84 public void updateUserTest() {
85 final String loginId = "login2";
86 URI create = buildUri(null, PortalApiConstants.API_PREFIX, "user");
87 logger.info("Invoking {}", create);
88 EcompUser user = new EcompUser();
89 user.setLoginId(loginId);
90 HttpEntity<Object> requestEntity = getEntityWithHeaders(user);
92 ResponseEntity<String> response = restTemplate.exchange(create, HttpMethod.POST, requestEntity, String.class);
93 Assertions.assertTrue(response.getStatusCode().is2xxSuccessful());
94 URI update = buildUri(null, PortalApiConstants.API_PREFIX, "user", loginId);
95 user.setEmail("user@company.org");
96 requestEntity = getEntityWithHeaders(user);
97 response = restTemplate.exchange(update, HttpMethod.POST, requestEntity, String.class);
98 Assertions.assertTrue(response.getStatusCode().is2xxSuccessful());