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