aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupQueryTest.java
blob: d6472ce1b4e01fa94b09932cd645a3fbdaeb4bd4 (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
/*
 * ============LICENSE_START=======================================================
 * ONAP PAP
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2021 Nordix Foundation.
 * Modifications Copyright (C) 2022 Bell Canada. 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.policy.pap.main.rest.e2e;

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

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.Response;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.models.pdp.concepts.Pdp;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpGroups;
import org.onap.policy.models.pdp.concepts.PdpSubGroup;
import org.onap.policy.models.pdp.enums.PdpHealthStatus;
import org.onap.policy.models.pdp.enums.PdpState;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;

public class PdpGroupQueryTest extends End2EndBase {
    private static final String GROUP_ENDPOINT = "pdps";

    /**
     * Sets up.
     *
     * @throws Exception the exception
     */
    @Override
    @Before
    public void setUp() throws Exception {

        addToscaPolicyTypes("monitoring.policy-type.yaml");
        addToscaPolicies("monitoring.policy.yaml");

        addGroups("queryGroup.json");
        super.setUp();
    }

    @Test
    public void test() throws Exception {
        Invocation.Builder invocationBuilder = sendRequest(GROUP_ENDPOINT);
        Response rawresp = invocationBuilder.get();
        PdpGroups resp = rawresp.readEntity(PdpGroups.class);
        assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());

        assertEquals("[queryGroup1, queryGroup2]", mapList(resp.getGroups(), PdpGroup::getName).toString());

        checkGroup1(resp.getGroups().get(0));
        checkGroup2(resp.getGroups().get(1));
    }

    private void checkGroup1(PdpGroup group) {
        assertEquals("[pdpTypeA, pdpTypeB]", mapList(group.getPdpSubgroups(), PdpSubGroup::getPdpType).toString());

        assertEquals("my description", group.getDescription());
        assertEquals(PdpState.PASSIVE, group.getPdpGroupState());

        Map<String, Object> props = new LinkedHashMap<>();
        props.put("abc", "def");
        assertEquals(props.toString(), group.getProperties().toString());

        checkSubGroup11(group.getPdpSubgroups().get(0));
    }

    private void checkSubGroup11(PdpSubGroup subgrp) {
        assertEquals(3, subgrp.getCurrentInstanceCount());
        assertEquals(2, subgrp.getDesiredInstanceCount());
        assertEquals("[pdpAA_1, pdpAA_2]", mapList(subgrp.getPdpInstances(), Pdp::getInstanceId).toString());
        assertEquals(2, filterList(subgrp.getPdpInstances(), pdp -> pdp.getPdpState() == PdpState.PASSIVE).size());
        assertEquals(2, filterList(subgrp.getPdpInstances(), pdp -> pdp.getHealthy() == PdpHealthStatus.HEALTHY)
                        .size());
        assertEquals("pdpTypeA", subgrp.getPdpType());
        assertEquals("[onap.restart.tca]", mapList(subgrp.getPolicies(), ToscaConceptIdentifier::getName).toString());
        assertEquals("[1.0.0]", mapList(subgrp.getPolicies(), ToscaConceptIdentifier::getVersion).toString());

        Map<String, Object> props = new LinkedHashMap<>();
        props.put("ten", 10);
        assertEquals(props.toString(), subgrp.getProperties().toString());

        assertEquals("[onap.policies.monitoring.cdap.tca.hi.lo.app]",
                        mapList(subgrp.getSupportedPolicyTypes(), ToscaConceptIdentifier::getName).toString());
        assertEquals("[1.0.0]",
                        mapList(subgrp.getSupportedPolicyTypes(), ToscaConceptIdentifier::getVersion).toString());
    }

    private void checkGroup2(PdpGroup group) {
        assertEquals("[pdpTypeA]", mapList(group.getPdpSubgroups(), PdpSubGroup::getPdpType).toString());

        assertEquals(PdpState.ACTIVE, group.getPdpGroupState());
        assertTrue(group.getProperties() == null || group.getProperties().isEmpty());
    }

    private <T, R> List<R> mapList(List<T> list, Function<T, R> mapFunc) {
        return list.stream().map(mapFunc).collect(Collectors.toList());
    }

    private <T> List<T> filterList(List<T> list, Predicate<T> pred) {
        return list.stream().filter(pred).collect(Collectors.toList());
    }
}