Implement validation and hierarchical get
[policy/models.git] / models-provider / src / test / java / org / onap / policy / models / provider / impl / DummyPolicyModelsProviderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.provider.impl;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.util.ArrayList;
31
32 import org.junit.Test;
33 import org.onap.policy.models.pdp.concepts.Pdp;
34 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
35 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
36 import org.onap.policy.models.provider.PolicyModelsProvider;
37 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
38 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
42 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
43 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
44
45 /**
46  * Test the dummy models provider implementation.
47  *
48  * @author Liam Fallon (liam.fallon@est.tech)
49  */
50 public class DummyPolicyModelsProviderTest {
51
52     private static final String POLICY_ID = "policy_id";
53     private static final String VERSION = "version";
54
55     @Test
56     public void testProvider() throws Exception {
57         PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
58         parameters.setImplementation(DummyPolicyModelsProviderImpl.class.getName());
59         parameters.setDatabaseUrl("jdbc:dummy");
60         parameters.setPersistenceUnit("dummy");
61
62         try (PolicyModelsProvider dummyProvider =
63                 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) {
64
65             dummyProvider.init();
66
67             ToscaServiceTemplate serviceTemplate = dummyProvider.getPolicies("onap.vcpe.tca", "1.0.0");
68             assertNotNull(serviceTemplate);
69             assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app",
70                     serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get("onap.vcpe.tca").getType());
71         }
72     }
73
74     @Test
75     public void testProviderMethods() throws Exception {
76         PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters();
77         parameters.setImplementation(DummyPolicyModelsProviderImpl.class.getName());
78         parameters.setDatabaseUrl("jdbc:dummy");
79         parameters.setPersistenceUnit("dummy");
80
81         PolicyModelsProvider dummyProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
82         dummyProvider.init();
83
84         assertNotNull(dummyProvider.getPolicyTypes("name", VERSION));
85         assertNotNull(dummyProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build()));
86         assertNotNull(dummyProvider.getPolicyTypeList("name", VERSION));
87         assertNotNull(dummyProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build()));
88         assertNotNull(dummyProvider.createPolicyTypes(new ToscaServiceTemplate()));
89         assertNotNull(dummyProvider.updatePolicyTypes(new ToscaServiceTemplate()));
90         assertNotNull(dummyProvider.deletePolicyType("name", VERSION));
91
92         assertNotNull(dummyProvider.getPolicies("name", VERSION));
93         assertNotNull(dummyProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build()));
94         assertNotNull(dummyProvider.getPolicyList("name", VERSION));
95         assertNotNull(dummyProvider.getFilteredPolicyList(ToscaPolicyFilter.builder().build()));
96         assertNotNull(dummyProvider.createPolicies(new ToscaServiceTemplate()));
97         assertNotNull(dummyProvider.updatePolicies(new ToscaServiceTemplate()));
98         assertNotNull(dummyProvider.deletePolicy("name", VERSION));
99
100         assertNotNull(dummyProvider.getOperationalPolicy(POLICY_ID, "1"));
101         assertNotNull(dummyProvider.createOperationalPolicy(new LegacyOperationalPolicy()));
102         assertNotNull(dummyProvider.updateOperationalPolicy(new LegacyOperationalPolicy()));
103         assertNotNull(dummyProvider.deleteOperationalPolicy(POLICY_ID, "1"));
104
105         assertNotNull(dummyProvider.getGuardPolicy(POLICY_ID, "1"));
106         assertNotNull(dummyProvider.createGuardPolicy(new LegacyGuardPolicyInput()));
107         assertNotNull(dummyProvider.updateGuardPolicy(new LegacyGuardPolicyInput()));
108         assertNotNull(dummyProvider.deleteGuardPolicy(POLICY_ID, "1"));
109
110         assertTrue(dummyProvider.getPdpGroups("name").isEmpty());
111         assertTrue(dummyProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).isEmpty());
112         assertTrue(dummyProvider.createPdpGroups(new ArrayList<>()).isEmpty());
113         assertTrue(dummyProvider.updatePdpGroups(new ArrayList<>()).isEmpty());
114         assertNull(dummyProvider.deletePdpGroup("name"));
115
116         dummyProvider.updatePdpSubGroup("name", new PdpSubGroup());
117         dummyProvider.updatePdp("name", "type", new Pdp());
118         dummyProvider.updatePdpStatistics(new ArrayList<>());
119         assertTrue(dummyProvider.getPdpStatistics("name", null).isEmpty());
120     }
121
122     @Test
123     public void testDummyResponse() {
124         try (DummyPolicyModelsProviderSubImpl resp =
125                 new DummyPolicyModelsProviderSubImpl(new PolicyModelsProviderParameters())) {
126             assertThatThrownBy(resp::getBadDummyResponse1).hasMessage("error serializing object");
127         }
128
129         try (DummyPolicyModelsProviderSubImpl resp =
130                 new DummyPolicyModelsProviderSubImpl(new PolicyModelsProviderParameters())) {
131             assertThatThrownBy(resp::getBadDummyResponse2).hasMessage("error serializing object");
132         }
133     }
134 }