2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.ophistory;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
28 import java.util.function.Consumer;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.common.parameters.ValidationResult;
32 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerParams.OperationHistoryDataManagerParamsBuilder;
34 public 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_URL = "my-url";
41 private static final String MY_USER = "my-user";
43 private OperationHistoryDataManagerParams params;
47 params = makeBuilder().build();
52 assertEquals(BATCH_SIZE, params.getBatchSize());
53 assertEquals(MAX_QUEUE_LENGTH, params.getMaxQueueLength());
54 assertEquals(MY_PASS, params.getPassword());
55 assertEquals(OperationHistoryDataManagerParams.DEFAULT_PU, params.getPersistenceUnit());
56 assertEquals(MY_URL, params.getUrl());
57 assertEquals(MY_USER, params.getUserName());
60 assertEquals(MY_PU, makeBuilder().persistenceUnit(MY_PU).build().getPersistenceUnit());
64 public void testValidate() {
65 assertTrue(params.validate(CONTAINER).isValid());
67 testValidateField("url", "null", params2 -> params2.setUrl(null));
68 testValidateField("userName", "null", params2 -> params2.setUserName(null));
69 testValidateField("password", "null", params2 -> params2.setPassword(null));
70 testValidateField("persistenceUnit", "null", params2 -> params2.setPersistenceUnit(null));
73 params.setBatchSize(0);
74 assertFalse(params.validate(CONTAINER).isValid());
76 params.setBatchSize(1);
77 assertTrue(params.validate(CONTAINER).isValid());
79 params.setMaxQueueLength(0);
80 assertFalse(params.validate(CONTAINER).isValid());
82 params.setMaxQueueLength(1);
83 assertTrue(params.validate(CONTAINER).isValid());
85 // blank password is ok
86 params.setPassword("");
87 assertTrue(params.validate(CONTAINER).isValid());
90 private void testValidateField(String fieldName, String expected,
91 Consumer<OperationHistoryDataManagerParams> makeInvalid) {
93 // original params should be valid
94 ValidationResult result = params.validate(CONTAINER);
95 assertTrue(fieldName, result.isValid());
97 // make invalid params
98 OperationHistoryDataManagerParams params2 = makeBuilder().build();
99 makeInvalid.accept(params2);
100 result = params2.validate(CONTAINER);
101 assertFalse(fieldName, result.isValid());
102 assertThat(result.getResult()).contains(CONTAINER).contains(fieldName).contains(expected);
105 private OperationHistoryDataManagerParamsBuilder makeBuilder() {
107 return OperationHistoryDataManagerParams.builder()
108 .batchSize(BATCH_SIZE)
109 .maxQueueLength(MAX_QUEUE_LENGTH)