|
| 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.accounting.glaccount.service; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 23 | +import static org.mockito.Mockito.times; |
| 24 | +import static org.mockito.Mockito.verify; |
| 25 | +import static org.mockito.Mockito.when; |
| 26 | + |
| 27 | +import java.util.Optional; |
| 28 | +import org.apache.fineract.accounting.glaccount.domain.GLAccount; |
| 29 | +import org.apache.fineract.accounting.glaccount.domain.GLAccountRepository; |
| 30 | +import org.apache.fineract.accounting.journalentry.domain.JournalEntry; |
| 31 | +import org.apache.fineract.accounting.journalentry.domain.JournalEntryRepository; |
| 32 | +import org.apache.fineract.accounting.producttoaccountmapping.domain.ProductToGLAccountMapping; |
| 33 | +import org.apache.fineract.accounting.producttoaccountmapping.domain.ProductToGLAccountMappingRepository; |
| 34 | +import org.apache.fineract.infrastructure.core.data.CommandProcessingResult; |
| 35 | +import org.junit.jupiter.api.BeforeEach; |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 38 | +import org.mockito.ArgumentMatchers; |
| 39 | +import org.mockito.InjectMocks; |
| 40 | +import org.mockito.Mock; |
| 41 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 42 | +import org.springframework.data.jpa.domain.Specification; |
| 43 | + |
| 44 | +@ExtendWith(MockitoExtension.class) |
| 45 | +class GLAccountWritePlatformServiceJpaRepositoryImplTest { |
| 46 | + |
| 47 | + @Mock |
| 48 | + private GLAccountRepository glAccountRepository; |
| 49 | + |
| 50 | + @Mock |
| 51 | + private JournalEntryRepository glJournalEntryRepository; |
| 52 | + |
| 53 | + @Mock |
| 54 | + private ProductToGLAccountMappingRepository productToGLAccountMappingRepository; |
| 55 | + |
| 56 | + @InjectMocks |
| 57 | + private GLAccountWritePlatformServiceJpaRepositoryImpl glAccountWritePlatformService; |
| 58 | + |
| 59 | + private GLAccount glAccount; |
| 60 | + private static final Long GL_ACCOUNT_ID = 123L; |
| 61 | + |
| 62 | + @BeforeEach |
| 63 | + void setUp() { |
| 64 | + glAccount = new GLAccount(); |
| 65 | + glAccount.setId(GL_ACCOUNT_ID); |
| 66 | + glAccount.setName("Test Account"); |
| 67 | + glAccount.setGlCode("TEST001"); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Verify that a GL account can be deleted when it has no dependencies. |
| 72 | + */ |
| 73 | + @Test |
| 74 | + void testDeleteGLAccountSuccessWithNoDependencies() { |
| 75 | + // Given: A GL account with no children, no journal entries, and no product mappings |
| 76 | + when(glAccountRepository.findById(GL_ACCOUNT_ID)).thenReturn(Optional.of(glAccount)); |
| 77 | + when(glJournalEntryRepository.exists(ArgumentMatchers.<Specification<JournalEntry>>any())).thenReturn(false); |
| 78 | + when(productToGLAccountMappingRepository.exists(ArgumentMatchers.<Specification<ProductToGLAccountMapping>>any())).thenReturn(false); |
| 79 | + |
| 80 | + // When: Attempting to delete the GL account |
| 81 | + CommandProcessingResult result = glAccountWritePlatformService.deleteGLAccount(GL_ACCOUNT_ID); |
| 82 | + |
| 83 | + // Then: The deletion should succeed |
| 84 | + assertNotNull(result); |
| 85 | + assertEquals(GL_ACCOUNT_ID, result.getResourceId()); |
| 86 | + verify(glAccountRepository, times(1)).delete(glAccount); |
| 87 | + } |
| 88 | +} |
0 commit comments