Skip to content

Commit 00e9ac8

Browse files
Aman-Mittaladamsaghy
authored andcommitted
FINERACT-2346: Refractor, Minor Sonar fixes and remove some deprecated code
1 parent 73d8659 commit 00e9ac8

File tree

17 files changed

+83
-123
lines changed

17 files changed

+83
-123
lines changed

fineract-client-feign/src/test/java/org/apache/fineract/client/feign/integration/FineractFeignClientIntegrationTest.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import static com.github.tomakehurst.wiremock.client.WireMock.get;
2525
import static com.github.tomakehurst.wiremock.client.WireMock.post;
2626
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
27+
import static com.github.tomakehurst.wiremock.common.ContentTypes.APPLICATION_JSON;
28+
import static com.github.tomakehurst.wiremock.common.ContentTypes.AUTHORIZATION;
29+
import static com.github.tomakehurst.wiremock.common.ContentTypes.CONTENT_TYPE;
2730
import static org.assertj.core.api.Assertions.assertThat;
2831
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2932

@@ -66,9 +69,8 @@ void tearDown() {
6669

6770
@Test
6871
void testJsonEncoding() {
69-
wireMockServer
70-
.stubFor(post(urlEqualTo("/test")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")
71-
.withBody("{\"id\":1,\"message\":\"success\",\"timestamp\":\"2024-01-15T10:30:00\"}")));
72+
wireMockServer.stubFor(post(urlEqualTo("/test")).willReturn(aResponse().withStatus(200).withHeader(CONTENT_TYPE, APPLICATION_JSON)
73+
.withBody("{\"id\":1,\"message\":\"success\",\"timestamp\":\"2024-01-15T10:30:00\"}")));
7274

7375
TestApi client = config.createClient(TestApi.class);
7476
TestRequest request = new TestRequest();
@@ -85,9 +87,8 @@ void testJsonEncoding() {
8587

8688
@Test
8789
void testJsonDecoding() {
88-
wireMockServer
89-
.stubFor(get(urlEqualTo("/test")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")
90-
.withBody("{\"id\":42,\"message\":\"decoded-message\",\"timestamp\":\"2024-12-25T18:45:30\"}")));
90+
wireMockServer.stubFor(get(urlEqualTo("/test")).willReturn(aResponse().withStatus(200).withHeader(CONTENT_TYPE, APPLICATION_JSON)
91+
.withBody("{\"id\":42,\"message\":\"decoded-message\",\"timestamp\":\"2024-12-25T18:45:30\"}")));
9192

9293
TestApi client = config.createClient(TestApi.class);
9394
TestResponse response = client.getTest();
@@ -101,7 +102,7 @@ void testJsonDecoding() {
101102
@Test
102103
void testJava8DateTimeSerialization() {
103104
wireMockServer.stubFor(post(urlEqualTo("/test")).withRequestBody(containing("2024-06-30"))
104-
.willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")
105+
.willReturn(aResponse().withStatus(200).withHeader(CONTENT_TYPE, APPLICATION_JSON)
105106
.withBody("{\"id\":99,\"message\":\"date-test\",\"timestamp\":\"2024-06-30T12:00:00\"}")));
106107

107108
TestApi client = config.createClient(TestApi.class);
@@ -131,7 +132,7 @@ void testErrorDecoder() {
131132
@Test
132133
void testErrorDecoderWithServerError() {
133134
wireMockServer.stubFor(
134-
get(urlEqualTo("/error")).willReturn(aResponse().withStatus(500).withHeader("Content-Type", "application/json").withBody(
135+
get(urlEqualTo("/error")).willReturn(aResponse().withStatus(500).withHeader(CONTENT_TYPE, APPLICATION_JSON).withBody(
135136
"{\"developerMessage\":\"Internal server error occurred\",\"httpStatusCode\":\"500\",\"defaultUserMessage\":\"Server Error\"}")));
136137

137138
TestApi client = config.createClient(TestApi.class);
@@ -144,9 +145,8 @@ void testConnectionTimeToLiveConfiguration() {
144145
FineractFeignClientConfig ttlConfig = FineractFeignClientConfig.builder().baseUrl(baseUrl).credentials("testuser", "testpass")
145146
.connectionTimeToLive(5, TimeUnit.MINUTES).build();
146147

147-
wireMockServer
148-
.stubFor(get(urlEqualTo("/test")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")
149-
.withBody("{\"id\":1,\"message\":\"ttl-test\",\"timestamp\":\"2024-01-01T00:00:00\"}")));
148+
wireMockServer.stubFor(get(urlEqualTo("/test")).willReturn(aResponse().withStatus(200).withHeader(CONTENT_TYPE, APPLICATION_JSON)
149+
.withBody("{\"id\":1,\"message\":\"ttl-test\",\"timestamp\":\"2024-01-01T00:00:00\"}")));
150150

151151
TestApi client = ttlConfig.createClient(TestApi.class);
152152
TestResponse response = client.getTest();
@@ -157,8 +157,8 @@ void testConnectionTimeToLiveConfiguration() {
157157

158158
@Test
159159
void testBasicAuthentication() {
160-
wireMockServer.stubFor(get(urlEqualTo("/test")).withHeader("Authorization", equalTo("Basic dGVzdHVzZXI6dGVzdHBhc3M="))
161-
.willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")
160+
wireMockServer.stubFor(get(urlEqualTo("/test")).withHeader(AUTHORIZATION, equalTo("Basic dGVzdHVzZXI6dGVzdHBhc3M="))
161+
.willReturn(aResponse().withStatus(200).withHeader(CONTENT_TYPE, APPLICATION_JSON)
162162
.withBody("{\"id\":1,\"message\":\"authenticated\",\"timestamp\":\"2024-01-01T00:00:00\"}")));
163163

164164
TestApi client = config.createClient(TestApi.class);
@@ -170,9 +170,8 @@ void testBasicAuthentication() {
170170

171171
@Test
172172
void testNullValueHandling() {
173-
wireMockServer
174-
.stubFor(post(urlEqualTo("/test")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")
175-
.withBody("{\"id\":1,\"message\":null,\"timestamp\":\"2024-01-01T00:00:00\"}")));
173+
wireMockServer.stubFor(post(urlEqualTo("/test")).willReturn(aResponse().withStatus(200).withHeader(CONTENT_TYPE, APPLICATION_JSON)
174+
.withBody("{\"id\":1,\"message\":null,\"timestamp\":\"2024-01-01T00:00:00\"}")));
176175

177176
TestApi client = config.createClient(TestApi.class);
178177
TestRequest request = new TestRequest();

fineract-client-feign/src/test/java/org/apache/fineract/client/services/ImagesApiIntegrationTest.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static com.github.tomakehurst.wiremock.client.WireMock.get;
2525
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
2626
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
27+
import static com.github.tomakehurst.wiremock.common.ContentTypes.CONTENT_TYPE;
2728
import static org.assertj.core.api.Assertions.assertThat;
2829

2930
import com.github.tomakehurst.wiremock.WireMockServer;
@@ -75,14 +76,14 @@ void tearDown() {
7576
void testRetrieveClientImage() throws IOException {
7677
byte[] imageData = new byte[] { (byte) 0xFF, (byte) 0xD8, (byte) 0xFF, 0x10, 0x11, 0x12 };
7778
wireMockServer.stubFor(get(urlEqualTo("/v1/clients/123/images"))
78-
.willReturn(aResponse().withStatus(200).withHeader("Content-Type", "image/jpeg").withBody(imageData)));
79+
.willReturn(aResponse().withStatus(200).withHeader(CONTENT_TYPE, "image/jpeg").withBody(imageData)));
7980

8081
ImagesApi api = config.createClient(ImagesApi.class);
8182
Response response = api.get("clients", 123L, new HashMap<>());
8283

8384
assertThat(response.status()).isEqualTo(200);
84-
assertThat(response.headers()).containsKey("Content-Type");
85-
assertThat(response.headers().get("Content-Type")).contains("image/jpeg");
85+
assertThat(response.headers()).containsKey(CONTENT_TYPE);
86+
assertThat(response.headers().get(CONTENT_TYPE)).contains("image/jpeg");
8687
assertThat(response.body().asInputStream().readAllBytes()).isEqualTo(imageData);
8788
}
8889

@@ -91,7 +92,7 @@ void testRetrieveClientImageWithMaxDimensions() throws IOException {
9192
byte[] resizedImage = new byte[] { (byte) 0xFF, (byte) 0xD8, (byte) 0xFF, 0x20, 0x21 };
9293
wireMockServer.stubFor(get(urlPathEqualTo("/v1/clients/123/images")).withQueryParam("maxWidth", equalTo("100"))
9394
.withQueryParam("maxHeight", equalTo("100"))
94-
.willReturn(aResponse().withStatus(200).withHeader("Content-Type", "image/jpeg").withBody(resizedImage)));
95+
.willReturn(aResponse().withStatus(200).withHeader(CONTENT_TYPE, "image/jpeg").withBody(resizedImage)));
9596

9697
ImagesApi api = config.createClient(ImagesApi.class);
9798
Map<String, Object> params = new HashMap<>();
@@ -108,7 +109,7 @@ void testRetrieveClientImageWithMaxDimensions() throws IOException {
108109
void testRetrieveClientImageWithMaxWidthOnly() throws IOException {
109110
byte[] resizedImage = new byte[] { (byte) 0xFF, (byte) 0xD8, (byte) 0xFF, 0x30 };
110111
wireMockServer.stubFor(get(urlPathEqualTo("/v1/clients/123/images")).withQueryParam("maxWidth", equalTo("200"))
111-
.willReturn(aResponse().withStatus(200).withHeader("Content-Type", "image/jpeg").withBody(resizedImage)));
112+
.willReturn(aResponse().withStatus(200).withHeader(CONTENT_TYPE, "image/jpeg").withBody(resizedImage)));
112113

113114
ImagesApi api = config.createClient(ImagesApi.class);
114115
Map<String, Object> params = new HashMap<>();
@@ -123,7 +124,7 @@ void testRetrieveClientImageWithMaxWidthOnly() throws IOException {
123124
@Test
124125
void testDeleteClientImage() {
125126
wireMockServer.stubFor(delete(urlEqualTo("/v1/clients/123/images"))
126-
.willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody("{\"resourceId\":123}")));
127+
.willReturn(aResponse().withStatus(200).withHeader(CONTENT_TYPE, "application/json").withBody("{\"resourceId\":123}")));
127128

128129
ImagesApi api = config.createClient(ImagesApi.class);
129130
Response response = api.delete("clients", 123L);
@@ -134,7 +135,7 @@ void testDeleteClientImage() {
134135
@Test
135136
void testDeleteStaffImage() {
136137
wireMockServer.stubFor(delete(urlEqualTo("/v1/staff/456/images"))
137-
.willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody("{\"resourceId\":456}")));
138+
.willReturn(aResponse().withStatus(200).withHeader(CONTENT_TYPE, "application/json").withBody("{\"resourceId\":456}")));
138139

139140
ImagesApi api = config.createClient(ImagesApi.class);
140141
Response response = api.delete("staff", 456L);
@@ -146,13 +147,13 @@ void testDeleteStaffImage() {
146147
void testRetrieveImageWithPngFormat() throws IOException {
147148
byte[] pngImage = new byte[] { (byte) 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A };
148149
wireMockServer.stubFor(get(urlEqualTo("/v1/clients/123/images"))
149-
.willReturn(aResponse().withStatus(200).withHeader("Content-Type", "image/png").withBody(pngImage)));
150+
.willReturn(aResponse().withStatus(200).withHeader(CONTENT_TYPE, "image/png").withBody(pngImage)));
150151

151152
ImagesApi api = config.createClient(ImagesApi.class);
152153
Response response = api.get("clients", 123L, new HashMap<>());
153154

154155
assertThat(response.status()).isEqualTo(200);
155-
assertThat(response.headers().get("Content-Type")).contains("image/png");
156+
assertThat(response.headers().get(CONTENT_TYPE)).contains("image/png");
156157
assertThat(response.body().asInputStream().readAllBytes()).isEqualTo(pngImage);
157158
}
158159

@@ -166,7 +167,7 @@ void testMultipleEntityTypes() {
166167
Long entityId = entityIds[i];
167168

168169
wireMockServer.stubFor(get(urlEqualTo("/v1/" + entityType + "/" + entityId + "/images"))
169-
.willReturn(aResponse().withStatus(200).withHeader("Content-Type", "image/jpeg").withBody(new byte[] { 0x01 })));
170+
.willReturn(aResponse().withStatus(200).withHeader(CONTENT_TYPE, "image/jpeg").withBody(new byte[] { 0x01 })));
170171
}
171172

172173
ImagesApi api = config.createClient(ImagesApi.class);

fineract-core/src/main/java/org/apache/fineract/accounting/glaccount/api/GLAccountJsonInputParams.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static Set<String> getAllValues() {
5757

5858
@Override
5959
public String toString() {
60-
return name().toString().replaceAll("_", " ");
60+
return name().replace("_", " ");
6161
}
6262

6363
public String getValue() {

fineract-core/src/main/java/org/apache/fineract/accounting/glaccount/domain/GLAccountType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public static int getMaxValue() {
116116

117117
@Override
118118
public String toString() {
119-
return name().toString();
119+
return name();
120120
}
121121

122122
public boolean isAssetType() {

fineract-core/src/main/java/org/apache/fineract/infrastructure/core/api/IdTypeResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static <T extends Enum<T>> T resolve(@NotNull Class<T> clazz, String idTy
6464
}
6565

6666
public static String formatIdType(String idType) {
67-
return idType == null ? null : idType.replaceAll("-", "_").toUpperCase();
67+
return idType == null ? null : idType.replace("-", "_").toUpperCase();
6868
}
6969

7070
public static RuntimeException resolveFailed(String idType, Exception e) {

fineract-core/src/main/java/org/apache/fineract/infrastructure/core/filters/IdempotencyStoreFilter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull Ht
5454
extractIdempotentKeyFromHttpServletRequest(request).ifPresent(idempotentKey -> fineractRequestContextHolder
5555
.setAttribute(SynchronousCommandProcessingService.IDEMPOTENCY_KEY_ATTRIBUTE, idempotentKey, request));
5656

57-
filterChain.doFilter(request, wrapper.getValue() != null ? wrapper.getValue() : response);
57+
filterChain.doFilter(request, wrapper.get() != null ? wrapper.get() : response);
5858
Optional<Long> commandId = helper.getCommandId(request);
59-
boolean isSuccessWithoutStored = commandId.isPresent() && wrapper.getValue() != null && helper.isStoreIdempotencyKey(request)
59+
boolean isSuccessWithoutStored = commandId.isPresent() && wrapper.get() != null && helper.isStoreIdempotencyKey(request)
6060
&& helper.isAllowedContentTypeResponse(response);
6161
if (isSuccessWithoutStored) {
62-
helper.storeCommandResult(response.getStatus(), Optional.ofNullable(wrapper.getValue())
62+
helper.storeCommandResult(response.getStatus(), Optional.ofNullable(wrapper.get())
6363
.map(ContentCachingResponseWrapper::getContentAsByteArray).map(s -> new String(s, StandardCharsets.UTF_8)).orElse(null),
6464
commandId.get());
6565
}
66-
if (wrapper.getValue() != null) {
67-
wrapper.getValue().copyBodyToResponse();
66+
if (wrapper.get() != null) {
67+
wrapper.get().copyBodyToResponse();
6868
}
6969
}
7070

fineract-core/src/main/java/org/apache/fineract/infrastructure/core/serialization/JsonParserHelper.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ public Integer convertToInteger(final String numericalValueFormatted, final Stri
642642
// http://bugs.sun.com/view_bug.do?bug_id=4510618
643643
final char groupingSeparator = symbols.getGroupingSeparator();
644644
if (groupingSeparator == '\u00a0') {
645-
source = source.replaceAll(" ", Character.toString('\u00a0'));
645+
source = source.replace(" ", Character.toString('\u00a0'));
646646
}
647647

648648
final Number parsedNumber = df.parse(source);
@@ -727,9 +727,8 @@ public BigDecimal convertFrom(final String numericalValueFormatted, final String
727727
// http://bugs.sun.com/view_bug.do?bug_id=4510618
728728
final char groupingSeparator = symbols.getGroupingSeparator();
729729
if (groupingSeparator == '\u00a0') {
730-
source = source.replaceAll(" ", Character.toString('\u00a0'));
730+
source = source.replace(" ", Character.toString('\u00a0'));
731731
}
732-
733732
final NumberStyleFormatter numberFormatter = new NumberStyleFormatter();
734733
final Number parsedNumber = numberFormatter.parse(source, clientApplicationLocale);
735734
if (parsedNumber instanceof BigDecimal) {

fineract-core/src/main/java/org/apache/fineract/portfolio/client/domain/LegalForm.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,11 @@ public enum LegalForm {
4141

4242
public static LegalForm fromInt(final Integer type) {
4343

44-
LegalForm legalForm = null;
45-
switch (type) {
46-
case 1:
47-
legalForm = LegalForm.PERSON;
48-
break;
49-
case 2:
50-
legalForm = LegalForm.ENTITY;
51-
break;
52-
}
53-
return legalForm;
44+
return switch (type) {
45+
case 1 -> LegalForm.PERSON;
46+
case 2 -> LegalForm.ENTITY;
47+
default -> null;
48+
};
5449
}
5550

5651
public boolean isPerson() {

fineract-document/src/main/java/org/apache/fineract/infrastructure/documentmanagement/api/ImagesApiResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public enum EntityTypeForImages {
207207

208208
@Override
209209
public String toString() {
210-
return name().toString().toLowerCase();
210+
return name().toLowerCase();
211211
}
212212
}
213213

fineract-document/src/main/java/org/apache/fineract/infrastructure/documentmanagement/contentrepository/FileSystemContentRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public StorageType getStorageType() {
128128
*/
129129
private String generateFileParentDirectory(final String entityType, final Long entityId) {
130130
return fineractProperties.getContent().getFilesystem().getRootFolder() + File.separator
131-
+ ThreadLocalContextUtil.getTenant().getName().replaceAll(" ", "").trim() + File.separator + "documents" + File.separator
131+
+ ThreadLocalContextUtil.getTenant().getName().replace(" ", "").trim() + File.separator + "documents" + File.separator
132132
+ entityType + File.separator + entityId + File.separator + ContentRepositoryUtils.generateRandomString();
133133
}
134134

@@ -138,7 +138,7 @@ private String generateFileParentDirectory(final String entityType, final Long e
138138
*/
139139
private String generateClientImageParentDirectory(final Long resourceId) {
140140
return fineractProperties.getContent().getFilesystem().getRootFolder() + File.separator
141-
+ ThreadLocalContextUtil.getTenant().getName().replaceAll(" ", "").trim() + File.separator + "images" + File.separator
141+
+ ThreadLocalContextUtil.getTenant().getName().replace(" ", "").trim() + File.separator + "images" + File.separator
142142
+ "clients" + File.separator + resourceId;
143143
}
144144

0 commit comments

Comments
 (0)