Skip to content
This repository was archived by the owner on Jul 7, 2021. It is now read-only.

Commit b275790

Browse files
Bump json from 20180813 to 20200518 (#321)
* Bump json from 20180813 to 20200518 Bumps [json](https://github.com/douglascrockford/JSON-java) from 20180813 to 20200518. - [Release notes](https://github.com/douglascrockford/JSON-java/releases) - [Commits](https://github.com/douglascrockford/JSON-java/commits) Signed-off-by: dependabot-preview[bot] <[email protected]> * fix new json version conflicts * fix failing build * PR comment * fix failing test * fix sonarcloud code smeels * fix build Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> Co-authored-by: Constantin Costescu <[email protected]>
1 parent f9fb954 commit b275790

File tree

10 files changed

+123
-16
lines changed

10 files changed

+123
-16
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
<dependency>
9090
<groupId>org.json</groupId>
9191
<artifactId>json</artifactId>
92-
<version>20180813</version>
92+
<version>20200518</version>
9393
</dependency>
9494
<dependency>
9595
<groupId>org.slf4j</groupId>

src/main/java/org/proshin/finapi/account/out/FpDailyBalance.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.time.OffsetDateTime;
2020
import org.json.JSONArray;
2121
import org.json.JSONObject;
22+
import org.proshin.finapi.primitives.BigDecimalOf;
2223
import org.proshin.finapi.primitives.IterableJsonArray;
2324
import org.proshin.finapi.primitives.OffsetDateTimeOf;
2425

@@ -37,17 +38,17 @@ public OffsetDateTime date() {
3738

3839
@Override
3940
public BigDecimal balance() {
40-
return this.origin.getBigDecimal("balance");
41+
return new BigDecimalOf(this.origin, "balance").get();
4142
}
4243

4344
@Override
4445
public BigDecimal income() {
45-
return this.origin.getBigDecimal("income");
46+
return new BigDecimalOf(this.origin, "income").get();
4647
}
4748

4849
@Override
4950
public BigDecimal spending() {
50-
return this.origin.getBigDecimal("spending");
51+
return new BigDecimalOf(this.origin, "spending").get();
5152
}
5253

5354
@Override

src/main/java/org/proshin/finapi/category/out/FpCashFlow.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.proshin.finapi.category.Category;
2323
import org.proshin.finapi.category.FpCategory;
2424
import org.proshin.finapi.endpoint.Endpoint;
25+
import org.proshin.finapi.primitives.BigDecimalOf;
2526
import org.proshin.finapi.primitives.optional.OptionalObjectOf;
2627

2728
public final class FpCashFlow implements CashFlow {
@@ -51,17 +52,17 @@ public Optional<Category> category() {
5152

5253
@Override
5354
public BigDecimal income() {
54-
return this.origin.getBigDecimal("income");
55+
return new BigDecimalOf(this.origin, "income").get();
5556
}
5657

5758
@Override
5859
public BigDecimal spending() {
59-
return this.origin.getBigDecimal("spending");
60+
return new BigDecimalOf(this.origin, "spending").get();
6061
}
6162

6263
@Override
6364
public BigDecimal balance() {
64-
return this.origin.getBigDecimal("balance");
65+
return new BigDecimalOf(this.origin, "balance").get();
6566
}
6667

6768
@Override

src/main/java/org/proshin/finapi/category/out/FpCashFlows.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.json.JSONObject;
2121
import org.proshin.finapi.accesstoken.AccessToken;
2222
import org.proshin.finapi.endpoint.Endpoint;
23+
import org.proshin.finapi.primitives.BigDecimalOf;
2324
import org.proshin.finapi.primitives.IterableJsonArray;
2425

2526
public final class FpCashFlows implements CashFlows {
@@ -43,17 +44,17 @@ public FpCashFlows(
4344

4445
@Override
4546
public BigDecimal income() {
46-
return this.origin.getBigDecimal("totalIncome");
47+
return new BigDecimalOf(this.origin, "totalIncome").get();
4748
}
4849

4950
@Override
5051
public BigDecimal spending() {
51-
return this.origin.getBigDecimal("totalSpending");
52+
return new BigDecimalOf(this.origin, "totalSpending").get();
5253
}
5354

5455
@Override
5556
public BigDecimal balance() {
56-
return this.origin.getBigDecimal("totalBalance");
57+
return new BigDecimalOf(this.origin, "totalBalance").get();
5758
}
5859

5960
@Override

src/main/java/org/proshin/finapi/payment/FpPayment.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.json.JSONObject;
2222
import org.proshin.finapi.payment.out.Status;
2323
import org.proshin.finapi.payment.out.Type;
24+
import org.proshin.finapi.primitives.BigDecimalOf;
2425
import org.proshin.finapi.primitives.OffsetDateTimeOf;
2526
import org.proshin.finapi.primitives.optional.OptionalOf;
2627
import org.proshin.finapi.primitives.optional.OptionalOffsetDateTimeOf;
@@ -50,7 +51,7 @@ public Type type() {
5051

5152
@Override
5253
public BigDecimal amount() {
53-
return this.origin.getBigDecimal("amount");
54+
return new BigDecimalOf(this.origin, "amount").get();
5455
}
5556

5657
@Override
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2020 Roman Proshin
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.proshin.finapi.primitives;
17+
18+
import java.math.BigDecimal;
19+
import java.util.function.Supplier;
20+
import org.cactoos.text.FormattedText;
21+
import org.json.JSONObject;
22+
import org.proshin.finapi.primitives.optional.OptionalBigDecimalOf;
23+
24+
public final class BigDecimalOf implements Supplier<BigDecimal> {
25+
26+
private final OptionalBigDecimalOf origin;
27+
private final String field;
28+
29+
public BigDecimalOf(final JSONObject origin, final String name) {
30+
this.origin = new OptionalBigDecimalOf(origin, name);
31+
this.field = name;
32+
}
33+
34+
@Override
35+
public BigDecimal get() {
36+
return this.origin.get()
37+
.orElseThrow(() -> new IllegalStateException(
38+
new FormattedText("Field '%s' cannot be null", this.field).toString()
39+
));
40+
}
41+
}

src/main/java/org/proshin/finapi/primitives/optional/OptionalBigDecimalOf.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public final class OptionalBigDecimalOf implements Supplier<Optional<BigDecimal>
2525
private final Supplier<Optional<BigDecimal>> origin;
2626

2727
public OptionalBigDecimalOf(final JSONObject origin, final String name) {
28-
this(new OptionalOf<>(origin, name, JSONObject::getBigDecimal));
28+
this(new OptionalOf<>(origin, name, (json, key) -> new BigDecimal(json.getNumber(key).toString())));
2929
}
3030

3131
public OptionalBigDecimalOf(final Supplier<Optional<BigDecimal>> origin) {

src/main/java/org/proshin/finapi/transaction/FpTransaction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.proshin.finapi.endpoint.Endpoint;
2727
import org.proshin.finapi.label.FpLabel;
2828
import org.proshin.finapi.label.Label;
29+
import org.proshin.finapi.primitives.BigDecimalOf;
2930
import org.proshin.finapi.primitives.IterableJsonArray;
3031
import org.proshin.finapi.primitives.OffsetDateTimeOf;
3132
import org.proshin.finapi.primitives.optional.OptionalBigDecimalOf;
@@ -88,7 +89,7 @@ public OffsetDateTime finapiBookingDate() {
8889

8990
@Override
9091
public BigDecimal amount() {
91-
return this.origin.getBigDecimal("amount");
92+
return new BigDecimalOf(this.origin, "amount").get();
9293
}
9394

9495
@Override

src/main/java/org/proshin/finapi/transaction/out/FpTransactionsPage.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.json.JSONObject;
2020
import org.proshin.finapi.accesstoken.AccessToken;
2121
import org.proshin.finapi.endpoint.Endpoint;
22+
import org.proshin.finapi.primitives.BigDecimalOf;
2223
import org.proshin.finapi.primitives.paging.FpPage;
2324
import org.proshin.finapi.primitives.paging.Page;
2425
import org.proshin.finapi.primitives.paging.Paging;
@@ -58,17 +59,17 @@ public FpTransactionsPage(final JSONObject origin, final Page<Transaction> page)
5859

5960
@Override
6061
public BigDecimal income() {
61-
return this.origin.getBigDecimal("income");
62+
return new BigDecimalOf(this.origin, "income").get();
6263
}
6364

6465
@Override
6566
public BigDecimal spending() {
66-
return this.origin.getBigDecimal("spending");
67+
return new BigDecimalOf(this.origin, "spending").get();
6768
}
6869

6970
@Override
7071
public BigDecimal balance() {
71-
return this.origin.getBigDecimal("balance");
72+
return new BigDecimalOf(this.origin, "balance").get();
7273
}
7374

7475
@Override
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2018 Roman Proshin
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.proshin.finapi.primitives;
17+
18+
import java.math.BigDecimal;
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
21+
import org.json.JSONException;
22+
import org.json.JSONObject;
23+
import org.junit.jupiter.api.Test;
24+
25+
public final class BigDecimalOfTest {
26+
27+
@Test
28+
public void testJsonDouble() {
29+
assertThat(new BigDecimalOf(new JSONObject().put("key", -12.34), "key").get())
30+
.isEqualTo(new BigDecimal("-12.34"));
31+
}
32+
33+
@Test
34+
public void testJsonInt() {
35+
assertThat(new BigDecimalOf(new JSONObject().put("key", -12), "key").get())
36+
.isEqualTo(new BigDecimal("-12"));
37+
}
38+
39+
@Test
40+
public void testJsonString() {
41+
assertThat(new BigDecimalOf(new JSONObject().put("key", "-12.34"), "key").get())
42+
.isEqualTo(new BigDecimal("-12.34"));
43+
}
44+
45+
@Test
46+
public void testNullValue() {
47+
final BigDecimalOf bigDecimalOf = new BigDecimalOf(new JSONObject(), "key");
48+
assertThatThrownBy(bigDecimalOf::get)
49+
.isInstanceOf(IllegalStateException.class)
50+
.hasMessage("Field 'key' cannot be null");
51+
}
52+
53+
@Test
54+
public void testInvalidValue() {
55+
final BigDecimalOf bigDecimalOf = new BigDecimalOf(new JSONObject().put("key", "abc"), "key");
56+
assertThatThrownBy(bigDecimalOf::get)
57+
.isInstanceOf(JSONException.class)
58+
.hasMessage("JSONObject[\"key\"] is not a number.");
59+
}
60+
}

0 commit comments

Comments
 (0)