Merge changes from topic '13891'
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / controller / PolicyControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 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.controller;
21
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import javax.script.SimpleBindings;
31
32 import org.apache.commons.io.IOUtils;
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.rest.dao.CommonClassDao;
38 import org.onap.policy.rest.jpa.Datatype;
39 import org.onap.policy.rest.jpa.FunctionDefinition;
40 import org.onap.policy.rest.jpa.PolicyEntity;
41
42 public class PolicyControllerTest {
43
44         private static Logger logger = FlexLogger.getLogger(PolicyControllerTest.class);
45         private static CommonClassDao commonClassDao;
46         
47         @Before
48         public void setUp() throws Exception{
49                 logger.info("setUp: Entering");
50         commonClassDao = mock(CommonClassDao.class);
51         PolicyController.setCommonClassDao(commonClassDao);
52         List<Object> data = new ArrayList<>();
53         String policyData = "";
54         try {
55                         ClassLoader classLoader = getClass().getClassLoader();
56                         policyData = IOUtils.toString(classLoader.getResourceAsStream("Config_SampleTest1206.1.xml"));
57                 } catch (Exception e1) {
58                         e1.printStackTrace();
59                 }
60         PolicyEntity entity = new PolicyEntity();
61         entity.setPolicyName("Config_SampleTest.1.xml");
62         entity.setPolicyData(policyData);
63         entity.setScope("com");
64         data.add(entity);
65    
66         when(commonClassDao.getDataByQuery("FROM PolicyEntity where policyName = 'Config_SampleTest1206.1.xml' and scope ='com'", new SimpleBindings())).thenReturn(data);
67         
68         FunctionDefinition fnDefinition = new FunctionDefinition();
69         fnDefinition.setXacmlid("Test");
70         fnDefinition.setShortname("Test");
71         Datatype dataType = new Datatype();
72         dataType.setXacmlId("Test");
73         fnDefinition.setDatatypeBean(dataType);
74         List<Object> fnObjects = new ArrayList<>();
75         fnObjects.add(fnDefinition);
76         when(commonClassDao.getData(FunctionDefinition.class)).thenReturn(fnObjects);
77         }
78         
79         @Test
80         public void testInit(){
81                 PolicyController controller = new PolicyController();
82                 PolicyController.setjUnit(true);
83                 controller.init();
84                 try{
85                         assertTrue(PolicyController.dropDownMap.size() > 0);
86                 }catch(Exception e){
87                         logger.error("Exception Occured"+e);
88                         fail();
89                 }
90         }
91 }