aboutsummaryrefslogtreecommitdiffstats
path: root/controlloop/m2/guard/src/test/java/org/onap/policy/guard/PolicyGuardXacmlHelperTest.java
blob: 47af1fbc6a38933402f6ca57bdfb73c1945e77bb (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
/*-
 * ============LICENSE_START=======================================================
 * guard
 * ================================================================================
 * Copyright (C) 2017-2019 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.policy.guard;

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

import java.util.Properties;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
import org.onap.policy.drools.system.PolicyEngineConstants;
import org.onap.policy.drools.utils.logging.LoggerUtil;

public class PolicyGuardXacmlHelperTest {

    private static final String TARGET = "target";
    private static final String REQUEST_ID = "requestId";
    private static final String RECIPE = "recipe";
    private static final String GUARD_URL = "guard.url";
    private static final String ACTOR = "actor";
    private static final Integer VF_COUNT = 100;

    /**
     * Set up test class.
     */
    @BeforeClass
    public static void setupSimulator() throws Exception {
        LoggerUtil.setLevel("ROOT", "INFO");
        LoggerUtil.setLevel("org.eclipse.jetty", "WARN");

        HttpServletServerFactoryInstance.getServerFactory().destroy();
        org.onap.policy.simulators.Util.buildGuardSim();

        //
        // Set guard properties
        //
        org.onap.policy.guard.Util.setGuardEnvProps("http://localhost:6669/policy/pdpx/v1/decision", "python", "test");
    }

    /**
     * Shuts down simulator and performs 1 more test for the case where the connection fails.
     */
    @AfterClass
    public static void tearDownSimulator() {
        HttpServletServerFactoryInstance.getServerFactory().destroy();

        // Null/ Bad Connection Case
        PolicyGuardXacmlRequestAttributes xacmlReq = new PolicyGuardXacmlRequestAttributes(
                        org.onap.policy.simulators.GuardSimulatorJaxRs.DENY_CLNAME, ACTOR, RECIPE, TARGET,
                        REQUEST_ID, VF_COUNT);
        String rawDecision = new PolicyGuardXacmlHelper().callPdp(xacmlReq);
        assertNotNull(rawDecision);
        assertEquals(Util.DENY, rawDecision);
    }

    @Test
    public void testSimulator() {
        PolicyGuardXacmlRequestAttributes request = new PolicyGuardXacmlRequestAttributes("clname_id", "actor_id",
                        "operation_id", "target_id", "request_id", VF_COUNT);
        String xacmlResponse = new PolicyGuardXacmlHelper().callPdp(request);
        assertNotNull(xacmlResponse);
    }

    @Test
    /*
     * Tests PolicyGuardXacmlHelper.callPdp method to determine if it returns DENY, PERMIT, or
     * INDETERMINATE as expected.
     */
    public void testCallPdp() {
        // Deny Case
        PolicyGuardXacmlRequestAttributes xacmlReq = new PolicyGuardXacmlRequestAttributes(
                        org.onap.policy.simulators.GuardSimulatorJaxRs.DENY_CLNAME, ACTOR, RECIPE, TARGET,
                        REQUEST_ID, VF_COUNT);
        String rawDecision = new PolicyGuardXacmlHelper().callPdp(xacmlReq);
        assertNotNull(rawDecision);
        assertEquals(Util.DENY, rawDecision);

        // Permit Case
        xacmlReq = new PolicyGuardXacmlRequestAttributes("clname", ACTOR, RECIPE, TARGET, REQUEST_ID, VF_COUNT);
        rawDecision = new PolicyGuardXacmlHelper().callPdp(xacmlReq);
        assertNotNull(rawDecision);
        assertEquals(Util.PERMIT, rawDecision);

        // Indeterminate case is in tearDown for efficiency
    }

    @Test
    public void testInit() {
        final Properties savedEnvironment = (Properties) PolicyEngineConstants.getManager().getEnvironment().clone();

        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngineConstants.getManager().getEnvironment().setProperty(GUARD_URL,
                "http://localhost:6669/pdp/api/getDecision,Dorothy");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngineConstants.getManager().getEnvironment().setProperty(GUARD_URL,
                "http://localhost:6669/pdp/api/getDecision,Dorothy,Toto");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngineConstants.getManager().getEnvironment().setProperty(GUARD_URL,
                "http://localhost:6969/policy/pdpx/v1/decision");

        PolicyEngineConstants.getManager().getEnvironment().setProperty("pdpx.timeout", "thisIsNotANumber");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngineConstants.getManager().getEnvironment().setProperty("pdpx.timeout", "1000");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngineConstants.getManager().getEnvironment().remove("pdpx.password");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngineConstants.getManager().getEnvironment().setProperty("pdpx.username", "python");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngineConstants.getManager().getEnvironment().setProperty(GUARD_URL, "///");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngineConstants.getManager().getEnvironment().setProperty("guard.disabled", "");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngineConstants.getManager().getEnvironment().setProperty("guard.disabled", "true");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngineConstants.getManager().getEnvironment().clear();
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngineConstants.getManager().setEnvironment(savedEnvironment);
    }
}