Set all cross references of policy/xacml-pdp
[policy/xacml-pdp.git] / applications / guard / src / test / java / org / onap / policy / xacml / pdp / application / guard / GuardPolicyRequestTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.xacml.pdp.application.guard;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
27
28 import java.util.HashMap;
29 import java.util.Map;
30 import org.junit.Test;
31 import org.onap.policy.common.utils.coder.StandardCoder;
32 import org.onap.policy.common.utils.resources.TextFileUtils;
33 import org.onap.policy.models.decisions.concepts.DecisionRequest;
34 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
35
36 public class GuardPolicyRequestTest {
37
38     @Test
39     public void testAnomalies() throws ToscaPolicyConversionException {
40         DecisionRequest decisionRequest = new DecisionRequest();
41         assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
42
43         Map<String, Object> resources = new HashMap<>();
44         decisionRequest.setResource(resources);
45         assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
46
47         resources.put("notguard", "foo");
48         decisionRequest.setResource(resources);
49         assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
50
51         resources.put("guard", null);
52         decisionRequest.setResource(resources);
53         assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
54
55         Map<String, Object> guard = new HashMap<>();
56         resources.put("guard", guard);
57         decisionRequest.setResource(resources);
58         assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
59
60         guard.put("crap", "notused");
61         resources.put("guard", guard);
62         decisionRequest.setResource(resources);
63         assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
64
65         guard.put("actor", "notused");
66         resources.put("guard", guard);
67         decisionRequest.setResource(resources);
68         assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
69
70         guard.put("recipe", "notused");
71         resources.put("guard", guard);
72         decisionRequest.setResource(resources);
73         assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
74
75         guard.put("clname", "notused");
76         resources.put("guard", guard);
77         decisionRequest.setResource(resources);
78         assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
79
80         guard.put("target", "notused");
81         resources.put("guard", guard);
82         decisionRequest.setResource(resources);
83         assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
84
85         guard.put("vfCount", 1);
86         resources.put("guard", guard);
87         decisionRequest.setResource(resources);
88         assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
89
90         guard.put("vfCount", "I am not valid");
91         resources.put("guard", guard);
92         decisionRequest.setResource(resources);
93         assertThatExceptionOfType(ToscaPolicyConversionException.class).isThrownBy(() ->
94             GuardPolicyRequest.createInstance(decisionRequest));
95     }
96
97     @Test
98     public void testFilterResources() throws Exception {
99         StandardCoder gson = new StandardCoder();
100
101         DecisionRequest request = gson.decode(
102                 TextFileUtils.getTextFileAsString("src/test/resources/requests/guard.filter.json"),
103                 DecisionRequest.class);
104
105         GuardPolicyRequest guardRequest = GuardPolicyRequest.createInstance(request);
106
107         assertThat(guardRequest.getVnfName()).isEqualTo("my-name");
108         assertThat(guardRequest.getVnfId()).isEqualTo("my-id");
109         assertThat(guardRequest.getVnfType()).isEqualTo("my-type");
110         assertThat(guardRequest.getVnfNfNamingCode()).isEqualTo("my-naming-code");
111         assertThat(guardRequest.getVserverId()).isEqualTo("my-server-id");
112         assertThat(guardRequest.getCloudRegionId()).isEqualTo("my-region");
113     }
114
115 }