03f7b25d717f24c647118e61a6e60f4835a11eab
[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
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.util.PortalApiConstants;
31 import org.onap.portalsdk.core.restful.domain.EcompRole;
32 import org.onap.portalsdk.core.restful.domain.EcompUser;
33 import org.oransc.ric.portal.dashboard.DashboardConstants;
34 import org.oransc.ric.portal.dashboard.config.PortalApiMockConfiguration;
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> getEntityWithHeaders(Object body) {
67                 HttpHeaders headers = new HttpHeaders();
68                 headers.set(PortalApiMockConfiguration.PORTAL_USERNAME_HEADER_KEY,
69                                 PortalApiMockConfiguration.PORTAL_USERNAME_HEADER_KEY);
70                 headers.set(PortalApiMockConfiguration.PORTAL_PASSWORD_HEADER_KEY,
71                                 PortalApiMockConfiguration.PORTAL_PASSWORD_HEADER_KEY);
72                 HttpEntity<Object> entity = new HttpEntity<>(body, headers);
73                 return entity;
74         }
75
76         private EcompUser createEcompUser(String loginId) {
77                 EcompUser user = new EcompUser();
78                 user.setLoginId(loginId);
79                 EcompRole role = new EcompRole();
80                 role.setRoleFunctions(Collections.EMPTY_SET);
81                 role.setId(1L);
82                 role.setName(DashboardConstants.ROLE_NAME_ADMIN);
83                 Set<EcompRole> roles = new HashSet<>();
84                 roles.add(role);
85                 user.setRoles(roles);
86                 return user;
87         }
88
89         @Test
90         public void createUserTest() {
91                 final String loginId = "login1";
92                 URI create = buildUri(null, PortalApiConstants.API_PREFIX, "user");
93                 logger.info("Invoking {}", create);
94                 HttpEntity<Object> requestEntity = getEntityWithHeaders(createEcompUser(loginId));
95                 ResponseEntity<String> response = restTemplate.exchange(create, HttpMethod.POST, requestEntity, String.class);
96                 Assertions.assertTrue(response.getStatusCode().is2xxSuccessful());
97         }
98
99         @Test
100         public void updateUserTest() {
101                 final String loginId = "login2";
102                 URI create = buildUri(null, PortalApiConstants.API_PREFIX, "user");
103                 EcompUser user = createEcompUser(loginId);
104                 logger.info("Invoking {}", create);
105                 HttpEntity<Object> requestEntity = getEntityWithHeaders(user);
106                 // Create
107                 ResponseEntity<String> response = restTemplate.exchange(create, HttpMethod.POST, requestEntity, String.class);
108                 Assertions.assertTrue(response.getStatusCode().is2xxSuccessful());
109                 URI update = buildUri(null, PortalApiConstants.API_PREFIX, "user", loginId);
110                 user.setEmail("user@company.org");
111                 requestEntity = getEntityWithHeaders(user);
112                 response = restTemplate.exchange(update, HttpMethod.POST, requestEntity, String.class);
113                 Assertions.assertTrue(response.getStatusCode().is2xxSuccessful());
114         }
115
116 }