2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.xacml.rest.components;
23 import com.att.research.xacml.api.pap.PAPException;
24 import com.att.research.xacml.util.XACMLProperties;
27 import java.io.IOException;
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;
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;
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;
58 public TemporaryFolder folder = new TemporaryFolder();
61 * Sets the up before class.
63 * @throws Exception the exception
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);
79 * @throws IOException Signals that an I/O exception has occurred.
80 * @throws PAPException the PAP exception
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);
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);
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();
113 populatePdpEntityDb(ONE, groupEntity);
114 populatePolicyInDb();
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();
129 Session session = sessionFactory.openSession();
130 session.getTransaction().begin();
131 session.persist(pdpEntity);
132 session.getTransaction().commit();
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);
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")));
160 PolicyDBDaoTransaction transaction = dbd.getNewTransaction();
161 transaction.createPolicy(policyObject, "testuser1");
162 transaction.commitTransaction();