aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org/onap/policy/pdpx/main/XacmlStateTest.java
blob: a1f1b8a7f192f74e7c5dc23a74d30faa285b1d06 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*-
 * ============LICENSE_START=======================================================
 * Copyright (C) 2019, 2021-2022 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2021 Nordix Foundation.
 * Modifications Copyright (C) 2023 Bell Canada.
 * ================================================================================
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.pdpx.main;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
import org.onap.policy.models.pdp.concepts.PdpStateChange;
import org.onap.policy.models.pdp.concepts.PdpStatistics;
import org.onap.policy.models.pdp.concepts.PdpStatus;
import org.onap.policy.models.pdp.concepts.PdpUpdate;
import org.onap.policy.models.pdp.enums.PdpHealthStatus;
import org.onap.policy.models.pdp.enums.PdpResponseStatus;
import org.onap.policy.models.pdp.enums.PdpState;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.onap.policy.pdpx.main.comm.XacmlPdpUpdatePublisher;
import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;

@RunWith(MockitoJUnitRunner.class)
public class XacmlStateTest {
    private static final String PDP_TYPE = "xacml-flavor";
    private static final String GROUP = "my-group";
    private static final String SUBGROUP = "my-subgroup";
    private static final PdpState STATE = PdpState.SAFE;

    @Mock
    private XacmlPdpApplicationManager appmgr;

    @Mock
    private XacmlPdpActivator act;

    private String pdpName;

    private XacmlState state;

    /**
     * Initializes objects, including the state.
     */
    @Before
    public void setUp() {
        pdpName = XacmlState.PDP_NAME;

        XacmlPdpActivator.setCurrent(act);
        state = new XacmlState(appmgr, GROUP, PDP_TYPE);
    }

    @AfterClass
    public static void tearDownAfterClass() {
        XacmlPdpActivator.setCurrent(null);
    }

    @Test
    public void testShouldHandle() {
        PdpUpdate msg = new PdpUpdate();
        assertFalse(state.shouldHandle(msg));

        msg.setName(XacmlState.PDP_NAME);
        assertTrue(state.shouldHandle(msg));
    }

    @Test
    public void testGenHeartbeat() {
        // not healthy
        PdpStatus status = state.genHeartbeat();
        assertEquals(PdpHealthStatus.NOT_HEALTHY, status.getHealthy());
        assertEquals(pdpName, status.getName());
        assertEquals(GROUP, status.getPdpGroup());
        assertEquals(PDP_TYPE, status.getPdpType());
        assertEquals(PdpState.PASSIVE, status.getState());
        assertTrue(status.getPolicies().isEmpty());

        // healthy
        when(act.isAlive()).thenReturn(true);

        status = state.genHeartbeat();
        assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy());
    }

    @Test
    public void testGetStatistics() {
        XacmlPdpStatisticsManager statmgr = new XacmlPdpStatisticsManager();
        XacmlPdpStatisticsManager.setCurrent(statmgr);

        ToscaPolicy policy1 = mock(ToscaPolicy.class);
        ToscaPolicy policy2 = mock(ToscaPolicy.class);
        ToscaConceptIdentifier ident = new ToscaConceptIdentifier("undeployed", "2.3.4");
        when(policy2.getIdentifier()).thenReturn(ident);

        PdpUpdate message = new PdpUpdate();
        message.setPoliciesToBeDeployed(Arrays.asList(policy1));
        message.setPoliciesToBeUndeployed(Arrays.asList(policy2.getIdentifier()));

        TopicSinkClient client = Mockito.mock(TopicSinkClient.class);
        XacmlPdpUpdatePublisher publisher = new XacmlPdpUpdatePublisher(client, state, appmgr);
        publisher.handlePdpUpdate(message);

        PdpStatistics stats = state.getStatistics();
        assertNotNull(stats);
        assertEquals(GROUP, stats.getPdpGroupName());
        assertEquals(1, stats.getPolicyDeployCount());
        assertEquals(1, stats.getPolicyDeploySuccessCount());
        assertEquals(0, stats.getPolicyDeployFailCount());
        assertEquals(1, stats.getPolicyUndeployCount());
        assertEquals(1, stats.getPolicyUndeployFailCount());
        assertEquals(0, stats.getPolicyUndeploySuccessCount());

        PdpStatistics test = new PdpStatistics();
        test.setTimeStamp(stats.getTimeStamp());
        test.setPdpGroupName(GROUP);
        test.setPolicyDeployCount(1);
        test.setPolicyDeploySuccessCount(1);
        test.setPolicyUndeployCount(1);
        test.setPolicyUndeployFailCount(1);

        assertEquals(stats.toString(), test.toString());
    }

    @Test
    public void testUpdateInternalStatePdpStateChange() {
        PdpStateChange req = new PdpStateChange();
        req.setName(pdpName);
        req.setPdpGroup("wrong-pdp-group");
        req.setPdpSubgroup(SUBGROUP);
        req.setState(STATE);

        PdpStatus status = state.updateInternalState(req);
        assertEquals(PdpState.SAFE, status.getState());
        assertEquals(GROUP, status.getPdpGroup());

        PdpResponseDetails resp = status.getResponse();
        assertNotNull(resp);
        assertEquals(req.getRequestId(), resp.getResponseTo());
        assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());

        // ensure info was saved
        status = state.genHeartbeat();
        assertEquals(PdpState.SAFE, status.getState());

        req.setState(PdpState.ACTIVE);
        status = state.updateInternalState(req);
        assertEquals(PdpState.ACTIVE, status.getState());
        verify(act).enableApi();

        req.setState(PdpState.PASSIVE);
        status = state.updateInternalState(req);
        assertEquals(PdpState.PASSIVE, status.getState());
        verify(act).disableApi();
    }

    @Test
    public void testUpdateInternalStatePdpUpdate() {
        PdpUpdate req = new PdpUpdate();
        req.setPdpGroup("wrong-pdp-group");
        req.setPdpSubgroup(SUBGROUP);

        PdpStatus status = state.updateInternalState(req, "");

        PdpResponseDetails resp = status.getResponse();
        assertNotNull(resp);
        assertEquals(req.getRequestId(), resp.getResponseTo());
        assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
        assertNull(resp.getResponseMessage());

        // ensure info was saved
        status = state.genHeartbeat();
        assertEquals(GROUP, status.getPdpGroup());
        assertEquals(SUBGROUP, status.getPdpSubgroup());

        status = state.updateInternalState(req, "Failed to load policy: failLoadPolicy1: null");
        assertEquals("Failed to load policy: failLoadPolicy1: null", status.getResponse().getResponseMessage());
        assertEquals(PdpResponseStatus.FAIL, status.getResponse().getResponseStatus());
        assertEquals(GROUP, status.getPdpGroup());
    }

    @Test
    public void testTerminatePdpMessage() {
        PdpStatus status = state.terminatePdpMessage();
        assertEquals(PdpState.TERMINATED, status.getState());
    }
}