Fix sonar and coverage
[policy/xacml-pdp.git] / applications / common / src / test / java / org / onap / policy / pdp / xacml / application / common / std / StdBaseTranslatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pdp.xacml.application.common.std;
24
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29
30 import java.util.HashMap;
31 import java.util.Map;
32 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
33 import org.junit.Test;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
35 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
36
37 public class StdBaseTranslatorTest {
38
39     @Test
40     public void test() {
41         StdBaseTranslator translator = new StdBaseTranslator();
42         assertNotNull(translator);
43         assertThatThrownBy(() -> translator.convertPolicy(null)).isInstanceOf(ToscaPolicyConversionException.class);
44         assertNull(translator.convertRequest(null));
45     }
46
47     @Test
48     public void testBadData() throws ToscaPolicyConversionException {
49         TestTranslator translator = new TestTranslator();
50
51         assertThatThrownBy(() -> translator.convertPolicy(
52                 new ToscaPolicy())).isInstanceOf(ToscaPolicyConversionException.class)
53                     .hasMessageContaining("missing metadata");
54
55         translator.metadata.put(StdBaseTranslator.POLICY_ID, "random.policy.id");
56
57         assertThatThrownBy(() -> translator.convertPolicy(
58                 new ToscaPolicy())).isInstanceOf(ToscaPolicyConversionException.class)
59                     .hasMessageContaining("missing metadata");
60
61         translator.metadata.put(StdBaseTranslator.POLICY_VERSION, "1.0.0");
62
63         ToscaPolicy policy = new ToscaPolicy();
64         assertEquals("1.0.0", translator.convertPolicy(policy).getVersion());
65
66     }
67
68     public class TestTranslator extends StdBaseTranslator {
69         public Map<String, String> metadata = new HashMap<>();
70
71         @Override
72         public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
73             PolicyType xacmlPolicy = new PolicyType();
74             this.fillMetadataSection(xacmlPolicy, metadata);
75             return xacmlPolicy;
76         }
77     }
78
79 }