Skip to content

Commit 4791351

Browse files
committed
FINERACT-5706: Add unit tests for MathUtil
Signed-off-by: Shlesha <shlesha04@gmail.com>
1 parent bbece9c commit 4791351

2 files changed

Lines changed: 64 additions & 41 deletions

File tree

fineract-core/src/test/java/org/apache/fineract/infrastructure/core/service/IpAddressUtilsTest.java

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
1-
/**
2-
* Licensed to the Apache Software Foundation (ASF) under one
3-
* or more contributor license agreements. See the NOTICE file
4-
* distributed with this work for additional information
5-
* regarding copyright ownership. The ASF licenses this file
6-
* to you under the Apache License, Version 2.0 (the
7-
* "License"); you may not use this file except in compliance
8-
* with the License. You may obtain a copy of the License at
9-
*
10-
* http://www.apache.org/licenses/LICENSE-2.0
11-
*
12-
* Unless required by applicable law or agreed to in writing,
13-
* software distributed under the License is distributed on an
14-
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15-
* KIND, either express or implied. See the License for the
16-
* specific language governing permissions and limitations
17-
* under the License.
18-
*/
191
package org.apache.fineract.infrastructure.core.service;
202

213
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -34,14 +16,14 @@ void clearContext() {
3416
RequestContextHolder.resetRequestAttributes();
3517
}
3618

37-
// Proper isolation helper
19+
// Helper method for setting request context
3820
private void withRequest(HttpServletRequest request, Runnable testLogic) {
3921
ServletRequestAttributes attributes = new ServletRequestAttributes(request);
4022
try {
4123
RequestContextHolder.setRequestAttributes(attributes);
4224
testLogic.run();
4325
} finally {
44-
RequestContextHolder.resetRequestAttributes(); // critical cleanup
26+
RequestContextHolder.resetRequestAttributes();
4527
}
4628
}
4729

@@ -86,4 +68,4 @@ void getClientIpConvertsNonStringAttributeUsingToString() {
8668
assertEquals("12345", result);
8769
});
8870
}
89-
}
71+
}

fineract-core/src/test/java/org/apache/fineract/infrastructure/core/service/MathUtilTest.java

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,41 @@ void nullToDefaultReturnsValueWhenNotNull() {
3939
assertEquals(10L, MathUtil.nullToDefault(10L, 5L));
4040
}
4141

42+
@Test
43+
void nullToDefaultHandlesNegativeValues() {
44+
assertEquals(-5, MathUtil.nullToDefault(-5, 10));
45+
}
46+
4247
@Test
4348
void nullToZeroLongReturnsZeroWhenNull() {
4449
assertEquals(0L, MathUtil.nullToZero((Long) null));
4550
}
4651

52+
@Test
53+
void nullToZeroLongReturnsValueWhenNotNull() {
54+
assertEquals(10L, MathUtil.nullToZero(10L));
55+
}
56+
57+
@Test
58+
void nullToZeroIntegerReturnsZeroWhenNull() {
59+
assertEquals(0, MathUtil.nullToZero((Integer) null));
60+
}
61+
62+
@Test
63+
void nullToZeroIntegerReturnsValueWhenNotNull() {
64+
assertEquals(7, MathUtil.nullToZero(7));
65+
}
66+
4767
@Test
4868
void zeroToNullReturnsNullWhenZero() {
4969
assertNull(MathUtil.zeroToNull(0L));
5070
}
5171

72+
@Test
73+
void zeroToNullReturnsValueWhenNonZero() {
74+
assertEquals(5L, MathUtil.zeroToNull(5L));
75+
}
76+
5277
@Test
5378
void negativeToZeroReturnsZeroForNegativeValue() {
5479
assertEquals(0L, MathUtil.negativeToZero(-10L));
@@ -79,37 +104,68 @@ void addReturnsSumOfValues() {
79104
assertEquals(30L, MathUtil.add(10L, 20L));
80105
}
81106

107+
@Test
108+
void addMultipleValuesWorks() {
109+
assertEquals(60L, MathUtil.add(10L, 20L, 30L));
110+
}
111+
82112
@Test
83113
void subtractReturnsCorrectDifference() {
84114
assertEquals(15L, MathUtil.subtract(20L, 5L));
85115
}
86116

117+
@Test
118+
void subtractHandlesNullSecond() {
119+
assertEquals(10L, MathUtil.subtract(10L, (Long) null));
120+
}
121+
122+
@Test
123+
void subtractToZeroNeverNegative() {
124+
assertEquals(0L, MathUtil.subtractToZero(10L, 20L));
125+
}
126+
87127
@Test
88128
void absReturnsPositiveValue() {
89129
assertEquals(25L, MathUtil.abs(-25L));
90130
}
91131

92132
@Test
93133
void bigDecimalAddReturnsCorrectSum() {
94-
BigDecimal result = MathUtil.add(new BigDecimal("10.5"), new BigDecimal("5.5"), new MathContext(10));
134+
BigDecimal result = MathUtil.add(
135+
new BigDecimal("10.5"),
136+
new BigDecimal("5.5"),
137+
new MathContext(10)
138+
);
95139
assertEquals(0, result.compareTo(new BigDecimal("16.0")));
96140
}
97141

98142
@Test
99143
void bigDecimalSubtractReturnsCorrectDifference() {
100-
BigDecimal result = MathUtil.subtract(new BigDecimal("20"), new BigDecimal("5"), new MathContext(10));
144+
BigDecimal result = MathUtil.subtract(
145+
new BigDecimal("20"),
146+
new BigDecimal("5"),
147+
new MathContext(10)
148+
);
101149
assertEquals(0, result.compareTo(new BigDecimal("15")));
102150
}
103151

104152
@Test
105153
void percentageOfCalculatesCorrectValue() {
106-
BigDecimal result = MathUtil.percentageOf(new BigDecimal("200"), new BigDecimal("10"), new MathContext(10));
154+
BigDecimal result = MathUtil.percentageOf(
155+
new BigDecimal("200"),
156+
new BigDecimal("10"),
157+
new MathContext(10)
158+
);
107159
assertEquals(0, result.compareTo(new BigDecimal("20")));
108160
}
109161

110162
@Test
111163
void percentageOfReturnsZeroWhenValueZero() {
112-
BigDecimal result = MathUtil.percentageOf(BigDecimal.ZERO, new BigDecimal("10"), new MathContext(10));
164+
BigDecimal result = MathUtil.percentageOf(
165+
BigDecimal.ZERO,
166+
new BigDecimal("10"),
167+
new MathContext(10)
168+
);
113169
assertEquals(0, result.compareTo(BigDecimal.ZERO));
114170
}
115171

@@ -123,19 +179,4 @@ void stripTrailingZerosRemovesExtraZeros() {
123179
void stripTrailingZerosReturnsNullWhenInputNull() {
124180
assertNull(MathUtil.stripTrailingZeros(null));
125181
}
126-
127-
@Test
128-
void subtractHandlesNullSecond() {
129-
assertEquals(10L, MathUtil.subtract(10L, (Long) null));
130-
}
131-
132-
@Test
133-
void addMultipleValuesWorks() {
134-
assertEquals(60L, MathUtil.add(10L, 20L, 30L));
135-
}
136-
137-
@Test
138-
void subtractToZeroNeverNegative() {
139-
assertEquals(0L, MathUtil.subtractToZero(10L, 20L));
140-
}
141-
}
182+
}

0 commit comments

Comments
 (0)