2 * ============LICENSE_START=======================================================
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
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.xacml.pdp.application.guard;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
28 import java.util.HashMap;
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;
36 public class GuardPolicyRequestTest {
39 public void testAnomalies() throws ToscaPolicyConversionException {
40 DecisionRequest decisionRequest = new DecisionRequest();
41 assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
43 Map<String, Object> resources = new HashMap<>();
44 decisionRequest.setResource(resources);
45 assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
47 resources.put("notguard", "foo");
48 decisionRequest.setResource(resources);
49 assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
51 resources.put("guard", null);
52 decisionRequest.setResource(resources);
53 assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
55 Map<String, Object> guard = new HashMap<>();
56 resources.put("guard", guard);
57 decisionRequest.setResource(resources);
58 assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
60 guard.put("crap", "notused");
61 resources.put("guard", guard);
62 decisionRequest.setResource(resources);
63 assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
65 guard.put("actor", "notused");
66 resources.put("guard", guard);
67 decisionRequest.setResource(resources);
68 assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
70 guard.put("recipe", "notused");
71 resources.put("guard", guard);
72 decisionRequest.setResource(resources);
73 assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
75 guard.put("clname", "notused");
76 resources.put("guard", guard);
77 decisionRequest.setResource(resources);
78 assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
80 guard.put("target", "notused");
81 resources.put("guard", guard);
82 decisionRequest.setResource(resources);
83 assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
85 guard.put("vfCount", 1);
86 resources.put("guard", guard);
87 decisionRequest.setResource(resources);
88 assertThat(GuardPolicyRequest.createInstance(decisionRequest)).isNotNull();
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));
98 public void testFilterResources() throws Exception {
99 StandardCoder gson = new StandardCoder();
101 DecisionRequest request = gson.decode(
102 TextFileUtils.getTextFileAsString("src/test/resources/requests/guard.filter.json"),
103 DecisionRequest.class);
105 GuardPolicyRequest guardRequest = GuardPolicyRequest.createInstance(request);
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");