8b5357457b0cc1c5642145bb6617892af5a57529
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24
25 import java.util.List;
26 import java.util.concurrent.atomic.AtomicInteger;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
30 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoop;
31 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ProviderUtils;
32 import org.onap.policy.common.utils.coder.Coder;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34 import org.onap.policy.common.utils.resources.ResourceUtils;
35 import org.onap.policy.models.dao.PfDao;
36 import org.onap.policy.models.dao.PfFilterParameters;
37 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
38 import org.onap.policy.models.provider.impl.ModelsProvider;
39
40 class FilterRepositoryImplTest {
41     private static final String CONTROL_LOOP_JSON = "src/test/resources/providers/TestControlLoops.json";
42     private static final Coder CODER = new StandardCoder();
43     private static final AtomicInteger dbNameCounter = new AtomicInteger();
44     private static final String originalJson = ResourceUtils.getResourceAsString(CONTROL_LOOP_JSON);
45     private static List<JpaControlLoop> jpaControlLoops;
46     private PfDao pfDao;
47
48     @BeforeEach
49     void beforeSetupDao() throws Exception {
50         var parameters = new PolicyModelsProviderParameters();
51         parameters.setDatabaseDriver("org.h2.Driver");
52         parameters.setName("PolicyProviderParameterGroup");
53         parameters.setImplementation("org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl");
54         parameters.setDatabaseUrl("jdbc:h2:mem:controlLoopProviderTestDb" + dbNameCounter.getAndDecrement());
55         parameters.setDatabaseUser("policy");
56         parameters.setDatabasePassword("P01icY");
57         parameters.setPersistenceUnit("ToscaConceptTest");
58
59         pfDao = ModelsProvider.init(parameters);
60         var inputControlLoops = CODER.decode(originalJson, ControlLoops.class);
61         jpaControlLoops = ProviderUtils.getJpaAndValidateList(inputControlLoops.getControlLoopList(),
62                 JpaControlLoop::new, "ControlLoops");
63
64         pfDao.createCollection(jpaControlLoops);
65     }
66
67     @Test
68     void testGetPfDao() {
69         assertThat(new FilterRepositoryImpl().getPfDao()).isNotNull();
70     }
71
72     @Test
73     void testGetFilteredParams() {
74         var filterRepositoryImpl = new FilterRepositoryImpl() {
75             @Override
76             protected PfDao getPfDao() {
77                 return pfDao;
78             }
79         };
80         var result = filterRepositoryImpl.getFiltered(JpaControlLoop.class, null, null);
81         assertThat(result).hasSize(2);
82
83         result = filterRepositoryImpl.getFiltered(JpaControlLoop.class, jpaControlLoops.get(0).getName(), null);
84         assertThat(result).hasSize(1);
85     }
86
87     @Test
88     void testGetFiltered() {
89         var filterRepositoryImpl = new FilterRepositoryImpl() {
90             @Override
91             protected PfDao getPfDao() {
92                 return pfDao;
93             }
94         };
95
96         // @formatter:off
97         PfFilterParameters filterParams = PfFilterParameters
98                 .builder()
99                 .name(jpaControlLoops.get(0).getName())
100                 .build();
101         // @formatter:on
102
103         var result = filterRepositoryImpl.getFiltered(JpaControlLoop.class, filterParams);
104         assertThat(result).hasSize(1);
105     }
106 }