2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controller;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
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;
42 public class CreatePolicyControllerTest {
44 private PolicyEntity entity;
47 * before - sets up the mocked PolicyEntity.
50 public void before() {
54 ConfigurationDataEntity dataEntity = mock(ConfigurationDataEntity.class);
55 when(dataEntity.getConfigType()).thenReturn("configtype");
56 entity = mock(PolicyEntity.class);
57 when(entity.getConfigurationData()).thenReturn(dataEntity);
61 public void testEasyStuff() {
62 CreatePolicyController controller = new CreatePolicyController();
63 PolicyRestAdapter adapter = new PolicyRestAdapter();
64 controller.prePopulateBaseConfigPolicyData(adapter, null);
66 // Create a simple PolicyType
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);
78 public void testBadDescription() {
79 PolicyRestAdapter adapter = new PolicyRestAdapter();
81 // Create a simple PolicyType
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);
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"));
107 AnyOfType anyOf = new AnyOfType();
108 anyOf.getAllOf().add(allOf);
110 TargetType target = new TargetType();
111 target.getAnyOf().add(anyOf);
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);
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);
132 MatchType match = new MatchType();
133 match.setAttributeValue(value);
134 match.setAttributeDesignator(designator);