View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.junit.bct;
18  
19  import static org.apache.juneau.junit.bct.BctAssertions.*;
20  import static org.apache.juneau.junit.bct.BctUtils.*;
21  import static org.junit.jupiter.api.Assertions.*;
22  
23  import org.apache.juneau.*;
24  import org.junit.jupiter.api.*;
25  
26  /**
27   * Unit tests for the {@link BctUtils} utility class.
28   *
29   * <p>This test class validates BCT-specific utility methods including assertion helpers,
30   * tokenization, and convenience aliases for common operations.
31   */
32  @DisplayName("BctUtils")
33  class BctUtils_Test extends TestBase {
34  
35  	// ====================================================================================================
36  	// Assertion Helper Tests
37  	// ====================================================================================================
38  
39  	@Test
40  	@DisplayName("assertEqualsFailed() - Basic error creation")
41  	void a01_assertEqualsFailedBasicCreation() {
42  		var error = assertEqualsFailed("expected", "actual", null);
43  
44  		assertEquals("Equals assertion failed. ==> expected: <expected> but was: <actual>", error.getMessage());
45  		assertEquals("expected", error.getExpected().getValue());
46  		assertEquals("actual", error.getActual().getValue());
47  	}
48  
49  	@Test
50  	@DisplayName("assertEqualsFailed() - With custom message")
51  	void a02_assertEqualsFailedCustomMessage() {
52  		var error = assertEqualsFailed(100, 200, () -> "Custom validation failed");
53  
54  		assertEquals("Custom validation failed ==> expected: <100> but was: <200>", error.getMessage());
55  	}
56  
57  	@Test
58  	@DisplayName("assertEqualsFailed() - Null values")
59  	void a03_assertEqualsFailedNullValues() {
60  		// Null expected
61  		var error1 = assertEqualsFailed(null, "actual", null);
62  		assertEquals("Equals assertion failed. ==> expected: <null> but was: <actual>", error1.getMessage());
63  
64  		// Null actual
65  		var error2 = assertEqualsFailed("expected", null, null);
66  		assertEquals("Equals assertion failed. ==> expected: <expected> but was: <null>", error2.getMessage());
67  
68  		// Both null
69  		var error3 = assertEqualsFailed(null, null, null);
70  		assertEquals("Equals assertion failed. ==> expected: <null> but was: <null>", error3.getMessage());
71  	}
72  
73  	// ====================================================================================================
74  	// Tokenization Tests
75  	// ====================================================================================================
76  
77  	@Test
78  	@DisplayName("tokenize() - Basic tokenization")
79  	void d01_tokenizeBasic() {
80  		var tokens = tokenize("name,age,city");
81  		assertList(tokens.stream().map(t -> t.getValue()).toList(), "name", "age", "city");
82  
83  		// Each should have no nested fields
84  		tokens.forEach(t -> assertEmpty(t.getNested()));
85  	}
86  
87  	@Test
88  	@DisplayName("tokenize() - Nested fields")
89  	void d02_tokenizeNested() {
90  		var tokens = tokenize("name,address{street,city,zip},age");
91  
92  		assertSize(3, tokens);
93  		assertEquals("name", tokens.get(0).getValue());
94  		assertEmpty(tokens.get(0).getNested());
95  
96  		assertEquals("address", tokens.get(1).getValue());
97  		var nested = tokens.get(1).getNested();
98  		assertList(nested.stream().map(t -> t.getValue()).toList(), "street", "city", "zip");
99  
100 		assertEquals("age", tokens.get(2).getValue());
101 		assertEmpty(tokens.get(2).getNested());
102 	}
103 }
104