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;
26 import java.io.IOException;
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.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.TemporaryFolder;
37 import org.onap.policy.rest.adapter.PolicyRestAdapter;
38 import org.onap.policy.rest.dao.PolicyDBException;
39 import org.onap.policy.rest.jpa.GroupEntity;
40 import org.onap.policy.rest.jpa.PdpEntity;
41 import org.onap.policy.xacml.std.pap.StdEngine;
43 public class HandleIncomingNotificationsTest {
44 private static final String API = "API";
45 private static final String TWO = "2";
46 private static final String PDP = "pdp";
47 private static final String GROUP = "group";
48 private static final String ONE = "1";
49 private static PolicyDBDao dbd;
50 private static StdEngine stdEngine = null;
51 private static SessionFactory sessionFactory = null;
52 private static HandleIncomingNotifications handleIncomingNotifications;
53 private static GroupEntity groupEntity;
55 public TemporaryFolder folder = new TemporaryFolder();
58 * Sets the up before class.
60 * @throws Exception the exception
63 public static void setUpBeforeClass() throws Exception {
64 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pap.properties");
65 sessionFactory = PolicyDBDaoTest.setupH2DbDaoImpl("testHandleIncoming");
66 handleIncomingNotifications = new HandleIncomingNotifications(sessionFactory);
67 PolicyDBDao.setJunit(true);
68 dbd = PolicyDBDao.getPolicyDBDaoInstance();
69 PolicyDBDao.setJunit(true);
76 * @throws IOException Signals that an I/O exception has occurred.
77 * @throws PAPException the PAP exception
80 public void setup() throws IOException, PAPException {
81 File tmpGrpDir = folder.newFolder("src", "test", "resources", "pdps", "grpTest");
82 stdEngine = new StdEngine(tmpGrpDir.toPath());
83 dbd.setPapEngine(stdEngine);
87 public void testHandleIncomingHttpNotification() {
88 handleIncomingNotifications.handleIncomingHttpNotification(null, ONE, GROUP, null, null);
89 handleIncomingNotifications.handleIncomingHttpNotification(null, ONE, GROUP, null, null);
90 handleIncomingNotifications.handleIncomingHttpNotification(null, ONE, "policy", null, null);
91 handleIncomingNotifications.handleIncomingHttpNotification(null, ONE, PDP, null, null);
92 populatePdpEntityDb(TWO, groupEntity);
93 handleIncomingNotifications.handleIncomingHttpNotification(null, TWO, PDP, null, null);
96 private static void populateDb() throws PolicyDBException, IOException {
97 groupEntity = new GroupEntity();
98 groupEntity.setCreatedBy(API);
99 groupEntity.setDefaultGroup(false);
100 groupEntity.setDeleted(false);
101 groupEntity.setDescription("a test group");
102 groupEntity.setGroupId(ONE);
103 groupEntity.setGroupName(ONE);
104 groupEntity.prePersist();
105 Session session = sessionFactory.openSession();
106 session.getTransaction().begin();
107 session.persist(groupEntity);
108 session.getTransaction().commit();
110 populatePdpEntityDb(ONE, groupEntity);
111 populatePolicyInDb();
114 private static void populatePdpEntityDb(String pdpId, GroupEntity groupEntity) {
115 PdpEntity pdpEntity = new PdpEntity();
116 pdpEntity.setCreatedBy(API);
117 pdpEntity.setDeleted(false);
118 pdpEntity.setDescription("test pdp");
119 pdpEntity.setGroup(groupEntity);
120 pdpEntity.setJmxPort(9993);
121 pdpEntity.setModifiedBy(API);
122 pdpEntity.setPdpId(pdpId);
123 pdpEntity.setPdpName("grouptest");
124 pdpEntity.prePersist();
126 Session session = sessionFactory.openSession();
127 session.getTransaction().begin();
128 session.persist(pdpEntity);
129 session.getTransaction().commit();
133 private static void populatePolicyInDb() throws PolicyDBException, IOException {
134 Policy policyObject = new ConfigPolicy();
135 policyObject.policyAdapter = new PolicyRestAdapter();
136 policyObject.policyAdapter.setConfigName("testpolicyhandle");
137 policyObject.policyAdapter.setPolicyDescription("my description");
138 policyObject.policyAdapter.setConfigBodyData("this is my test config file");
139 policyObject.policyAdapter.setPolicyName("SampleTest1206");
140 policyObject.policyAdapter.setConfigType(ConfigPolicy.OTHER_CONFIG);
141 policyObject.policyAdapter.setPolicyType("Config");
142 policyObject.policyAdapter.setDomainDir("com");
143 policyObject.policyAdapter.setVersion(ONE);
144 policyObject.policyAdapter.setHighestVersion(1);
145 PolicyType policyTypeObject = new PolicyType();
146 policyObject.policyAdapter.setPolicyData(policyTypeObject);
148 PolicyType policyConfig = new PolicyType();
149 policyConfig.setVersion(ONE);
150 policyConfig.setPolicyId("");
151 policyConfig.setTarget(new TargetType());
152 policyObject.policyAdapter.setData(policyConfig);
153 ClassLoader classLoader = HandleIncomingNotificationsTest.class.getClassLoader();
154 policyObject.policyAdapter
155 .setParentPath(IOUtils.toString(classLoader.getResourceAsStream("Config_SampleTest1206.1.xml")));
157 PolicyDBDaoTransaction transaction = dbd.getNewTransaction();
158 transaction.createPolicy(policyObject, "testuser1");
159 transaction.commitTransaction();