2b1300cc408dee7ab67e087ffecc74bd7b20dc44
[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 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 package org.onap.policy.pap.xacml.rest.elk;
21
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.IOException;
25 import java.sql.SQLException;
26 import java.util.Date;
27
28 import javax.servlet.ServletException;
29
30 import org.apache.commons.io.IOUtils;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.common.logging.flexlogger.FlexLogger;
35 import org.onap.policy.common.logging.flexlogger.Logger;
36 import org.onap.policy.pap.test.XACMLPAPTest;
37 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
38 import org.onap.policy.pap.xacml.rest.elk.client.ElasticSearchPolicyUpdate;
39 import org.onap.policy.rest.jpa.ConfigurationDataEntity;
40 import org.onap.policy.rest.jpa.PolicyEntity;
41
42 public class ElasticSearchPolicyUpdateTest {
43         
44         private static Logger logger = FlexLogger.getLogger(ElasticSearchPolicyUpdateTest.class);
45         private Object policyContent = "";
46         private XACMLPAPTest papTest;
47         
48         @Before
49         public void setUp() throws IOException, ServletException, SQLException{
50                 // Set the system property temporarily
51                 System.setProperty("PROPERTY_FILE", "src/test/resources/policyelk.properties");
52         try {
53                         ClassLoader classLoader = getClass().getClassLoader();
54                         policyContent = IOUtils.toString(classLoader.getResourceAsStream("Config_SampleTest1206.1.xml"));
55                 } catch (Exception e1) {
56                         logger.error("Exception Occured"+e1);
57                 }
58         papTest = new XACMLPAPTest();
59                 papTest.setDBDao();
60         }
61         
62         @Test
63         public void testElasticSearchMainFunction() throws SQLException{
64                 ConfigurationDataEntity configurationEntity = new ConfigurationDataEntity();
65         configurationEntity.setConfigBody("Sample Test");
66         configurationEntity.setConfigType("OTHER");
67         configurationEntity.setConfigurationName("com.Config_SampleTest1206.1.txt");
68         configurationEntity.setDescription("test");
69         configurationEntity.setModifiedBy("Test");
70         configurationEntity.setModifiedDate(new Date());
71         
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 }