623df93d077826b80ccf2e1c1f5a14cfd32a08c4
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023-2024 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop.ophistory;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28
29 import java.util.function.Consumer;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerParams.OperationHistoryDataManagerParamsBuilder;
33
34 class OperationHistoryDataManagerParamsTest {
35     private static final String CONTAINER = "my-container";
36     private static final int BATCH_SIZE = 10;
37     private static final int MAX_QUEUE_LENGTH = 20;
38     private static final String MY_PASS = "my-pass";
39     private static final String MY_PU = "my-pu";
40     private static final String MY_DRIVER = "my-driver";
41     private static final String MY_URL = "my-url";
42     private static final String MY_USER = "my-user";
43
44     private OperationHistoryDataManagerParams params;
45
46     @BeforeEach
47     public void setUp() {
48         params = makeBuilder().build();
49     }
50
51     @Test
52     void test() {
53         assertEquals(BATCH_SIZE, params.getBatchSize());
54         assertEquals(MAX_QUEUE_LENGTH, params.getMaxQueueLength());
55         assertEquals(MY_PASS, params.getPassword());
56         assertEquals(OperationHistoryDataManagerParams.DEFAULT_PU, params.getPersistenceUnit());
57         assertEquals(OperationHistoryDataManagerParams.DEFAULT_DRIVER, params.getDriver());
58         assertEquals(MY_URL, params.getUrl());
59         assertEquals(MY_USER, params.getUserName());
60
61         // use specified PU
62         assertEquals(MY_PU, makeBuilder().persistenceUnit(MY_PU).build().getPersistenceUnit());
63
64         // use specified driver
65         assertEquals(MY_DRIVER, makeBuilder().driver(MY_DRIVER).build().getDriver());
66     }
67
68     @Test
69     void testValidate() {
70         assertTrue(params.validate(CONTAINER).isValid());
71
72         testValidateField("url", "null", params2 -> params2.setUrl(null));
73         testValidateField("userName", "null", params2 -> params2.setUserName(null));
74         testValidateField("password", "null", params2 -> params2.setPassword(null));
75         testValidateField("persistenceUnit", "null", params2 -> params2.setPersistenceUnit(null));
76         testValidateField("driver", "null", params2 -> params2.setDriver(null));
77
78         // check edge cases
79         params.setBatchSize(0);
80         assertFalse(params.validate(CONTAINER).isValid());
81
82         params.setBatchSize(1);
83         assertTrue(params.validate(CONTAINER).isValid());
84
85         params.setMaxQueueLength(0);
86         assertFalse(params.validate(CONTAINER).isValid());
87
88         params.setMaxQueueLength(1);
89         assertTrue(params.validate(CONTAINER).isValid());
90
91         // blank password is ok
92         params.setPassword("");
93         assertTrue(params.validate(CONTAINER).isValid());
94     }
95
96     private void testValidateField(String fieldName, String expected,
97                     Consumer<OperationHistoryDataManagerParams> makeInvalid) {
98
99         // original params should be valid
100         var result = params.validate(CONTAINER);
101         assertTrue(result.isValid());
102
103         // make invalid params
104         var params2 = makeBuilder().build();
105         makeInvalid.accept(params2);
106         result = params2.validate(CONTAINER);
107         assertFalse(result.isValid());
108         assertThat(result.getResult()).contains(CONTAINER).contains(fieldName).contains(expected);
109     }
110
111     private OperationHistoryDataManagerParamsBuilder makeBuilder() {
112         // @formatter:off
113         return OperationHistoryDataManagerParams.builder()
114                         .batchSize(BATCH_SIZE)
115                         .maxQueueLength(MAX_QUEUE_LENGTH)
116                         .password(MY_PASS)
117                         .url(MY_URL)
118                         .userName(MY_USER);
119         // @formatter:on
120     }
121 }