Consolidate PolicyRestAdapter setup
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / controller / CreatePolicyControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 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.controller;
22
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25
26 import com.att.research.xacml.api.XACML3;
27 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
28 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
29 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
30 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
31 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
32 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
33 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
34 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
35 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableDefinitionType;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.rest.adapter.PolicyRestAdapter;
39 import org.onap.policy.rest.jpa.ConfigurationDataEntity;
40 import org.onap.policy.rest.jpa.PolicyEntity;
41
42 public class CreatePolicyControllerTest {
43
44     private PolicyEntity entity;
45
46     /**
47      * before - sets up the mocked PolicyEntity.
48      */
49     @Before
50     public void before() {
51         //
52         // PolicyEntity
53         //
54         ConfigurationDataEntity dataEntity = mock(ConfigurationDataEntity.class);
55         when(dataEntity.getConfigType()).thenReturn("configtype");
56         entity = mock(PolicyEntity.class);
57         when(entity.getConfigurationData()).thenReturn(dataEntity);
58     }
59
60     @Test
61     public void testEasyStuff() {
62         CreatePolicyController controller = new CreatePolicyController();
63         PolicyRestAdapter adapter = new PolicyRestAdapter();
64         controller.prePopulateBaseConfigPolicyData(adapter, null);
65         //
66         // Create a simple PolicyType
67         //
68         VariableDefinitionType var = new VariableDefinitionType();
69         PolicyType policy = new PolicyType();
70         policy.setDescription("i am a description. @CreatedBy: policy designer");
71         policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(var);
72         adapter.setPolicyData(policy);
73         adapter.setPolicyName("name");
74         controller.prePopulateBaseConfigPolicyData(adapter, entity);
75     }
76
77     @Test
78     public void testBadDescription() {
79         PolicyRestAdapter adapter = new PolicyRestAdapter();
80         //
81         // Create a simple PolicyType
82         //
83         VariableDefinitionType var = new VariableDefinitionType();
84         PolicyType policy = new PolicyType();
85         policy.setDescription("i am a description");
86         policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(var);
87         adapter.setPolicyData(policy);
88         adapter.setPolicyName("name");
89         CreatePolicyController controller = new CreatePolicyController();
90         controller.prePopulateBaseConfigPolicyData(adapter, entity);
91     }
92
93     @Test
94     public void testExpectedPolicyContents() {
95         AllOfType allOf = new AllOfType();
96         allOf.getMatch().add(createMatchType("ONAPName", "ONAPName"));
97         allOf.getMatch().add(createMatchType("RiskType", "RiskType"));
98         allOf.getMatch().add(createMatchType("RiskLevel", "RiskLevel"));
99         allOf.getMatch().add(createMatchType("guard", "guard"));
100         allOf.getMatch().add(createMatchType("TTLDate", "TTLDate"));
101         allOf.getMatch().add(createMatchType("ConfigName", "ConfigName"));
102         allOf.getMatch().add(createMatchType("NA", "TTLDate"));
103         allOf.getMatch().add(createMatchType("custom", "custom"));
104         allOf.getMatch().add(createMatchType("custom", "custom"));
105         allOf.getMatch().add(createMatchType("custom", "custom"));
106
107         AnyOfType anyOf = new AnyOfType();
108         anyOf.getAllOf().add(allOf);
109
110         TargetType target = new TargetType();
111         target.getAnyOf().add(anyOf);
112
113         RuleType rule = new RuleType();
114         PolicyType policy = new PolicyType();
115         policy.setDescription("i am a description. @CreatedBy: policy designer");
116         policy.setTarget(target);
117         policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
118         PolicyRestAdapter adapter = new PolicyRestAdapter();
119         adapter.setPolicyData(policy);
120         adapter.setPolicyName("name");
121         CreatePolicyController controller = new CreatePolicyController();
122         controller.prePopulateBaseConfigPolicyData(adapter, entity);
123     }
124
125     private MatchType createMatchType(String strValue, String id) {
126         AttributeValueType value = new AttributeValueType();
127         value.setDataType(XACML3.ID_DATATYPE_STRING.stringValue());
128         value.getContent().add(new String(strValue));
129         AttributeDesignatorType designator = new AttributeDesignatorType();
130         designator.setAttributeId(id);
131
132         MatchType match = new MatchType();
133         match.setAttributeValue(value);
134         match.setAttributeDesignator(designator);
135
136         return match;
137     }
138
139 }