Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.core.api;

import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class ApiParameterHelperTest {

@Test
void givenMissingOrBlankCommandId_whenExtractingCommandId_thenReturnsNull() {
MultivaluedMap<String, String> params = queryParams();
Assertions.assertNull(ApiParameterHelper.commandId(params));

params = queryParams("commandId", " ");
Assertions.assertNull(ApiParameterHelper.commandId(params));
}

@Test
void givenValidCommandId_whenExtractingCommandId_thenReturnsLongValue() {
MultivaluedMap<String, String> params = queryParams("commandId", "42");

Assertions.assertEquals(42L, ApiParameterHelper.commandId(params));
}

@Test
void givenNonNumericCommandId_whenExtractingCommandId_thenThrowsNumberFormatException() {
MultivaluedMap<String, String> params = queryParams("commandId", "abc");

Assertions.assertThrows(NumberFormatException.class, () -> ApiParameterHelper.commandId(params));
}

@Test
void givenMissingOrBlankFields_whenExtractingFields_thenReturnsEmptySet() {
Assertions.assertTrue(ApiParameterHelper.extractFieldsForResponseIfProvided(queryParams()).isEmpty());
Assertions.assertTrue(ApiParameterHelper.extractFieldsForResponseIfProvided(queryParams("fields", " ")).isEmpty());
}

@Test
void givenCommaSeparatedFields_whenExtractingFields_thenReturnsTrimmedDistinctSet() {
MultivaluedMap<String, String> params = queryParams("fields", "id, name, id");

Assertions.assertEquals(Set.of("id", "name"), ApiParameterHelper.extractFieldsForResponseIfProvided(params));
}

@Test
void givenMissingOrBlankAssociations_whenExtractingAssociations_thenReturnsEmptySet() {
Assertions.assertTrue(ApiParameterHelper.extractAssociationsForResponseIfProvided(queryParams()).isEmpty());
Assertions.assertTrue(ApiParameterHelper.extractAssociationsForResponseIfProvided(queryParams("associations", " ")).isEmpty());
}

@Test
void givenCommaSeparatedAssociations_whenExtractingAssociations_thenReturnsTrimmedSet() {
MultivaluedMap<String, String> params = queryParams("associations", "charges, repayments");

Assertions.assertEquals(Set.of("charges", "repayments"), ApiParameterHelper.extractAssociationsForResponseIfProvided(params));
}

@Test
void givenExcludeString_whenExcludingAssociations_thenRemovesOnlyRequestedValues() {
Set<String> fields = new HashSet<>(Set.of("charges", "repayments", "guarantors"));

ApiParameterHelper.excludeAssociationsForResponseIfProvided("charges, guarantors", fields);

Assertions.assertEquals(Set.of("repayments"), fields);
}

@Test
void givenBlankExcludeString_whenExcludingAssociations_thenKeepsOriginalSet() {
Set<String> fields = new HashSet<>(Set.of("charges", "repayments"));

ApiParameterHelper.excludeAssociationsForResponseIfProvided(" ", fields);

Assertions.assertEquals(Set.of("charges", "repayments"), fields);
}

@Test
void givenExcludeQueryParam_whenExcludingAssociations_thenRemovesRequestedValues() {
Set<String> fields = new HashSet<>(Set.of("charges", "repayments", "guarantors"));
MultivaluedMap<String, String> params = queryParams("exclude", "repayments");

ApiParameterHelper.excludeAssociationsForResponseIfProvided(params, fields);

Assertions.assertEquals(Set.of("charges", "guarantors"), fields);
}

@Test
void givenMissingExcludeQueryParam_whenExcludingAssociations_thenDoesNothing() {
Set<String> fields = new HashSet<>(Set.of("charges", "repayments"));

ApiParameterHelper.excludeAssociationsForResponseIfProvided(queryParams(), fields);

Assertions.assertEquals(Set.of("charges", "repayments"), fields);
}

@Test
void givenMissingLocale_whenExtractingLocale_thenReturnsNull() {
Assertions.assertNull(ApiParameterHelper.extractLocale(queryParams()));
}

@Test
void givenValidLocale_whenExtractingLocale_thenReturnsParsedLocale() {
Locale locale = ApiParameterHelper.extractLocale(queryParams("locale", "en_US"));

Assertions.assertEquals(Locale.forLanguageTag("en-US"), locale);
}

@Test
void givenBlankLocale_whenExtractingLocale_thenThrowsValidationException() {
MultivaluedMap<String, String> params = queryParams("locale", " ");

Assertions.assertThrows(PlatformApiDataValidationException.class, () -> ApiParameterHelper.extractLocale(params));
}

@Test
void givenInvalidLocale_whenExtractingLocale_thenThrowsValidationException() {
MultivaluedMap<String, String> params = queryParams("locale", "zz_US");

Assertions.assertThrows(PlatformApiDataValidationException.class, () -> ApiParameterHelper.extractLocale(params));
}

@Test
void givenParameterTypeQueryParam_whenParsingBoolean_thenRespectsTrueFalseAndMissing() {
Assertions.assertTrue(ApiParameterHelper.parameterType(queryParams("parameterType", "TRUE")));
Assertions.assertFalse(ApiParameterHelper.parameterType(queryParams("parameterType", "false")));
Assertions.assertFalse(ApiParameterHelper.parameterType(queryParams()));
}

@Test
void givenTemplateQueryParam_whenParsingBoolean_thenRespectsTrueFalseAndMissing() {
Assertions.assertTrue(ApiParameterHelper.template(queryParams("template", "TrUe")));
Assertions.assertFalse(ApiParameterHelper.template(queryParams("template", "no")));
Assertions.assertFalse(ApiParameterHelper.template(queryParams()));
}

@Test
void givenMakerCheckerableQueryParam_whenParsingBoolean_thenRespectsTrueFalseAndMissing() {
Assertions.assertTrue(ApiParameterHelper.makerCheckerable(queryParams("makerCheckerable", "TRUE")));
Assertions.assertFalse(ApiParameterHelper.makerCheckerable(queryParams("makerCheckerable", "0")));
Assertions.assertFalse(ApiParameterHelper.makerCheckerable(queryParams()));
}

@Test
void givenIncludeJsonQueryParam_whenParsingBoolean_thenRespectsTrueFalseAndMissing() {
Assertions.assertTrue(ApiParameterHelper.includeJson(queryParams("includeJson", "true")));
Assertions.assertFalse(ApiParameterHelper.includeJson(queryParams("includeJson", "False")));
Assertions.assertFalse(ApiParameterHelper.includeJson(queryParams()));
}

@Test
void givenGenericResultSetQueryParam_whenParsingBoolean_thenRespectsTrueFalseAndMissing() {
Assertions.assertTrue(ApiParameterHelper.genericResultSet(queryParams("genericResultSet", "TRUE")));
Assertions.assertFalse(ApiParameterHelper.genericResultSet(queryParams("genericResultSet", "false")));
Assertions.assertFalse(ApiParameterHelper.genericResultSet(queryParams()));
}

@Test
void givenGenericResultSetPresence_whenCheckingPassedFlag_thenDependsOnlyOnPresence() {
Assertions.assertTrue(ApiParameterHelper.genericResultSetPassed(queryParams("genericResultSet", "false")));
Assertions.assertFalse(ApiParameterHelper.genericResultSetPassed(queryParams()));
}

// Tiny helper so each test can declare params inline and stay readable.
private static MultivaluedMap<String, String> queryParams(String... keyValuePairs) {
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
for (int i = 0; i < keyValuePairs.length; i += 2) {
params.add(keyValuePairs[i], keyValuePairs[i + 1]);
}
return params;
}
}