Merge "New junits and bug fixes"
[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 static org.junit.Assert.fail;
24 import com.att.research.xacml.util.XACMLProperties;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
28 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
29 import org.apache.commons.io.IOUtils;
30 import org.hibernate.Session;
31 import org.hibernate.SessionFactory;
32 import org.junit.Assert;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.rest.adapter.PolicyRestAdapter;
36 import org.onap.policy.rest.jpa.GroupEntity;
37 import org.onap.policy.rest.jpa.PdpEntity;
38 import org.onap.policy.xacml.std.pap.StdEngine;
39
40 public class HandleIncomingNotificationsTest {
41     private static PolicyDBDao dbd;
42     private static Path repository;
43     private static StdEngine stdEngine = null;
44     private static SessionFactory sessionFactory = null;
45     private static HandleIncomingNotifications handleIncomingNotifications;
46     private static GroupEntity groupEntity;
47
48     /**
49      * Sets the up before class.
50      *
51      * @throws Exception the exception
52      */
53     @BeforeClass
54     public static void setUpBeforeClass() throws Exception {
55         System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pap.properties");
56         try {
57             sessionFactory = PolicyDBDaoTest.setupH2DbDaoImpl("testHandleIncoming");
58             handleIncomingNotifications = new HandleIncomingNotifications(sessionFactory);
59         } catch (Exception e) {
60             Assert.fail();
61         }
62
63         PolicyDBDao.setJunit(true);
64         dbd = PolicyDBDao.getPolicyDBDaoInstance();
65         PolicyDBDao.setJunit(true);
66         repository = Paths.get("src/test/resources/pdps");
67         stdEngine = new StdEngine(repository);
68         dbd.setPapEngine(stdEngine);
69         populateDb();
70     }
71
72
73     @Test
74     public void testHandleIncomingHttpNotification() {
75         handleIncomingNotifications.handleIncomingHttpNotification(null, "1", "group", null, null);
76         handleIncomingNotifications.handleIncomingHttpNotification(null, "1", "group", null, null);
77         handleIncomingNotifications.handleIncomingHttpNotification(null, "1", "policy", null, null);
78         handleIncomingNotifications.handleIncomingHttpNotification(null, "1", "pdp", null, null);
79         populatePdpEntityDb("2", groupEntity);
80         handleIncomingNotifications.handleIncomingHttpNotification(null, "2", "pdp", null, null);
81     }
82
83     private static void populateDb() {
84         groupEntity = new GroupEntity();
85         groupEntity.setCreatedBy("API");
86         groupEntity.setDefaultGroup(false);
87         groupEntity.setDeleted(false);
88         groupEntity.setDescription("a test group");
89         groupEntity.setGroupId("1");
90         groupEntity.setGroupName("1");
91         groupEntity.prePersist();
92         Session session = sessionFactory.openSession();
93         session.getTransaction().begin();
94         session.persist(groupEntity);
95         session.getTransaction().commit();
96         session.close();
97         populatePdpEntityDb("1", groupEntity);
98         populatePolicyInDb();
99     }
100
101     private static void populatePdpEntityDb(String pdpId, GroupEntity groupEntity) {
102         PdpEntity pdpEntity = new PdpEntity();
103         pdpEntity.setCreatedBy("API");
104         pdpEntity.setDeleted(false);
105         pdpEntity.setDescription("test pdp");
106         pdpEntity.setGroup(groupEntity);
107         pdpEntity.setJmxPort(9993);
108         pdpEntity.setModifiedBy("API");
109         pdpEntity.setPdpId(pdpId);
110         pdpEntity.setPdpName("grouptest");
111         pdpEntity.prePersist();
112
113         Session session = sessionFactory.openSession();
114         session.getTransaction().begin();
115         session.persist(pdpEntity);
116         session.getTransaction().commit();
117         session.close();
118     }
119
120     private static void populatePolicyInDb() {
121         Policy policyObject = new ConfigPolicy();
122         policyObject.policyAdapter = new PolicyRestAdapter();
123         policyObject.policyAdapter.setConfigName("testpolicyhandle");
124         policyObject.policyAdapter.setPolicyDescription("my description");
125         policyObject.policyAdapter.setConfigBodyData("this is my test config file");
126         policyObject.policyAdapter.setPolicyName("SampleTest1206");
127         policyObject.policyAdapter.setConfigType(ConfigPolicy.OTHER_CONFIG);
128         policyObject.policyAdapter.setPolicyType("Config");
129         policyObject.policyAdapter.setDomainDir("com");
130         policyObject.policyAdapter.setVersion("1");
131         policyObject.policyAdapter.setHighestVersion(1);
132         PolicyType policyTypeObject = new PolicyType();
133         policyObject.policyAdapter.setPolicyData(policyTypeObject);
134         ClassLoader classLoader = HandleIncomingNotificationsTest.class.getClassLoader();
135         PolicyType policyConfig = new PolicyType();
136         policyConfig.setVersion("1");
137         policyConfig.setPolicyId("");
138         policyConfig.setTarget(new TargetType());
139         policyObject.policyAdapter.setData(policyConfig);
140         try {
141             policyObject.policyAdapter
142                     .setParentPath(IOUtils.toString(classLoader.getResourceAsStream("Config_SampleTest1206.1.xml")));
143         } catch (Exception e2) {
144             fail();
145         }
146
147         PolicyDBDaoTransaction transaction = dbd.getNewTransaction();
148         try {
149             transaction.createPolicy(policyObject, "testuser1");
150             transaction.commitTransaction();
151         } catch (Exception e) {
152             transaction.rollbackTransaction();
153             Assert.fail();
154         }
155     }
156 }