Reorganize dashboard into subfolders
[portal/ric-dashboard.git] / dashboard / 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
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 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.Set;
27
28 import org.junit.jupiter.api.Assertions;
29 import org.junit.jupiter.api.Test;
30 import org.onap.portalsdk.core.onboarding.crossapi.IPortalRestCentralService;
31 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
32 import org.onap.portalsdk.core.restful.domain.EcompRole;
33 import org.onap.portalsdk.core.restful.domain.EcompUser;
34 import org.oransc.ric.portal.dashboard.DashboardConstants;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.http.HttpEntity;
38 import org.springframework.http.HttpHeaders;
39 import org.springframework.http.HttpMethod;
40 import org.springframework.http.ResponseEntity;
41
42 public class PortalRestCentralServiceTest extends AbstractControllerTest {
43
44         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
45
46         @Test
47         public void getAnalyticsTest() {
48                 // paths are hardcoded here exactly like the EPSDK-FW library :(
49                 URI uri = buildUri(null, PortalApiConstants.API_PREFIX, "/analytics");
50                 logger.info("Invoking {}", uri);
51                 ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, null, String.class);
52                 // No Portal is available so this always fails
53                 Assertions.assertTrue(response.getStatusCode().is4xxClientError());
54         }
55
56         @Test
57         public void getErrorPageTest() {
58                 // Send unauthorized request
59                 URI uri = buildUri(null, "/favicon.ico");
60                 logger.info("Invoking {}", uri);
61                 ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, null, String.class);
62                 Assertions.assertTrue(response.getStatusCode().is4xxClientError());
63                 Assertions.assertTrue(response.getBody().contains("Static error page"));
64         }
65
66         private HttpEntity<Object> getEntityWithAuthHeaders(Object body) {
67                 HttpHeaders headers = new HttpHeaders();
68                 headers.set(IPortalRestCentralService.CREDENTIALS_USER, IPortalRestCentralService.CREDENTIALS_USER);
69                 headers.set(IPortalRestCentralService.CREDENTIALS_PASS, IPortalRestCentralService.CREDENTIALS_PASS);
70                 HttpEntity<Object> entity = new HttpEntity<>(body, headers);
71                 return entity;
72         }
73
74         private EcompUser createEcompUser(String loginId) {
75                 EcompUser user = new EcompUser();
76                 user.setLoginId(loginId);
77                 EcompRole role = new EcompRole();
78                 role.setRoleFunctions(Collections.EMPTY_SET);
79                 role.setId(1L);
80                 role.setName(DashboardConstants.ROLE_NAME_ADMIN);
81                 Set<EcompRole> roles = new HashSet<>();
82                 roles.add(role);
83                 user.setRoles(roles);
84                 return user;
85         }
86
87         @Test
88         public void createUserTest() {
89                 final String loginId = "login1";
90                 URI create = buildUri(null, PortalApiConstants.API_PREFIX, "user");
91                 logger.info("createUserTest invoking {}", create);
92                 HttpEntity<Object> requestEntity = getEntityWithAuthHeaders(createEcompUser(loginId));
93                 ResponseEntity<String> response = restTemplate.exchange(create, HttpMethod.POST, requestEntity, String.class);
94                 logger.info("createUserTest response {}", response);
95                 Assertions.assertTrue(response.getStatusCode().is2xxSuccessful());
96         }
97
98         @Test
99         public void updateUserTest() {
100                 final String loginId = "login2";
101                 URI create = buildUri(null, PortalApiConstants.API_PREFIX, "user");
102                 EcompUser user = createEcompUser(loginId);
103                 logger.info("updateUserTest invoking {}", create);
104                 HttpEntity<Object> requestEntity = getEntityWithAuthHeaders(user);
105                 ResponseEntity<String> response = restTemplate.exchange(create, HttpMethod.POST, requestEntity, String.class);
106                 logger.info("updateUserTest response {}", response);
107                 Assertions.assertTrue(response.getStatusCode().is2xxSuccessful());
108                 URI update = buildUri(null, PortalApiConstants.API_PREFIX, "user", loginId);
109                 user.setEmail("user@company.org");
110                 requestEntity = getEntityWithAuthHeaders(user);
111                 logger.info("updateUserTest invoking {}", update);
112                 response = restTemplate.exchange(update, HttpMethod.POST, requestEntity, String.class);
113                 logger.info("updateUserTest response {}", response);
114                 Assertions.assertTrue(response.getStatusCode().is2xxSuccessful());
115         }
116
117 }