9efb187cde5694e1945a22fd690519d9fe2b03f3
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / components / HandleIncomingNotificationsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest.components;
22
23 import com.att.research.xacml.api.pap.PAPException;
24 import com.att.research.xacml.util.XACMLProperties;
25
26 import java.io.File;
27 import java.io.IOException;
28
29 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
30 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
31
32 import org.apache.commons.io.IOUtils;
33 import org.hibernate.Session;
34 import org.hibernate.SessionFactory;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.TemporaryFolder;
40 import org.onap.policy.rest.adapter.PolicyRestAdapter;
41 import org.onap.policy.rest.dao.PolicyDBException;
42 import org.onap.policy.rest.jpa.GroupEntity;
43 import org.onap.policy.rest.jpa.PdpEntity;
44 import org.onap.policy.xacml.std.pap.StdEngine;
45
46 public class HandleIncomingNotificationsTest {
47     private static final String API = "API";
48     private static final String TWO = "2";
49     private static final String PDP = "pdp";
50     private static final String GROUP = "group";
51     private static final String ONE = "1";
52     private static PolicyDBDao dbd;
53     private static StdEngine stdEngine = null;
54     private static SessionFactory sessionFactory = null;
55     private static HandleIncomingNotifications handleIncomingNotifications;
56     private static GroupEntity groupEntity;
57     @Rule
58     public TemporaryFolder folder = new TemporaryFolder();
59
60     /**
61      * Sets the up before class.
62      *
63      * @throws Exception the exception
64      */
65     @BeforeClass
66     public static void setUpBeforeClass() throws Exception {
67         System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pap.properties");
68         sessionFactory = PolicyDBDaoTest.setupH2DbDaoImpl("testHandleIncoming");
69         handleIncomingNotifications = new HandleIncomingNotifications(sessionFactory);
70         PolicyDBDao.setJunit(true);
71         dbd = PolicyDBDao.getPolicyDBDaoInstance();
72         PolicyDBDao.setJunit(true);
73         populateDb();
74     }
75
76     /**
77      * Setup.
78      *
79      * @throws IOException Signals that an I/O exception has occurred.
80      * @throws PAPException the PAP exception
81      */
82     @Before
83     public void setup() throws IOException, PAPException {
84         File tmpGrpDir = folder.newFolder("src", "test", "resources", "pdps", "grpTest");
85         stdEngine = new StdEngine(tmpGrpDir.toPath());
86         dbd.setPapEngine(stdEngine);
87     }
88
89     @Test
90     public void testHandleIncomingHttpNotification() {
91         handleIncomingNotifications.handleIncomingHttpNotification(null, ONE, GROUP, null, null);
92         handleIncomingNotifications.handleIncomingHttpNotification(null, ONE, GROUP, null, null);
93         handleIncomingNotifications.handleIncomingHttpNotification(null, ONE, "policy", null, null);
94         handleIncomingNotifications.handleIncomingHttpNotification(null, ONE, PDP, null, null);
95         populatePdpEntityDb(TWO, groupEntity);
96         handleIncomingNotifications.handleIncomingHttpNotification(null, TWO, PDP, null, null);
97     }
98
99     private static void populateDb() throws PolicyDBException, IOException {
100         groupEntity = new GroupEntity();
101         groupEntity.setCreatedBy(API);
102         groupEntity.setDefaultGroup(false);
103         groupEntity.setDeleted(false);
104         groupEntity.setDescription("a test group");
105         groupEntity.setGroupId(ONE);
106         groupEntity.setGroupName(ONE);
107         groupEntity.prePersist();
108         Session session = sessionFactory.openSession();
109         session.getTransaction().begin();
110         session.persist(groupEntity);
111         session.getTransaction().commit();
112         session.close();
113         populatePdpEntityDb(ONE, groupEntity);
114         populatePolicyInDb();
115     }
116
117     private static void populatePdpEntityDb(String pdpId, GroupEntity groupEntity) {
118         PdpEntity pdpEntity = new PdpEntity();
119         pdpEntity.setCreatedBy(API);
120         pdpEntity.setDeleted(false);
121         pdpEntity.setDescription("test pdp");
122         pdpEntity.setGroup(groupEntity);
123         pdpEntity.setJmxPort(9993);
124         pdpEntity.setModifiedBy(API);
125         pdpEntity.setPdpId(pdpId);
126         pdpEntity.setPdpName("grouptest");
127         pdpEntity.prePersist();
128
129         Session session = sessionFactory.openSession();
130         session.getTransaction().begin();
131         session.persist(pdpEntity);
132         session.getTransaction().commit();
133         session.close();
134     }
135
136     private static void populatePolicyInDb() throws PolicyDBException, IOException {
137         Policy policyObject = new ConfigPolicy();
138         policyObject.policyAdapter = new PolicyRestAdapter();
139         policyObject.policyAdapter.setConfigName("testpolicyhandle");
140         policyObject.policyAdapter.setPolicyDescription("my description");
141         policyObject.policyAdapter.setConfigBodyData("this is my test config file");
142         policyObject.policyAdapter.setPolicyName("SampleTest1206");
143         policyObject.policyAdapter.setConfigType(ConfigPolicy.OTHER_CONFIG);
144         policyObject.policyAdapter.setPolicyType("Config");
145         policyObject.policyAdapter.setDomainDir("com");
146         policyObject.policyAdapter.setVersion(ONE);
147         policyObject.policyAdapter.setHighestVersion(1);
148         PolicyType policyTypeObject = new PolicyType();
149         policyObject.policyAdapter.setPolicyData(policyTypeObject);
150
151         PolicyType policyConfig = new PolicyType();
152         policyConfig.setVersion(ONE);
153         policyConfig.setPolicyId("");
154         policyConfig.setTarget(new TargetType());
155         policyObject.policyAdapter.setData(policyConfig);
156         ClassLoader classLoader = HandleIncomingNotificationsTest.class.getClassLoader();
157         policyObject.policyAdapter
158                 .setParentPath(IOUtils.toString(classLoader.getResourceAsStream("Config_SampleTest1206.1.xml")));
159
160         PolicyDBDaoTransaction transaction = dbd.getNewTransaction();
161         transaction.createPolicy(policyObject, "testuser1");
162         transaction.commitTransaction();
163
164     }
165 }