Consolidate PolicyRestAdapter setup
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / elk / ElasticSearchPolicyUpdateTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2018-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.elk;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.IOException;
26 import java.sql.SQLException;
27 import java.util.Date;
28
29 import javax.servlet.ServletException;
30
31 import org.apache.commons.io.IOUtils;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.common.logging.flexlogger.FlexLogger;
36 import org.onap.policy.common.logging.flexlogger.Logger;
37 import org.onap.policy.pap.test.XACMLPAPTest;
38 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
39 import org.onap.policy.pap.xacml.rest.elk.client.ElasticSearchPolicyUpdate;
40 import org.onap.policy.rest.jpa.ConfigurationDataEntity;
41 import org.onap.policy.rest.jpa.PolicyEntity;
42
43 public class ElasticSearchPolicyUpdateTest {
44
45     private static Logger logger = FlexLogger.getLogger(ElasticSearchPolicyUpdateTest.class);
46     private Object policyContent = "";
47     private XACMLPAPTest papTest;
48
49     @Before
50     public void setUp() throws IOException, ServletException, SQLException {
51         // Set the system property temporarily
52         System.setProperty("PROPERTY_FILE", "src/test/resources/policyelk.properties");
53         try {
54             ClassLoader classLoader = getClass().getClassLoader();
55             policyContent = IOUtils.toString(classLoader.getResourceAsStream("Config_SampleTest1206.1.xml"));
56         } catch (Exception e1) {
57             logger.error("Exception Occured" + e1);
58         }
59         papTest = new XACMLPAPTest();
60         papTest.setDBDao();
61     }
62
63     @Test
64     public void testElasticSearchMainFunction() throws SQLException {
65         ConfigurationDataEntity configurationEntity = new ConfigurationDataEntity();
66         configurationEntity.setConfigBody("Sample Test");
67         configurationEntity.setConfigType("OTHER");
68         configurationEntity.setConfigurationName("com.Config_SampleTest1206.1.txt");
69         configurationEntity.setDescription("test");
70         configurationEntity.setModifiedBy("Test");
71         configurationEntity.setModifiedDate(new Date());
72
73         PolicyEntity entity = new PolicyEntity();
74         entity.setPolicyName("Config_SampleTest.1.xml");
75         entity.setPolicyData(policyContent.toString());
76         entity.setScope("com");
77         entity.setCreatedBy("Test");
78         entity.setDeleted(false);
79         entity.setDescription("Test");
80         entity.setModifiedBy("Test");
81         entity.setConfigurationData(configurationEntity);
82         entity.preUpdate();
83         CommonClassDaoImpl dao = new CommonClassDaoImpl();
84         dao.save(configurationEntity);
85         dao.save(entity);
86         dao.delete(dao.getEntityItem(PolicyEntity.class, "policyName", "Config_SampleTest.1.xml"));
87         ElasticSearchPolicyUpdate.main(null);
88         StringBuilder policyDataString = new StringBuilder();
89         ElasticSearchPolicyUpdate.constructPolicyData(policyContent, policyDataString);
90         assertTrue(policyDataString.toString().contains("onapName"));
91     }
92
93     @After
94     public void reset() {
95         System.clearProperty("PROPERTY_FILE");
96     }
97 }