Improve doc for Portal API configuration
[portal/ric-dashboard.git] / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / controller / PortalRestCentralServiceTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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 package org.oransc.ric.portal.dashboard.controller;
21
22 import java.lang.invoke.MethodHandles;
23 import java.net.URI;
24
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.DashboardConstants;
30 import org.oransc.ric.portal.dashboard.config.PortalApIMockConfiguration;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.http.HttpEntity;
34 import org.springframework.http.HttpHeaders;
35 import org.springframework.http.HttpMethod;
36 import org.springframework.http.ResponseEntity;
37
38 public class PortalRestCentralServiceTest extends AbstractControllerTest {
39
40         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
41
42         // paths are hardcoded here exactly like the EPSDK-FW library :(
43
44         @Test
45         public void getAnalyticsTest() {
46                 // paths are hardcoded here exactly like the EPSDK-FW library :(
47                 URI uri = buildUri(null, PortalApiConstants.API_PREFIX, "/analytics");
48                 logger.info("Invoking {}", uri);
49                 ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, null, String.class);
50                 // No Portal is available so this always fails
51                 Assertions.assertTrue(response.getStatusCode().is4xxClientError());
52         }
53
54         @Test
55         public void getLoginPageTest() {
56                 URI uri = buildUri(null, DashboardConstants.LOGIN_PAGE);
57                 logger.info("Invoking {}", uri);
58                 ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, null, String.class);
59                 Assertions.assertTrue(response.getStatusCode().is2xxSuccessful());
60                 Assertions.assertTrue(response.getBody().contains("Please log in"));
61         }
62
63         private HttpEntity<Object> getEntityWithHeaders(Object body) {
64                 HttpHeaders headers = new HttpHeaders();
65                 headers.set(PortalApIMockConfiguration.PORTAL_USERNAME_HEADER_KEY,
66                                 PortalApIMockConfiguration.PORTAL_USERNAME_HEADER_KEY);
67                 headers.set(PortalApIMockConfiguration.PORTAL_PASSWORD_HEADER_KEY,
68                                 PortalApIMockConfiguration.PORTAL_PASSWORD_HEADER_KEY);
69                 HttpEntity<Object> entity = new HttpEntity<>(body, headers);
70                 return entity;
71         }
72
73         @Test
74         public void createUserTest() {
75                 final String loginId = "login1";
76                 URI create = buildUri(null, PortalApiConstants.API_PREFIX, "user");
77                 logger.info("Invoking {}", create);
78                 EcompUser user = new EcompUser();
79                 user.setLoginId(loginId);
80                 HttpEntity<Object> requestEntity = getEntityWithHeaders(user);
81                 ResponseEntity<String> response = restTemplate.exchange(create, HttpMethod.POST, requestEntity, String.class);
82                 Assertions.assertTrue(response.getStatusCode().is2xxSuccessful());
83         }
84
85         @Test
86         public void updateUserTest() {
87                 final String loginId = "login2";
88                 URI create = buildUri(null, PortalApiConstants.API_PREFIX, "user");
89                 logger.info("Invoking {}", create);
90                 EcompUser user = new EcompUser();
91                 user.setLoginId(loginId);
92                 HttpEntity<Object> requestEntity = getEntityWithHeaders(user);
93                 // Create
94                 ResponseEntity<String> response = restTemplate.exchange(create, HttpMethod.POST, requestEntity, String.class);
95                 Assertions.assertTrue(response.getStatusCode().is2xxSuccessful());
96                 URI update = buildUri(null, PortalApiConstants.API_PREFIX, "user", loginId);
97                 user.setEmail("user@company.org");
98                 requestEntity = getEntityWithHeaders(user);
99                 response = restTemplate.exchange(update, HttpMethod.POST, requestEntity, String.class);
100                 Assertions.assertTrue(response.getStatusCode().is2xxSuccessful());
101         }
102
103 }