aboutsummaryrefslogtreecommitdiffstats
path: root/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/StdA1ClientTest.java
blob: db4c79cd8b0fa7299b7b9eac52934371c47322d5 (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
/*-
 * ========================LICENSE_START=================================
 * ONAP : ccsdk oran
 * ======================================================================
 * Copyright (C) 2019-2020 Nordix Foundation. 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.ccsdk.oran.a1policymanagementservice.clients;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

@ExtendWith(MockitoExtension.class)
class StdA1ClientTest {
    private static final String RIC_URL = "RicUrl";
    private static final String POLICY_TYPE_1_NAME = "type1";
    private static final String POLICY_1_ID = "policy1";
    private static final String POLICY_2_ID = "policy2";
    private static final String POLICY_JSON = "{\"policyId\":\"policy1\"}";
    private static final String POLICY_TYPE = "typeName";

    StdA1ClientVersion1 clientUnderTest;

    @Mock
    AsyncRestClient asyncRestClientMock;

    @Mock
    RicConfig ricConfigMock;

    @BeforeEach
    void init() {
        clientUnderTest = new StdA1ClientVersion1(asyncRestClientMock, ricConfigMock);
    }

    private String policiesUrl() {
        return RIC_URL + "/A1-P/v1/policies";
    }

    private String policiesBaseUrl() {
        return policiesUrl() + "/";
    }

    @Test
    void testGetPolicyTypeIdentities() {
        List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
        assertEquals(1, policyTypeIds.size(), "should hardcoded to one");
        assertEquals("", policyTypeIds.get(0), "should hardcoded to empty");
    }

    @Test
    void testGetPolicyIdentities() {
        doReturn(RIC_URL).when(ricConfigMock).getBaseUrl();
        Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
        when(asyncRestClientMock.get(anyString())).thenReturn(policyIds);

        List<String> result = clientUnderTest.getPolicyIdentities().block();
        assertEquals(2, result.size(), "");

        verify(asyncRestClientMock).get(policiesUrl());
    }

    @Test
    void testGetValidPolicyType() {
        String policyType = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_NAME).block();
        assertEquals("{}", policyType, "");
    }

    @Test
    void testPutPolicyValidResponse() {
        doReturn(RIC_URL).when(ricConfigMock).getBaseUrl();
        when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON));

        Mono<String> policyMono =
                clientUnderTest.putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON, POLICY_TYPE));

        verify(asyncRestClientMock).put(policiesBaseUrl() + POLICY_1_ID, POLICY_JSON);
        StepVerifier.create(policyMono).expectNext(POLICY_JSON).expectComplete().verify();
    }

    @Test
    void testDeletePolicy() {
        doReturn(RIC_URL).when(ricConfigMock).getBaseUrl();
        final String url = policiesBaseUrl() + POLICY_1_ID;
        when(asyncRestClientMock.delete(url)).thenReturn(Mono.empty());

        Policy policy = A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON, POLICY_TYPE);
        Mono<?> responseMono = clientUnderTest.deletePolicy(policy);
        verify(asyncRestClientMock).delete(url);
        StepVerifier.create(responseMono).expectComplete().verify();
    }

    @Test
    void testDeleteAllPolicies() {
        doReturn(RIC_URL).when(ricConfigMock).getBaseUrl();
        Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
        when(asyncRestClientMock.get(policiesUrl())).thenReturn(policyIds);
        when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());

        Flux<String> responseFlux = clientUnderTest.deleteAllPolicies();
        StepVerifier.create(responseFlux).expectComplete().verify();
        verify(asyncRestClientMock).get(policiesUrl());
        verify(asyncRestClientMock).delete(policiesBaseUrl() + POLICY_1_ID);
        verify(asyncRestClientMock).delete(policiesBaseUrl() + POLICY_2_ID);
    }
}