From 9c8239a9b10fa65417359265cc8dd444c9e0e907 Mon Sep 17 00:00:00 2001 From: Jose Alberto Hernandez Date: Sun, 19 Jun 2022 10:23:22 -0500 Subject: [PATCH] Implement integration tests for Accounting rules --- .../AccountingRuleIntegrationTest.java | 79 +++++++++++++++++++ .../common/accounting/AccountRuleHelper.java | 59 ++++++++++++++ .../accounting/AccountingRuleBuilder.java | 58 ++++++++++++++ 3 files changed, 196 insertions(+) create mode 100644 integration-tests/src/test/java/org/apache/fineract/integrationtests/accounting/AccountingRuleIntegrationTest.java create mode 100644 integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/AccountRuleHelper.java create mode 100644 integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/AccountingRuleBuilder.java diff --git a/integration-tests/src/test/java/org/apache/fineract/integrationtests/accounting/AccountingRuleIntegrationTest.java b/integration-tests/src/test/java/org/apache/fineract/integrationtests/accounting/AccountingRuleIntegrationTest.java new file mode 100644 index 000000000..9d6698dc0 --- /dev/null +++ b/integration-tests/src/test/java/org/apache/fineract/integrationtests/accounting/AccountingRuleIntegrationTest.java @@ -0,0 +1,79 @@ +/** + * 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.integrationtests.accounting; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import io.restassured.builder.RequestSpecBuilder; +import io.restassured.builder.ResponseSpecBuilder; +import io.restassured.http.ContentType; +import io.restassured.specification.RequestSpecification; +import io.restassured.specification.ResponseSpecification; +import java.util.ArrayList; +import org.apache.fineract.client.models.GetAccountRulesResponse; +import org.apache.fineract.client.models.GetOfficesResponse; +import org.apache.fineract.client.models.PostAccountingRulesResponse; +import org.apache.fineract.integrationtests.common.OfficeHelper; +import org.apache.fineract.integrationtests.common.Utils; +import org.apache.fineract.integrationtests.common.accounting.Account; +import org.apache.fineract.integrationtests.common.accounting.AccountHelper; +import org.apache.fineract.integrationtests.common.accounting.AccountRuleHelper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class AccountingRuleIntegrationTest { + + private ResponseSpecification responseSpec; + private RequestSpecification requestSpec; + + private AccountHelper accountHelper; + private AccountRuleHelper accountRuleHelper; + + @BeforeEach + public void setup() { + Utils.initializeRESTAssured(); + + requestSpec = new RequestSpecBuilder().setContentType(ContentType.JSON).build(); + requestSpec.header("Authorization", "Basic " + Utils.loginIntoServerAndGetBase64EncodedAuthenticationKey()); + responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); + + accountRuleHelper = new AccountRuleHelper(requestSpec, responseSpec); + accountHelper = new AccountHelper(requestSpec, responseSpec); + } + + @Test + public void testAccountingRuleCreation() { + // given + final Account accountToCredit = accountHelper.createIncomeAccount(); + final Account accountToDebit = accountHelper.createExpenseAccount(); + final GetOfficesResponse headOffice = OfficeHelper.getHeadOffice(requestSpec, responseSpec); + + // when + final PostAccountingRulesResponse accountingRule = accountRuleHelper.createAccountRule(headOffice.getId(), accountToCredit, + accountToDebit); + final ArrayList accountingRules = accountRuleHelper.getAccountingRules(); + + // then + assertNotNull(accountingRule); + assertNotNull(accountingRule.getResourceId()); + assertNotNull(accountingRules); + assertTrue(accountingRules.size() > 0); + } +} diff --git a/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/AccountRuleHelper.java b/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/AccountRuleHelper.java new file mode 100644 index 000000000..c13db44a2 --- /dev/null +++ b/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/AccountRuleHelper.java @@ -0,0 +1,59 @@ +/** + * 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.integrationtests.common.accounting; + +import com.google.gson.Gson; +import com.linecorp.armeria.internal.shaded.guava.reflect.TypeToken; +import io.restassured.specification.RequestSpecification; +import io.restassured.specification.ResponseSpecification; +import java.lang.reflect.Type; +import java.util.ArrayList; +import org.apache.fineract.client.models.GetAccountRulesResponse; +import org.apache.fineract.client.models.PostAccountingRulesResponse; +import org.apache.fineract.client.util.JSON; +import org.apache.fineract.integrationtests.common.Utils; + +public class AccountRuleHelper { + + private static final Gson GSON = new JSON().getGson(); + + private static final String ACCOUNTINGRULES_URL = "/fineract-provider/api/v1/accountingrules?" + Utils.TENANT_IDENTIFIER; + + private final RequestSpecification requestSpec; + private final ResponseSpecification responseSpec; + + public AccountRuleHelper(final RequestSpecification requestSpec, final ResponseSpecification responseSpec) { + this.requestSpec = requestSpec; + this.responseSpec = responseSpec; + } + + public ArrayList getAccountingRules() { + final String response = Utils.performServerGet(this.requestSpec, this.responseSpec, ACCOUNTINGRULES_URL); + Type accountRuleListType = new TypeToken>() {}.getType(); + return GSON.fromJson(response, accountRuleListType); + } + + public PostAccountingRulesResponse createAccountRule(final Long officeId, final Account accountToCredit, final Account accountToDebit) { + final String assetAccountJSON = new AccountingRuleBuilder() + .withGLAccounts(accountToCredit.getAccountID(), accountToDebit.getAccountID()).withOffice(officeId).build(); + final String response = Utils.performServerPost(requestSpec, responseSpec, ACCOUNTINGRULES_URL, assetAccountJSON); + return GSON.fromJson(response, PostAccountingRulesResponse.class); + } + +} diff --git a/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/AccountingRuleBuilder.java b/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/AccountingRuleBuilder.java new file mode 100644 index 000000000..5eb467b50 --- /dev/null +++ b/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/accounting/AccountingRuleBuilder.java @@ -0,0 +1,58 @@ +/** + * 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.integrationtests.common.accounting; + +import com.google.gson.Gson; +import java.util.HashMap; +import org.apache.fineract.integrationtests.common.Utils; + +public class AccountingRuleBuilder { + + private Long officeId; + private Integer accountToCreditId; + private Integer accountToDebitId; + private String name; + private String description; + + public AccountingRuleBuilder() { + name = Utils.randomStringGenerator("ACCOUNTRULE_NAME_", 5); + description = name; + } + + public String build() { + final HashMap map = new HashMap<>(); + map.put("name", name); + map.put("officeId", officeId); + map.put("accountToCredit", accountToCreditId); + map.put("accountToDebit", accountToDebitId); + map.put("description", description); + return new Gson().toJson(map); + } + + public AccountingRuleBuilder withGLAccounts(final Integer accountToCreditId, final Integer accountToDebitId) { + this.accountToCreditId = accountToCreditId; + this.accountToDebitId = accountToDebitId; + return this; + } + + public AccountingRuleBuilder withOffice(final Long officeId) { + this.officeId = officeId; + return this; + } +}