Skip to content

Commit

Permalink
Feature ETP-870: Resolved SonarQube suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Emi-Polliotti committed Dec 17, 2024
1 parent 2a0bf37 commit 6faeae9
Show file tree
Hide file tree
Showing 17 changed files with 486 additions and 455 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,17 @@
import org.openbravo.dal.core.OBContext;
import org.openbravo.erpCommon.utility.OBMessageUtils;

/**
* Unit test class for {@link ReportValuationStock#addAdditionalParameters}.
* It validates the correct behavior when additional parameters are added
* to the report generation process.
*/
@RunWith(MockitoJUnitRunner.class)
public class ReportValuationStockAddParametersTest {

/**
* Rule to handle expected exceptions during test execution.
*/
@Rule
public ExpectedException thrown = ExpectedException.none();

Expand Down Expand Up @@ -69,6 +77,11 @@ public class ReportValuationStockAddParametersTest {
private static final String TEST_CURRENCY_ID = "testCurrencyId";
private static final String TEST_DATE = "2024-01-01";

/**
* Set up the test environment, including mocks and static contexts.
*
* @throws ServletException if an error occurs during setup
*/
@Before
public void setUp() throws ServletException {
reportValuationStock = spy(new ReportValuationStock());
Expand Down Expand Up @@ -102,6 +115,9 @@ public void setUp() throws ServletException {
);
}

/**
* Clean up static mocks after each test case execution.
*/
@After
public void tearDown() {
if (mockedRequestContext != null) {
Expand All @@ -118,6 +134,12 @@ public void tearDown() {
}
}

/**
* Tests that additional parameters are added correctly to the report process
* when valid input JSON content is provided.
*
* @throws Exception if an unexpected error occurs during the test
*/
@Test
public void testAddAdditionalParametersValidInput() throws Exception {
Map<String, Object> parameters = new HashMap<>();
Expand All @@ -141,6 +163,4 @@ public void testAddAdditionalParametersValidInput() throws Exception {
assertNotNull("Parameters should not be null", parameters);
assertEquals("PDF", parameters.get("OUTPUT_FORMAT"));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import javax.servlet.ServletException;

import org.apache.commons.lang.StringUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -70,6 +71,8 @@ public class ReportValuationStockBuildDataTest {
private static final String TEST_CATEGORY_ID = "testCategoryId";
private static final String TEST_CURRENCY_ID = "testCurrencyId";
private static final String TEST_CLIENT_ID = "testClientId";
private static final String WAREHOUSE_NOT_IN_LE = "WarehouseNotInLE";


/**
* Sets up the initial state required for the tests. Prepare mocks and retrieves
Expand Down Expand Up @@ -127,9 +130,9 @@ public void testBuildDataWithNullLegalEntity() {
when(mockOsp.getLegalEntity(any(Organization.class))).thenReturn(null);

OBError mockError = mock(OBError.class);
when(mockError.getMessage()).thenReturn("WarehouseNotInLE");
when(mockError.getMessage()).thenReturn(WAREHOUSE_NOT_IN_LE);
obMessageUtilsMock.when(() -> OBMessageUtils.messageBD(anyString()))
.thenReturn("WarehouseNotInLE");
.thenReturn(WAREHOUSE_NOT_IN_LE);
obMessageUtilsMock.when(() -> OBMessageUtils.translateError(anyString()))
.thenReturn(mockError);

Expand All @@ -148,7 +151,7 @@ public void testBuildDataWithNullLegalEntity() {
} catch (Exception e) {
assertTrue("Expected ServletException", e.getCause() instanceof ServletException);
assertTrue("Expected correct error message",
e.getCause().getMessage().contains("WarehouseNotInLE"));
StringUtils.contains(e.getCause().getMessage(), WAREHOUSE_NOT_IN_LE));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ public class ReportValuationStockGetReportTest {
private static final String TEST_CLIENT = "TEST_CLIENT";
private static final String TEST_LANGUAGE = "en_US";

private static final String DATE_NEXT = "dateNext";
private static final String MAX_AGG_DATE = "maxAggDate";
private static final String DATE_FORMAT = "dateFormat";
private static final String ORG_IDS = "orgIds";
private static final String ERROR_RESULT_NULL = "Result should not be null";
private static final String ERROR_DATA_LENGTH = "Should return expected data length";
private static final String ERROR_EXPECTED_DATA = "Should return expected data";


/**
* Sets up the test environment by initializing the required objects
* and configuring mock behaviors.
Expand Down Expand Up @@ -106,12 +115,12 @@ public void testGetReportDataWithCostTypeAndNoWarehouseConsolidation() throws Ex
reportValuationStock,
vars, TEST_DATE, TEST_CATEGORY, TEST_CURRENCY, false, "processTime",
"N", "STA", TEST_WAREHOUSE, readOnlyCP, filterOrg, TEST_ORG,
"orgIds", TEST_ORG, TEST_CLIENT, "dateNext", "maxAggDate", "dateFormat"
ORG_IDS, TEST_ORG, TEST_CLIENT, DATE_NEXT, MAX_AGG_DATE, DATE_FORMAT
);

assertNotNull("Result should not be null", result);
assertEquals("Should return expected data length", expectedData.length, result.length);
assertEquals("Should return expected data", expectedData[0], result[0]);
assertNotNull(ERROR_RESULT_NULL, result);
assertEquals(ERROR_DATA_LENGTH, expectedData.length, result.length);
assertEquals(ERROR_EXPECTED_DATA, expectedData[0], result[0]);
}
}

Expand All @@ -136,12 +145,12 @@ public void testGetReportDataWithoutCostTypeAndWithWarehouseConsolidation() thro
reportValuationStock,
vars, TEST_DATE, TEST_CATEGORY, TEST_CURRENCY, true, "processTime",
"N", null, TEST_WAREHOUSE, readOnlyCP, filterOrg, TEST_ORG,
"orgIds", TEST_ORG, TEST_CLIENT, "dateNext", "maxAggDate", "dateFormat"
ORG_IDS, TEST_ORG, TEST_CLIENT, DATE_NEXT, MAX_AGG_DATE, DATE_FORMAT
);

assertNotNull("Result should not be null", result);
assertEquals("Should return expected data length", expectedData.length, result.length);
assertEquals("Should return expected data", expectedData[0], result[0]);
assertNotNull(ERROR_RESULT_NULL, result);
assertEquals(ERROR_DATA_LENGTH, expectedData.length, result.length);
assertEquals(ERROR_EXPECTED_DATA, expectedData[0], result[0]);
}
}

Expand All @@ -166,12 +175,12 @@ public void testGetReportDataWithoutCostTypeAndNoWarehouseConsolidation() throws
reportValuationStock,
vars, TEST_DATE, TEST_CATEGORY, TEST_CURRENCY, false, "processTime",
"N", null, TEST_WAREHOUSE, readOnlyCP, filterOrg, TEST_ORG,
"orgIds", TEST_ORG, TEST_CLIENT, "dateNext", "maxAggDate", "dateFormat"
ORG_IDS, TEST_ORG, TEST_CLIENT, DATE_NEXT, MAX_AGG_DATE, DATE_FORMAT
);

assertNotNull("Result should not be null", result);
assertEquals("Should return expected data length", expectedData.length, result.length);
assertEquals("Should return expected data", expectedData[0], result[0]);
assertNotNull(ERROR_RESULT_NULL, result);
assertEquals(ERROR_DATA_LENGTH, expectedData.length, result.length);
assertEquals(ERROR_EXPECTED_DATA, expectedData[0], result[0]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public class ReportValuationStockPrintTest {
private static final String TEST_ALGORITHM_NAME = "Average Algorithm";
private static final String TEST_TRANSLATED_HEADER = "Translated Cost Header";
private static final String TEST_TRANSLATED_VALUATION = "Translated Valuation Header";
private static final String NUMBER_FORMAT = "#,##0.00";
private static final String ERROR_PARAMETERS_NULL = "Parameters should not be null";
private static final String NUMBER_FORMAT_KEY = "NUMBERFORMAT";
private static final String ERROR_DECIMAL_FORMAT = "Should have correct decimal format";
private static final String COST_FORMAT_KEY = "COSTFORMAT";


/**
* Sets up the test environment by initializing mocks and reflective access to the method under test.
Expand All @@ -72,7 +78,7 @@ public void setUp() throws Exception {

when(vars.getSessionValue("#AD_ReportDecimalSeparator")).thenReturn(".");
when(vars.getSessionValue("#AD_ReportGroupingSeparator")).thenReturn(",");
when(vars.getSessionValue("#AD_ReportNumberFormat")).thenReturn("#,##0.00");
when(vars.getSessionValue("#AD_ReportNumberFormat")).thenReturn(NUMBER_FORMAT);
when(vars.getJavaDateFormat()).thenReturn("yyyy-MM-dd");

when(costingAlgorithm.getName()).thenReturn(TEST_ALGORITHM_NAME);
Expand All @@ -92,7 +98,7 @@ public void setUp() throws Exception {
public void testPrintReportWithCostingAlgorithm() throws Exception {
ReportValuationStockData[] testData = new ReportValuationStockData[0];
Map<String, Object> parameters = new HashMap<>();
DecimalFormat mockFormat = new DecimalFormat("#,##0.00");
DecimalFormat mockFormat = new DecimalFormat(NUMBER_FORMAT);

try (MockedStatic<OBMessageUtils> obMessageUtilsMock = mockStatic(OBMessageUtils.class);
MockedStatic<Utility> utilityMock = mockStatic(Utility.class)) {
Expand All @@ -117,7 +123,7 @@ public void testPrintReportWithCostingAlgorithm() throws Exception {
parameters
);

assertNotNull("Parameters should not be null", parameters);
assertNotNull(ERROR_PARAMETERS_NULL, parameters);
assertEquals("Should have correct cost header",
TEST_TRANSLATED_HEADER, parameters.get("ALG_COST"));
assertEquals("Should have correct valuation header",
Expand All @@ -127,9 +133,9 @@ public void testPrintReportWithCostingAlgorithm() throws Exception {
assertEquals("Should have correct date",
TEST_DATE, parameters.get("DATE"));
assertNotNull("Should have number format",
parameters.get("NUMBERFORMAT"));
assertEquals("Should have correct decimal format",
mockFormat, parameters.get("COSTFORMAT"));
parameters.get(NUMBER_FORMAT_KEY));
assertEquals(ERROR_DECIMAL_FORMAT,
mockFormat, parameters.get(COST_FORMAT_KEY));
}
}

Expand All @@ -147,7 +153,7 @@ public void testPrintReportWithCostingAlgorithm() throws Exception {
public void testPrintReportWithoutCostingAlgorithm() throws Exception {
ReportValuationStockData[] testData = new ReportValuationStockData[0];
Map<String, Object> parameters = new HashMap<>();
DecimalFormat mockFormat = new DecimalFormat("#,##0.00");
DecimalFormat mockFormat = new DecimalFormat(NUMBER_FORMAT);

try (MockedStatic<Utility> utilityMock = mockStatic(Utility.class)) {
utilityMock.when(() -> Utility.getFormat(any(), anyString())).thenReturn(mockFormat);
Expand All @@ -162,7 +168,7 @@ public void testPrintReportWithoutCostingAlgorithm() throws Exception {
parameters
);

assertNotNull("Parameters should not be null", parameters);
assertNotNull(ERROR_PARAMETERS_NULL, parameters);
assertEquals("Should have empty cost header",
"", parameters.get("ALG_COST"));
assertEquals("Should have empty valuation header",
Expand All @@ -172,9 +178,9 @@ public void testPrintReportWithoutCostingAlgorithm() throws Exception {
assertEquals("Should have correct date",
TEST_DATE, parameters.get("DATE"));
assertNotNull("Should have number format",
parameters.get("NUMBERFORMAT"));
assertEquals("Should have correct decimal format",
mockFormat, parameters.get("COSTFORMAT"));
parameters.get(NUMBER_FORMAT_KEY));
assertEquals(ERROR_DECIMAL_FORMAT,
mockFormat, parameters.get(COST_FORMAT_KEY));
}
}

Expand Down Expand Up @@ -211,11 +217,11 @@ public void testPrintReportWithCustomFormats() throws Exception {
parameters
);

assertNotNull("Parameters should not be null", parameters);
assertNotNull(ERROR_PARAMETERS_NULL, parameters);
assertNotNull("Should have number format with custom separators",
parameters.get("NUMBERFORMAT"));
assertEquals("Should have correct decimal format",
mockFormat, parameters.get("COSTFORMAT"));
parameters.get(NUMBER_FORMAT_KEY));
assertEquals(ERROR_DECIMAL_FORMAT,
mockFormat, parameters.get(COST_FORMAT_KEY));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,19 @@ public class ReportValuationStockSummaryTest {

private Method getSummaryProductCategoriesMethod;

private static final String ERROR_RESULT_NULL = "Result should not be null";
private static final String CATEGORY_NAME = "categoryName";
private static final String TEST_CATEGORY = "TestCategory";
private static final String TOTAL_COST = "totalCost";
private static final String TEST_COST_VALUE = "100.00";
private static final String ERROR_ONE_CATEGORY = "Should contain one category";


/**
* Sets up the test environment by initializing reflective access to the method under test.
*
* @throws Exception if the method cannot be accessed.
*/

@Before
public void setUp() throws Exception {
getSummaryProductCategoriesMethod = ReportValuationStock.class.getDeclaredMethod(
Expand Down Expand Up @@ -76,7 +83,7 @@ public void testGetSummaryProductCategoriesWithEmptyData() throws Exception {
(Object) data
);

assertNotNull("Result should not be null", result);
assertNotNull(ERROR_RESULT_NULL, result);
assertEquals("Empty data should return empty result", 0, result.length);
}

Expand All @@ -92,8 +99,8 @@ public void testGetSummaryProductCategoriesWithEmptyData() throws Exception {
@Test
public void testGetSummaryProductCategoriesWithSingleCategory() throws Exception {
ReportValuationStockData singleData = new ReportValuationStockData();
setFieldValue(singleData, "categoryName", "TestCategory");
setFieldValue(singleData, "totalCost", "100.00");
setFieldValue(singleData, CATEGORY_NAME, TEST_CATEGORY);
setFieldValue(singleData, TOTAL_COST, TEST_COST_VALUE);

ReportValuationStockData[] data = new ReportValuationStockData[] { singleData };

Expand All @@ -102,11 +109,11 @@ public void testGetSummaryProductCategoriesWithSingleCategory() throws Exception
(Object) data
);

assertNotNull("Result should not be null", result);
assertEquals("Should contain one category", 1, result.length);
assertEquals("Should have correct category name", "TestCategory",
assertNotNull(ERROR_RESULT_NULL, result);
assertEquals(ERROR_ONE_CATEGORY, 1, result.length);
assertEquals("Should have correct category name", TEST_CATEGORY,
result[0].getField("category"));
assertEquals("Should have correct cost", "100.00",
assertEquals("Should have correct cost", TEST_COST_VALUE,
result[0].getField("cost"));
}

Expand All @@ -122,12 +129,12 @@ public void testGetSummaryProductCategoriesWithSingleCategory() throws Exception
@Test
public void testGetSummaryProductCategoriesWithMultipleEntriesSameCategory() throws Exception {
ReportValuationStockData data1 = new ReportValuationStockData();
setFieldValue(data1, "categoryName", "TestCategory");
setFieldValue(data1, "totalCost", "100.00");
setFieldValue(data1, CATEGORY_NAME, TEST_CATEGORY);
setFieldValue(data1, TOTAL_COST, TEST_COST_VALUE);

ReportValuationStockData data2 = new ReportValuationStockData();
setFieldValue(data2, "categoryName", "TestCategory");
setFieldValue(data2, "totalCost", "50.00");
setFieldValue(data2, CATEGORY_NAME, TEST_CATEGORY);
setFieldValue(data2, TOTAL_COST, "50.00");

ReportValuationStockData[] data = new ReportValuationStockData[] { data1, data2 };

Expand All @@ -136,9 +143,9 @@ public void testGetSummaryProductCategoriesWithMultipleEntriesSameCategory() thr
(Object) data
);

assertNotNull("Result should not be null", result);
assertEquals("Should contain one category", 1, result.length);
assertEquals("Category name should match", "TestCategory",
assertNotNull(ERROR_RESULT_NULL, result);
assertEquals(ERROR_ONE_CATEGORY, 1, result.length);
assertEquals("Category name should match", TEST_CATEGORY,
result[0].getField("category"));
assertEquals("Total cost should be summed correctly", "150.00",
result[0].getField("cost"));
Expand All @@ -156,8 +163,8 @@ public void testGetSummaryProductCategoriesWithMultipleEntriesSameCategory() thr
@Test
public void testGetSummaryProductCategoriesWithNullCosts() throws Exception {
ReportValuationStockData nullCostData = new ReportValuationStockData();
setFieldValue(nullCostData, "categoryName", "TestCategory");
setFieldValue(nullCostData, "totalCost", null);
setFieldValue(nullCostData, CATEGORY_NAME, TEST_CATEGORY);
setFieldValue(nullCostData, TOTAL_COST, null);

ReportValuationStockData[] data = new ReportValuationStockData[] { nullCostData };

Expand All @@ -166,8 +173,8 @@ public void testGetSummaryProductCategoriesWithNullCosts() throws Exception {
(Object) data
);

assertNotNull("Result should not be null", result);
assertEquals("Should contain one category", 1, result.length);
assertNotNull(ERROR_RESULT_NULL, result);
assertEquals(ERROR_ONE_CATEGORY, 1, result.length);
assertEquals("Cost should be zero for null values", "0",
result[0].getField("cost"));
}
Expand Down
Loading

0 comments on commit 6faeae9

Please sign in to comment.