aboutsummaryrefslogtreecommitdiffstats
path: root/aai-resources/src/test/java/org/onap/aai/it/multitenancy/MultiTenancyIT.java
blob: baa78c9bb84e6aeea827b64f859223fc7aa97566 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
 * ============LICENSE_START==================================================
 * org.onap.aai
 * ===========================================================================
 * Copyright © 2017-2020 AT&T Intellectual Property. All rights reserved.
 * ===========================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END====================================================
 */

package org.onap.aai.it.multitenancy;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import dasniko.testcontainers.keycloak.KeycloakContainer;

import java.util.Collections;

import org.junit.Test;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.KeycloakBuilder;
import org.keycloak.representations.AccessTokenResponse;
import org.onap.aai.PayloadUtil;
import org.onap.aai.rest.AbstractSpringRestTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.TestPropertySource;

@Import(KeycloakTestConfiguration.class)
@TestPropertySource(locations = "classpath:it/application-keycloak-test.properties")
public class MultiTenancyIT extends AbstractSpringRestTest {

    @Autowired
    private KeycloakContainer keycloakContainer;
    @Autowired
    private RoleHandler roleHandler;
    @Autowired
    private KeycloakTestProperties properties;

    @Test
    public void testCreateAndGetPnf() throws Exception {
        baseUrl = "http://localhost:" + randomPort;
        String endpoint = baseUrl + "/aai/v23/network/pnfs/pnf/pnf-1";
        ResponseEntity<String> responseEntity = null;

        // create pnf with ran (operator)
        String username = "ran", password = "ran";
        headers = this.getHeaders(username, password);
        httpEntity = new HttpEntity<String>(PayloadUtil.getResourcePayload("pnf.json"), headers);
        responseEntity = restTemplate.exchange(endpoint, HttpMethod.PUT, httpEntity, String.class);
        assertEquals(HttpStatus.CREATED, responseEntity.getStatusCode());

        // get pnf with bob (operator_readOnly)
        username = "bob";
        password = "bob";
        headers = this.getHeaders(username, password);
        httpEntity = new HttpEntity<String>("", headers);
        responseEntity = restTemplate.exchange(endpoint, HttpMethod.GET, httpEntity, String.class);
        assertEquals(HttpStatus.OK, responseEntity.getStatusCode());

        // get pnf with ted (selector)
        username = "ted";
        password = "ted";
        headers = this.getHeaders(username, password);
        httpEntity = new HttpEntity<String>("", headers);
        responseEntity = restTemplate.exchange(endpoint, HttpMethod.GET, httpEntity, String.class);
        assertEquals(HttpStatus.FORBIDDEN, responseEntity.getStatusCode());

        // add role to ted and try to get pnf again
        roleHandler.addToUser(RoleHandler.OPERATOR_READ_ONLY, username);
        headers = this.getHeaders(username, password);
        httpEntity = new HttpEntity<String>("", headers);
        responseEntity = restTemplate.exchange(endpoint, HttpMethod.GET, httpEntity, String.class);
        assertEquals(HttpStatus.OK, responseEntity.getStatusCode());

        // get pnf with ran
        username = "ran";
        password = "ran";
        headers = this.getHeaders(username, password);
        httpEntity = new HttpEntity<String>("", headers);
        responseEntity = restTemplate.exchange(endpoint, HttpMethod.GET, httpEntity, String.class);
        assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
    }

    private HttpHeaders getHeaders(String username, String password) {
        HttpHeaders headers = new HttpHeaders();

        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        headers.add("Real-Time", "true");
        headers.add("X-FromAppId", "JUNIT");
        headers.add("X-TransactionId", "JUNIT");
        headers.add("Authorization", "Bearer " + getStringToken(username, password));

        return headers;
    }

    private String getStringToken(String username, String password) {
        Keycloak keycloakClient = KeycloakBuilder.builder().serverUrl(keycloakContainer.getAuthServerUrl())
                .realm(properties.realm).clientId(properties.clientId).clientSecret(properties.clientSecret)
                .username(username).password(password).build();

        AccessTokenResponse tokenResponse = keycloakClient.tokenManager().getAccessToken();
        assertNotNull(tokenResponse);
        return tokenResponse.getToken();
    }
}