|
| 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 | + */ |
| 19 | +package org.apache.fineract.portfolio.client.service; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 24 | +import static org.mockito.ArgumentMatchers.any; |
| 25 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 26 | +import static org.mockito.ArgumentMatchers.anyString; |
| 27 | +import static org.mockito.Mockito.mock; |
| 28 | +import static org.mockito.Mockito.when; |
| 29 | + |
| 30 | +import java.util.Collections; |
| 31 | +import org.apache.fineract.infrastructure.codes.service.CodeValueReadPlatformService; |
| 32 | +import org.apache.fineract.infrastructure.core.service.PaginationHelper; |
| 33 | +import org.apache.fineract.infrastructure.core.service.database.DatabaseSpecificSQLGenerator; |
| 34 | +import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext; |
| 35 | +import org.apache.fineract.infrastructure.security.utils.ColumnValidator; |
| 36 | +import org.apache.fineract.portfolio.client.data.ClientData; |
| 37 | +import org.apache.fineract.portfolio.client.domain.Client; |
| 38 | +import org.apache.fineract.portfolio.client.domain.ClientRepositoryWrapper; |
| 39 | +import org.apache.fineract.portfolio.client.exception.ClientNotFoundException; |
| 40 | +import org.apache.fineract.portfolio.client.mapper.ClientMapper; |
| 41 | +import org.apache.fineract.portfolio.collateralmanagement.domain.ClientCollateralManagementRepositoryWrapper; |
| 42 | +import org.junit.jupiter.api.Test; |
| 43 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 44 | +import org.mockito.InjectMocks; |
| 45 | +import org.mockito.Mock; |
| 46 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 47 | +import org.springframework.dao.EmptyResultDataAccessException; |
| 48 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 49 | +import org.springframework.jdbc.core.RowMapper; |
| 50 | + |
| 51 | +@ExtendWith(MockitoExtension.class) |
| 52 | +class ClientReadPlatformServiceImplTest { |
| 53 | + |
| 54 | + @Mock |
| 55 | + private JdbcTemplate jdbcTemplate; |
| 56 | + @Mock |
| 57 | + private PlatformSecurityContext context; |
| 58 | + @Mock |
| 59 | + private CodeValueReadPlatformService codeValueReadPlatformService; |
| 60 | + @Mock |
| 61 | + private PaginationHelper paginationHelper; |
| 62 | + @Mock |
| 63 | + private DatabaseSpecificSQLGenerator sqlGenerator; |
| 64 | + @Mock |
| 65 | + private ColumnValidator columnValidator; |
| 66 | + @Mock |
| 67 | + private ClientCollateralManagementRepositoryWrapper collateralRepoWrapper; |
| 68 | + @Mock |
| 69 | + private ClientRepositoryWrapper clientRepositoryWrapper; |
| 70 | + @Mock |
| 71 | + private ClientMapper clientMapper; |
| 72 | + |
| 73 | + @InjectMocks |
| 74 | + private ClientReadPlatformServiceImpl clientReadPlatformService; |
| 75 | + |
| 76 | + @Test |
| 77 | + void testRetrieveOne_Success() { |
| 78 | + // Arrange |
| 79 | + Long clientId = 1L; |
| 80 | + String mockHierarchy = "Root/"; |
| 81 | + Client mockClientEntity = mock(Client.class); |
| 82 | + |
| 83 | + // FIX: Using .lookup() instead of .builder() to match Fineract's ClientData patterns |
| 84 | + ClientData mockClientData = ClientData.lookup(clientId, "Test Client", 1L, "Test Office"); |
| 85 | + |
| 86 | + // Stubbing dependencies |
| 87 | + when(context.officeHierarchy()).thenReturn(mockHierarchy); |
| 88 | + when(clientRepositoryWrapper.getClientByClientIdAndHierarchy(clientId, mockHierarchy + "%")).thenReturn(mockClientEntity); |
| 89 | + when(clientMapper.map(mockClientEntity)).thenReturn(mockClientData); |
| 90 | + when(collateralRepoWrapper.getCollateralsPerClient(clientId)).thenReturn(Collections.emptyList()); |
| 91 | + |
| 92 | + // Mock the groups query to return an empty list |
| 93 | + when(jdbcTemplate.query(anyString(), any(RowMapper.class), anyLong())).thenReturn(Collections.emptyList()); |
| 94 | + |
| 95 | + // Act |
| 96 | + ClientData result = clientReadPlatformService.retrieveOne(clientId); |
| 97 | + |
| 98 | + // Assert |
| 99 | + assertNotNull(result); |
| 100 | + assertEquals(clientId, result.getId()); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void testRetrieveOne_ClientNotFound_ThrowsException() { |
| 105 | + // Arrange |
| 106 | + Long clientId = 99L; |
| 107 | + when(context.officeHierarchy()).thenReturn("Root/"); |
| 108 | + |
| 109 | + // Simulate database "Not Found" error |
| 110 | + when(clientRepositoryWrapper.getClientByClientIdAndHierarchy(anyLong(), anyString())) |
| 111 | + .thenThrow(new EmptyResultDataAccessException(1)); |
| 112 | + |
| 113 | + // Act & Assert |
| 114 | + assertThrows(ClientNotFoundException.class, () -> { |
| 115 | + clientReadPlatformService.retrieveOne(clientId); |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void testRetrieveAll_ThrowsExceptionWhenStatusIsInvalid() { |
| 121 | + // Arrange |
| 122 | + // We create a SearchParameters object with an "INVALID" status |
| 123 | + org.apache.fineract.infrastructure.core.service.SearchParameters searchParameters = org.apache.fineract.infrastructure.core.service.SearchParameters |
| 124 | + .builder().status("INVALID").build(); |
| 125 | + |
| 126 | + // Act & Assert |
| 127 | + // The service should catch the "INVALID" status and throw a validation exception |
| 128 | + assertThrows(org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException.class, () -> { |
| 129 | + clientReadPlatformService.retrieveAll(searchParameters); |
| 130 | + }); |
| 131 | + } |
| 132 | +} |
0 commit comments