97f30652832b3a8a0ed686358e39c056adda7606
[policy/drools-applications.git] / controlloop / common / eventmanager / src / test / java / org / onap / policy / controlloop / ophistory / OperationHistoryDataManagerParamsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.ophistory;
22
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;
27
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;
33
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_DRIVER = "my-driver";
41     private static final String MY_DB_TYPE = "my-db-type";
42     private static final String MY_URL = "my-url";
43     private static final String MY_USER = "my-user";
44
45     private OperationHistoryDataManagerParams params;
46
47     @Before
48     public void setUp() {
49         params = makeBuilder().build();
50     }
51
52     @Test
53     public void test() {
54         assertEquals(BATCH_SIZE, params.getBatchSize());
55         assertEquals(MAX_QUEUE_LENGTH, params.getMaxQueueLength());
56         assertEquals(MY_PASS, params.getPassword());
57         assertEquals(OperationHistoryDataManagerParams.DEFAULT_PU, params.getPersistenceUnit());
58         assertEquals(OperationHistoryDataManagerParams.DEFAULT_DRIVER, params.getDriver());
59         assertEquals(OperationHistoryDataManagerParams.DEFAULT_TYPE, params.getDbType());
60         assertEquals(MY_URL, params.getUrl());
61         assertEquals(MY_USER, params.getUserName());
62
63         // use specified PU
64         assertEquals(MY_PU, makeBuilder().persistenceUnit(MY_PU).build().getPersistenceUnit());
65
66         // use specified driver
67         assertEquals(MY_DRIVER, makeBuilder().driver(MY_DRIVER).build().getDriver());
68
69         // use specified DB type
70         assertEquals(MY_DB_TYPE, makeBuilder().dbType(MY_DB_TYPE).build().getDbType());
71     }
72
73     @Test
74     public void testValidate() {
75         assertTrue(params.validate(CONTAINER).isValid());
76
77         testValidateField("url", "null", params2 -> params2.setUrl(null));
78         testValidateField("userName", "null", params2 -> params2.setUserName(null));
79         testValidateField("password", "null", params2 -> params2.setPassword(null));
80         testValidateField("persistenceUnit", "null", params2 -> params2.setPersistenceUnit(null));
81         testValidateField("driver", "null", params2 -> params2.setDriver(null));
82         testValidateField("dbType", "null", params2 -> params2.setDbType(null));
83
84         // check edge cases
85         params.setBatchSize(0);
86         assertFalse(params.validate(CONTAINER).isValid());
87
88         params.setBatchSize(1);
89         assertTrue(params.validate(CONTAINER).isValid());
90
91         params.setMaxQueueLength(0);
92         assertFalse(params.validate(CONTAINER).isValid());
93
94         params.setMaxQueueLength(1);
95         assertTrue(params.validate(CONTAINER).isValid());
96
97         // blank password is ok
98         params.setPassword("");
99         assertTrue(params.validate(CONTAINER).isValid());
100     }
101
102     private void testValidateField(String fieldName, String expected,
103                     Consumer<OperationHistoryDataManagerParams> makeInvalid) {
104
105         // original params should be valid
106         ValidationResult result = params.validate(CONTAINER);
107         assertTrue(fieldName, result.isValid());
108
109         // make invalid params
110         OperationHistoryDataManagerParams params2 = makeBuilder().build();
111         makeInvalid.accept(params2);
112         result = params2.validate(CONTAINER);
113         assertFalse(fieldName, result.isValid());
114         assertThat(result.getResult()).contains(CONTAINER).contains(fieldName).contains(expected);
115     }
116
117     private OperationHistoryDataManagerParamsBuilder makeBuilder() {
118         // @formatter:off
119         return OperationHistoryDataManagerParams.builder()
120                         .batchSize(BATCH_SIZE)
121                         .maxQueueLength(MAX_QUEUE_LENGTH)
122                         .password(MY_PASS)
123                         .url(MY_URL)
124                         .userName(MY_USER);
125         // @formatter:on
126     }
127 }