452bbd42a83258a023bb6735519471f5b8ec3d51
[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         try (PolicyModelsProvider dummyProvider =
82                 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) {
83             dummyProvider.init();
84
85             assertNotNull(dummyProvider.getPolicyTypes("name", VERSION));
86             assertNotNull(dummyProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build()));
87             assertNotNull(dummyProvider.getPolicyTypeList("name", VERSION));
88             assertNotNull(dummyProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build()));
89             assertNotNull(dummyProvider.createPolicyTypes(new ToscaServiceTemplate()));
90             assertNotNull(dummyProvider.updatePolicyTypes(new ToscaServiceTemplate()));
91             assertNotNull(dummyProvider.deletePolicyType("name", VERSION));
92
93             assertNotNull(dummyProvider.getPolicies("name", VERSION));
94             assertNotNull(dummyProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build()));
95             assertNotNull(dummyProvider.getPolicyList("name", VERSION));
96             assertNotNull(dummyProvider.getFilteredPolicyList(ToscaPolicyFilter.builder().build()));
97             assertNotNull(dummyProvider.createPolicies(new ToscaServiceTemplate()));
98             assertNotNull(dummyProvider.updatePolicies(new ToscaServiceTemplate()));
99             assertNotNull(dummyProvider.deletePolicy("name", VERSION));
100
101             assertNotNull(dummyProvider.getOperationalPolicy(POLICY_ID, "1"));
102             assertNotNull(dummyProvider.createOperationalPolicy(new LegacyOperationalPolicy()));
103             assertNotNull(dummyProvider.updateOperationalPolicy(new LegacyOperationalPolicy()));
104             assertNotNull(dummyProvider.deleteOperationalPolicy(POLICY_ID, "1"));
105
106             assertNotNull(dummyProvider.getGuardPolicy(POLICY_ID, "1"));
107             assertNotNull(dummyProvider.createGuardPolicy(new LegacyGuardPolicyInput()));
108             assertNotNull(dummyProvider.updateGuardPolicy(new LegacyGuardPolicyInput()));
109             assertNotNull(dummyProvider.deleteGuardPolicy(POLICY_ID, "1"));
110
111             assertTrue(dummyProvider.getPdpGroups("name").isEmpty());
112             assertTrue(dummyProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).isEmpty());
113             assertTrue(dummyProvider.createPdpGroups(new ArrayList<>()).isEmpty());
114             assertTrue(dummyProvider.updatePdpGroups(new ArrayList<>()).isEmpty());
115             assertNull(dummyProvider.deletePdpGroup("name"));
116
117             dummyProvider.updatePdpSubGroup("name", new PdpSubGroup());
118             dummyProvider.updatePdp("name", "type", new Pdp());
119             dummyProvider.updatePdpStatistics(new ArrayList<>());
120             assertTrue(dummyProvider.getPdpStatistics("name", null).isEmpty());
121         }
122     }
123
124     @Test
125     public void testDummyResponse() {
126         try (DummyPolicyModelsProviderSubImpl resp =
127                 new DummyPolicyModelsProviderSubImpl(new PolicyModelsProviderParameters())) {
128             assertThatThrownBy(resp::getBadDummyResponse1).hasMessage("error serializing object");
129         }
130
131         try (DummyPolicyModelsProviderSubImpl resp =
132                 new DummyPolicyModelsProviderSubImpl(new PolicyModelsProviderParameters())) {
133             assertThatThrownBy(resp::getBadDummyResponse2).hasMessage("error serializing object");
134         }
135     }
136 }