af3c88257935ca20036d188cac50566f3e85bdbc
[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, 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.controller;
24
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import javax.script.SimpleBindings;
34
35 import org.apache.commons.io.IOUtils;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.common.logging.flexlogger.FlexLogger;
39 import org.onap.policy.common.logging.flexlogger.Logger;
40 import org.onap.policy.rest.dao.CommonClassDao;
41 import org.onap.policy.rest.jpa.Datatype;
42 import org.onap.policy.rest.jpa.FunctionDefinition;
43 import org.onap.policy.rest.jpa.PolicyEntity;
44
45 public class PolicyControllerTest {
46
47     private static Logger logger = FlexLogger.getLogger(PolicyControllerTest.class);
48     private static CommonClassDao commonClassDao;
49
50     @Before
51     public void setUp() throws Exception {
52         logger.info("setUp: Entering");
53         commonClassDao = mock(CommonClassDao.class);
54         PolicyController.setCommonClassDao(commonClassDao);
55         List<Object> data = new ArrayList<>();
56         String policyData = "";
57         try {
58             ClassLoader classLoader = getClass().getClassLoader();
59             policyData = IOUtils.toString(classLoader.getResourceAsStream("Config_SampleTest1206.1.xml"));
60         } catch (Exception e1) {
61             e1.printStackTrace();
62         }
63         PolicyEntity entity = new PolicyEntity();
64         entity.setPolicyName("Config_SampleTest.1.xml");
65         entity.setPolicyData(policyData);
66         entity.setScope("com");
67         data.add(entity);
68
69         when(commonClassDao.getDataByQuery(
70                 "FROM PolicyEntity where policyName = 'Config_SampleTest1206.1.xml' and scope ='com'",
71                 new SimpleBindings())).thenReturn(data);
72
73         FunctionDefinition fnDefinition = new FunctionDefinition();
74         fnDefinition.setXacmlid("Test");
75         fnDefinition.setShortname("Test");
76         Datatype dataType = new Datatype();
77         dataType.setXacmlId("Test");
78         fnDefinition.setDatatypeBean(dataType);
79         List<Object> fnObjects = new ArrayList<>();
80         fnObjects.add(fnDefinition);
81         when(commonClassDao.getData(FunctionDefinition.class)).thenReturn(fnObjects);
82     }
83
84     @Test
85     public void testInit() {
86         PolicyController controller = new PolicyController();
87         PolicyController.setjUnit(true);
88         controller.init();
89         try {
90             assertTrue(PolicyController.dropDownMap.size() > 0);
91         } catch (Exception e) {
92             logger.error("Exception Occured" + e);
93             fail();
94         }
95     }
96 }