aboutsummaryrefslogtreecommitdiffstats
path: root/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/RESTfulPAPEngineTest.java
blob: be248598f3d8834e4d7ee0c08c3c5530ea38035a (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP Policy Engine
 * ================================================================================
 * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Modifications Copyright (C) 2019 Samsung
 * ================================================================================
 * 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.admin;

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

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URLConnection;
import javax.servlet.http.HttpServletResponse;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;
import org.onap.policy.rest.adapter.PolicyRestAdapter;
import org.onap.policy.xacml.api.pap.OnapPDP;
import org.onap.policy.xacml.api.pap.OnapPDPGroup;
import com.att.research.xacml.api.pap.PAPException;
import com.att.research.xacml.api.pap.PDPPolicy;
import com.att.research.xacml.util.XACMLProperties;

public class RESTfulPAPEngineTest {
    @Rule
    public ExpectedException thrown = ExpectedException.none();

    private RESTfulPAPEngine engine = null;
    private String name = "testName";
    private String id = "testID";
    private String description = "testDescription";
    private String policyType = "testType";
    private String policyContent = "testContent";
    private int jmxport = 0;
    OnapPDPGroup group = Mockito.mock(OnapPDPGroup.class);
    OnapPDPGroup newGroup = Mockito.mock(OnapPDPGroup.class);
    OnapPDP pdp = Mockito.mock(OnapPDP.class);
    InputStream policy;

    @BeforeClass
    public static void setUpBeforeClass() {
        XACMLProperties.reloadProperties();
    }

    @AfterClass
    public static void tearDownAfterClass() {
        XACMLProperties.reloadProperties();
    }

    @Before
    public void runConstructor() throws Exception {
        // Mock connection
        HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
        Mockito.when(connection.getResponseCode()).thenReturn(HttpServletResponse.SC_NO_CONTENT);

        // Set the system property temporarily
        String systemKey = "xacml.properties";
        String oldProperty = System.getProperty(systemKey);
        System.setProperty(systemKey, "src/test/resources/xacml.admin.properties");

        // Test constructor
        String urlName = "localhost:1234";
        engine = new RESTfulPAPEngine(urlName) {
            @Override
            protected URLConnection makeConnection(String fullURL) throws IOException {
                return connection;
            }
        };

        // Initialize policy
        policy = new ByteArrayInputStream(policyContent.getBytes("UTF-8"));

        // Restore the original system property
        if (oldProperty != null) {
            System.setProperty(systemKey, oldProperty);
        } else {
            System.clearProperty(systemKey);
        }
    }

    @Test
    public void testGroups() throws Exception {
        engine.setDefaultGroup(group);
        assertEquals(engine.getDefaultGroup(), null);
        engine.newGroup(name, description);
        engine.removeGroup(group, newGroup);
        assertEquals(engine.getPDPGroup(pdp), null);
        assertEquals(engine.getPDPGroup(id), null);
        assertEquals(engine.getPDP(id), null);
        assertEquals(engine.getStatus(pdp), null);

        thrown.expect(NullPointerException.class);
        engine.getOnapPDPGroups();
        fail("Expecting an exception.");
    }

    @Test
    public void testUpdateGroup() throws PAPException {
        thrown.expect(PAPException.class);
        engine.updateGroup(group);
        fail("Expecting an exception.");
    }

    @Test
    public void testPDP() throws PAPException {
        assertEquals(engine.getGroup(name), null);
        engine.movePDP(pdp, newGroup);

        thrown.expect(PAPException.class);
        engine.newPDP(id, newGroup, name, description, jmxport);
        fail("Expecting an exception.");
    }

    @Test
    public void testUpdatePDP() throws PAPException {
        thrown.expect(NullPointerException.class);
        engine.updatePDP(pdp);
        fail("Expecting an exception.");
    }

    @Test
    public void testRemovePDP() throws PAPException {
        thrown.expect(NullPointerException.class);
        engine.removePDP(pdp);
        fail("Expecting an exception.");
    }

    @Test
    public void testValidatePolicy() throws PAPException {
        PolicyRestAdapter policyAdapter = new PolicyRestAdapter();

        thrown.expect(PAPException.class);
        engine.validatePolicyRequest(policyAdapter, policyType);
        fail("Expecting an exception.");
    }

    @Test
    public void testPublishPolicy() throws PAPException {
        thrown.expect(PAPException.class);
        engine.publishPolicy(id, name, false, policy, newGroup);
        fail("Expecting an exception.");
    }

    @Test
    public void testCopy() throws PAPException {
        engine.copyFile(id, newGroup, policy);
        PDPPolicy pdpPolicy = Mockito.mock(PDPPolicy.class);

        thrown.expect(PAPException.class);
        engine.copyPolicy(pdpPolicy, newGroup);
        fail("Expecting an exception.");
    }
}