48aa0844af64856890b3c53970b8c99b0408d937
[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.beans.factory.annotation.Value;
38 import org.springframework.http.HttpEntity;
39 import org.springframework.http.HttpHeaders;
40 import org.springframework.http.HttpMethod;
41 import org.springframework.http.ResponseEntity;
42
43 public class PortalRestCentralServiceTest extends AbstractControllerTest {
44
45         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
46
47         // Get values from configuration, probably blank
48         @Value("${portalapi.username}")
49         private String portalApiUsername;
50         @Value("${portalapi.password}")
51         private String portalApiPassword;
52
53         @Test
54         public void getAnalyticsTest() {
55                 // paths are hardcoded here exactly like the EPSDK-FW library :(
56                 URI uri = buildUri(null, PortalApiConstants.API_PREFIX, "/analytics");
57                 logger.info("Invoking {}", uri);
58                 ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, null, String.class);
59                 // No Portal is available so this always fails
60                 Assertions.assertTrue(response.getStatusCode().is4xxClientError());
61         }
62
63         @Test
64         public void getErrorPageTest() {
65                 // Send unauthorized request
66                 URI uri = buildUri(null, "/favicon.ico");
67                 logger.info("Invoking {}", uri);
68                 ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, null, String.class);
69                 Assertions.assertTrue(response.getStatusCode().is4xxClientError());
70                 Assertions.assertTrue(response.getBody().contains("Static error page"));
71         }
72
73         private HttpEntity<Object> getEntityWithAuthHeaders(Object body) {
74                 HttpHeaders headers = new HttpHeaders();
75                 headers.set(IPortalRestCentralService.CREDENTIALS_USER, portalApiUsername);
76                 headers.set(IPortalRestCentralService.CREDENTIALS_PASS, portalApiPassword);
77                 HttpEntity<Object> entity = new HttpEntity<>(body, headers);
78                 return entity;
79         }
80
81         private EcompUser createEcompUser(String loginId) {
82                 EcompUser user = new EcompUser();
83                 user.setLoginId(loginId);
84                 EcompRole role = new EcompRole();
85                 role.setRoleFunctions(Collections.EMPTY_SET);
86                 role.setId(1L);
87                 role.setName(DashboardConstants.ROLE_NAME_ADMIN);
88                 Set<EcompRole> roles = new HashSet<>();
89                 roles.add(role);
90                 user.setRoles(roles);
91                 return user;
92         }
93
94         /** See comments in {@link DefaultContextTest} */
95         @Test
96         public void createUserTest() {
97                 final String loginId = "login-" + Long.toString(System.currentTimeMillis());
98                 URI create = buildUri(null, PortalApiConstants.API_PREFIX, "user");
99                 logger.info("createUserTest invoking {}", create);
100                 HttpEntity<Object> requestEntity = getEntityWithAuthHeaders(createEcompUser(loginId));
101                 ResponseEntity<String> response = restTemplate.exchange(create, HttpMethod.POST, requestEntity, String.class);
102                 logger.info("createUserTest response {}", response);
103                 Assertions.assertTrue(response.getStatusCode().is2xxSuccessful());
104         }
105
106         /** See comments in {@link DefaultContextTest} */
107         @Test
108         public void updateUserTest() {
109                 final String loginId = "login-" + Long.toString(System.currentTimeMillis());
110                 URI create = buildUri(null, PortalApiConstants.API_PREFIX, "user");
111                 EcompUser user = createEcompUser(loginId);
112                 logger.info("updateUserTest invoking {}", create);
113                 HttpEntity<Object> requestEntity = getEntityWithAuthHeaders(user);
114                 ResponseEntity<String> response = restTemplate.exchange(create, HttpMethod.POST, requestEntity, String.class);
115                 logger.info("updateUserTest create response {}", response);
116                 Assertions.assertTrue(response.getStatusCode().is2xxSuccessful());
117                 URI update = buildUri(null, PortalApiConstants.API_PREFIX, "user", loginId);
118                 user.setEmail("user@company.org");
119                 requestEntity = getEntityWithAuthHeaders(user);
120                 logger.info("updateUserTest invoking {}", update);
121                 response = restTemplate.exchange(update, HttpMethod.POST, requestEntity, String.class);
122                 logger.info("updateUserTest response {}", response);
123                 Assertions.assertTrue(response.getStatusCode().is2xxSuccessful());
124         }
125
126 }