aeeac4796aae48a64915995dfcb358ef31d1d7cb
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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_URL = "my-url";
41     private static final String MY_USER = "my-user";
42
43     private OperationHistoryDataManagerParams params;
44
45     @Before
46     public void setUp() {
47         params = makeBuilder().build();
48     }
49
50     @Test
51     public void test() {
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());
58
59         // use specified PU
60         assertEquals(MY_PU, makeBuilder().persistenceUnit(MY_PU).build().getPersistenceUnit());
61     }
62
63     @Test
64     public void testValidate() {
65         assertTrue(params.validate(CONTAINER).isValid());
66
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));
71
72         // check edge cases
73         params.setBatchSize(0);
74         assertFalse(params.validate(CONTAINER).isValid());
75
76         params.setBatchSize(1);
77         assertTrue(params.validate(CONTAINER).isValid());
78
79         params.setMaxQueueLength(0);
80         assertFalse(params.validate(CONTAINER).isValid());
81
82         params.setMaxQueueLength(1);
83         assertTrue(params.validate(CONTAINER).isValid());
84
85         // blank password is ok
86         params.setPassword("");
87         assertTrue(params.validate(CONTAINER).isValid());
88     }
89
90     private void testValidateField(String fieldName, String expected,
91                     Consumer<OperationHistoryDataManagerParams> makeInvalid) {
92
93         // original params should be valid
94         ValidationResult result = params.validate(CONTAINER);
95         assertTrue(fieldName, result.isValid());
96
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);
103     }
104
105     private OperationHistoryDataManagerParamsBuilder makeBuilder() {
106         // @formatter:off
107         return OperationHistoryDataManagerParams.builder()
108                         .batchSize(BATCH_SIZE)
109                         .maxQueueLength(MAX_QUEUE_LENGTH)
110                         .password(MY_PASS)
111                         .url(MY_URL)
112                         .userName(MY_USER);
113         // @formatter:on
114     }
115 }