2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.controlloop.ophistory;
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;
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;
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";
44 private OperationHistoryDataManagerParams params;
48 params = makeBuilder().build();
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());
62 assertEquals(MY_PU, makeBuilder().persistenceUnit(MY_PU).build().getPersistenceUnit());
64 // use specified driver
65 assertEquals(MY_DRIVER, makeBuilder().driver(MY_DRIVER).build().getDriver());
70 assertTrue(params.validate(CONTAINER).isValid());
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));
79 params.setBatchSize(0);
80 assertFalse(params.validate(CONTAINER).isValid());
82 params.setBatchSize(1);
83 assertTrue(params.validate(CONTAINER).isValid());
85 params.setMaxQueueLength(0);
86 assertFalse(params.validate(CONTAINER).isValid());
88 params.setMaxQueueLength(1);
89 assertTrue(params.validate(CONTAINER).isValid());
91 // blank password is ok
92 params.setPassword("");
93 assertTrue(params.validate(CONTAINER).isValid());
96 private void testValidateField(String fieldName, String expected,
97 Consumer<OperationHistoryDataManagerParams> makeInvalid) {
99 // original params should be valid
100 var result = params.validate(CONTAINER);
101 assertTrue(result.isValid());
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);
111 private OperationHistoryDataManagerParamsBuilder makeBuilder() {
113 return OperationHistoryDataManagerParams.builder()
114 .batchSize(BATCH_SIZE)
115 .maxQueueLength(MAX_QUEUE_LENGTH)