summaryrefslogtreecommitdiffstats
path: root/aai-traversal/src/it/java/org/onap/aai/multitenancy/MultiTenancyIT.java
blob: e34ac2bec4649e3721b053ca2d9c0648300b16f6 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
 * ============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.multitenancy;

import com.jayway.jsonpath.JsonPath;
import dasniko.testcontainers.keycloak.KeycloakContainer;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.janusgraph.core.JanusGraphTransaction;
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.dbmap.AAIGraph;
import org.onap.aai.rest.AbstractSpringRestTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.http.*;
import org.springframework.test.context.TestPropertySource;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;

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

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

    @Override
    public void createTestGraph() {
        JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction();
        boolean success = true;

        try {
            GraphTraversalSource g = transaction.traversal();

            g.addV().property("aai-node-type", "pnf")
                    .property("pnf-name", "test-pnf-name-01")
                    .property("prov-status", "in_service")
                    .property("data-owner", "operator")
                    .property("in-maint", false)
                    .property("source-of-truth", "JUNIT")
                    .property("aai-uri", "/network/pnfs/pnf/test-pnf-name-01").next();

            g.addV().property("aai-node-type", "pnf")
                    .property("pnf-name", "test-pnf-name-02")
                    .property("prov-status", "in_service")
                    .property("in-maint", false)
                    .property("source-of-truth", "JUNIT")
                    .property("aai-uri", "/network/pnfs/pnf/test-pnf-name-02").next();

            g.addV().property("aai-node-type", "pnf")
                    .property("pnf-name", "test-pnf-name-03")
                    .property("prov-status", "in_service")
                    .property("data-owner", "selector")
                    .property("in-maint", false)
                    .property("source-of-truth", "JUNIT")
                    .property("aai-uri", "/network/pnfs/pnf/test-pnf-name-03").next();

            g.addV().property("aai-node-type", "pnf")
                    .property("pnf-name", "test-pnf-name-04")
                    .property("prov-status", "in_service")
                    .property("data-owner", "selector")
                    .property("in-maint", false)
                    .property("source-of-truth", "JUNIT")
                    .property("aai-uri", "/network/pnfs/pnf/test-pnf-name-04").next();

            g.addV().property("aai-node-type", "pnf")
                    .property("pnf-name", "test-pnf-name-05")
                    .property("prov-status", "in_service")
                    .property("data-owner", "selector")
                    .property("in-maint", false)
                    .property("source-of-truth", "JUNIT")
                    .property("aai-uri", "/network/pnfs/pnf/test-pnf-name-05").next();
        } catch (Exception ex) {
            success = false;
        } finally {
            if (success) {
                transaction.commit();
            } else {
                transaction.rollback();
                fail("Unable to setup the graph");
            }
        }
    }

    @Test
    public void testDslQueryWithDataOwner() throws Exception {
        baseUrl = "http://localhost:" + randomPort;
        String endpoint = baseUrl + "/aai/v23/dsl?format=console";
        List<Object> queryResults = null;
        ResponseEntity responseEntity = null;

        Map<String, String> dslQueryMap = new HashMap<>();
        dslQueryMap.put("dsl-query", "pnf*('prov-status','in_service') ");
        String payload = PayloadUtil.getTemplatePayload("dsl-query.json", dslQueryMap);

        // get pnf with ran (operator)
        String username = "ran", password = "ran";
        headers = this.getHeaders(username, password);
        httpEntity = new HttpEntity(payload, headers);
        responseEntity = restTemplate.exchange(endpoint, HttpMethod.PUT, httpEntity, String.class);
        queryResults = JsonPath.read(responseEntity.getBody().toString(), "$.results");
        assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
        assertEquals(queryResults.size(), 2);

        // get pnf with bob (operator_readOnly)
        username = "bob"; password = "bob";
        headers = this.getHeaders(username, password);
        httpEntity = new HttpEntity(payload, headers);
        responseEntity = restTemplate.exchange(endpoint, HttpMethod.PUT, httpEntity, String.class);
        queryResults = JsonPath.read(responseEntity.getBody().toString(), "$.results");
        assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
        assertEquals(queryResults.size(), 2);

        // get pnf with ted (selector)
        username = "ted"; password = "ted";
        headers = this.getHeaders(username, password);
        httpEntity = new HttpEntity(payload, headers);
        responseEntity = restTemplate.exchange(endpoint, HttpMethod.PUT, httpEntity, String.class);
        queryResults = JsonPath.read(responseEntity.getBody().toString(), "$.results");
        assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
        assertEquals(queryResults.size(), 4);

        // add role to ted and try to get pnf again
        roleHandler.addToUser(RoleHandler.OPERATOR, username);
        headers = this.getHeaders(username, password);
        httpEntity = new HttpEntity(payload, headers);
        responseEntity = restTemplate.exchange(endpoint, HttpMethod.PUT, httpEntity, String.class);
        queryResults = JsonPath.read(responseEntity.getBody().toString(), "$.results");
        assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
        assertEquals(queryResults.size(), 5);
    }

    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();
    }
}