|
| 1 | +/* |
| 2 | + * Copyright 2023-present ByteChef Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.bytechef.component.quickbooks.util; |
| 18 | + |
| 19 | +import static com.bytechef.component.definition.ComponentDsl.option; |
| 20 | +import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.ACCOUNT; |
| 21 | +import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.CUSTOMER; |
| 22 | +import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.DISPLAY_NAME; |
| 23 | +import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.ID; |
| 24 | +import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.INVENTORY; |
| 25 | +import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.INVOICE; |
| 26 | +import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.ITEM; |
| 27 | +import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.NAME; |
| 28 | +import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.PAYMENT; |
| 29 | +import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.SERVICE; |
| 30 | +import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.TYPE; |
| 31 | +import static com.bytechef.component.quickbooks.util.QuickbooksUtils.addPropertiesForItem; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 33 | +import static org.mockito.ArgumentMatchers.any; |
| 34 | +import static org.mockito.Mockito.mock; |
| 35 | +import static org.mockito.Mockito.when; |
| 36 | + |
| 37 | +import com.bytechef.component.definition.ActionContext; |
| 38 | +import com.bytechef.component.definition.Context; |
| 39 | +import com.bytechef.component.definition.Option; |
| 40 | +import com.bytechef.component.definition.Parameters; |
| 41 | +import com.bytechef.component.definition.Property; |
| 42 | +import com.bytechef.component.definition.TypeReference; |
| 43 | +import java.net.URLEncoder; |
| 44 | +import java.nio.charset.StandardCharsets; |
| 45 | +import java.util.List; |
| 46 | +import java.util.Map; |
| 47 | +import org.junit.jupiter.api.Test; |
| 48 | +import org.mockito.ArgumentCaptor; |
| 49 | + |
| 50 | +/** |
| 51 | + * @author Monika Kušter |
| 52 | + */ |
| 53 | +class QuickbooksUtilsTest { |
| 54 | + |
| 55 | + private final List<Option<String>> expectedOptions = List.of(option("abc", "123")); |
| 56 | + private final Context.Http.Executor mockedExecutor = mock(Context.Http.Executor.class); |
| 57 | + private final Parameters mockedParameters = mock(Parameters.class); |
| 58 | + private final Context.Http.Response mockedResponse = mock(Context.Http.Response.class); |
| 59 | + private final ActionContext mockedActionContext = mock(ActionContext.class); |
| 60 | + private final ArgumentCaptor<String> queryNameArgumentCapture = ArgumentCaptor.forClass(String.class); |
| 61 | + private final ArgumentCaptor<String> queryValueArgumentCapture = ArgumentCaptor.forClass(String.class); |
| 62 | + |
| 63 | + @Test |
| 64 | + void testAddPropertiesForItemWithInventoryType() { |
| 65 | + verifyProperties(INVENTORY, true, true, true); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void testAddPropertiesForItemWithServiceType() { |
| 70 | + verifyProperties(SERVICE, true, false, false); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void testAddPropertiesForItemWithOtherType() { |
| 75 | + verifyProperties("Non-Inventory", false, false, false); |
| 76 | + } |
| 77 | + |
| 78 | + private void |
| 79 | + verifyProperties(String type, boolean incomeRequired, boolean assetRequired, boolean inventoryDateRequired) { |
| 80 | + when(mockedParameters.getRequiredString(TYPE)).thenReturn(type); |
| 81 | + |
| 82 | + List<? extends Property.ValueProperty<?>> properties = |
| 83 | + addPropertiesForItem(mockedParameters, mockedParameters, Map.of(), mockedActionContext); |
| 84 | + |
| 85 | + assertEquals(3, properties.size()); |
| 86 | + assertEquals(incomeRequired, properties.get(0) |
| 87 | + .getRequired() |
| 88 | + .orElse(false)); |
| 89 | + assertEquals(assetRequired, properties.get(1) |
| 90 | + .getRequired() |
| 91 | + .orElse(false)); |
| 92 | + assertEquals(inventoryDateRequired, properties.get(2) |
| 93 | + .getRequired() |
| 94 | + .orElse(false)); |
| 95 | + } |
| 96 | + |
| 97 | + private void setupHttpMock(Map<String, Object> responseBody) { |
| 98 | + when(mockedActionContext.http(any())).thenReturn(mockedExecutor); |
| 99 | + when(mockedExecutor.queryParameter(queryNameArgumentCapture.capture(), queryValueArgumentCapture.capture())) |
| 100 | + .thenReturn(mockedExecutor); |
| 101 | + when(mockedExecutor.configuration(any())).thenReturn(mockedExecutor); |
| 102 | + when(mockedExecutor.execute()).thenReturn(mockedResponse); |
| 103 | + when(mockedResponse.getBody(any(TypeReference.class))).thenReturn(responseBody); |
| 104 | + } |
| 105 | + |
| 106 | + private void verifyQuery(String expectedEntity) { |
| 107 | + assertEquals("query", queryNameArgumentCapture.getValue()); |
| 108 | + assertEquals(URLEncoder.encode("SELECT * FROM " + expectedEntity, StandardCharsets.UTF_8), |
| 109 | + queryValueArgumentCapture.getValue()); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void testGetOptionsForCustomer() throws Exception { |
| 114 | + setupHttpMock(Map.of("QueryResponse", Map.of("Customer", List.of(Map.of(DISPLAY_NAME, "abc", ID, "123"))))); |
| 115 | + |
| 116 | + List<? extends Option<String>> result = QuickbooksUtils.getOptions(CUSTOMER, null) |
| 117 | + .apply(mockedParameters, mockedParameters, null, "", mockedActionContext); |
| 118 | + |
| 119 | + assertEquals(expectedOptions, result); |
| 120 | + verifyQuery(CUSTOMER); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void testGetOptionsForInvoice() throws Exception { |
| 125 | + setupHttpMock(Map.of("QueryResponse", Map.of(INVOICE, List.of(Map.of("DocNumber", "abc", ID, "123"))))); |
| 126 | + |
| 127 | + List<? extends Option<String>> result = QuickbooksUtils.getOptions(INVOICE, null) |
| 128 | + .apply(mockedParameters, mockedParameters, null, "", mockedActionContext); |
| 129 | + |
| 130 | + assertEquals(expectedOptions, result); |
| 131 | + verifyQuery(INVOICE); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void testGetOptionsForPayment() throws Exception { |
| 136 | + setupHttpMock(Map.of("QueryResponse", Map.of(PAYMENT, List.of(Map.of(ID, "123"))))); |
| 137 | + |
| 138 | + List<? extends Option<String>> result = QuickbooksUtils.getOptions(PAYMENT, null) |
| 139 | + .apply(mockedParameters, mockedParameters, null, "", mockedActionContext); |
| 140 | + |
| 141 | + assertEquals(List.of(option("123", "123")), result); |
| 142 | + verifyQuery(PAYMENT); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + void testGetOptionsForItem() throws Exception { |
| 147 | + setupHttpMock(Map.of("QueryResponse", Map.of(ITEM, List.of(Map.of(NAME, "abc", ID, "123"))))); |
| 148 | + |
| 149 | + List<? extends Option<String>> result = QuickbooksUtils.getOptions(ITEM, null) |
| 150 | + .apply(mockedParameters, mockedParameters, null, "", mockedActionContext); |
| 151 | + |
| 152 | + assertEquals(expectedOptions, result); |
| 153 | + verifyQuery(ITEM); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + void testGetOptionsForIncomeAccount() throws Exception { |
| 158 | + setupHttpMock( |
| 159 | + Map.of("QueryResponse", Map.of(ACCOUNT, List.of(Map.of(NAME, "abc", ID, "123", "AccountType", "Income"))))); |
| 160 | + |
| 161 | + List<? extends Option<String>> result = QuickbooksUtils.getOptions(ACCOUNT, "Income") |
| 162 | + .apply(mockedParameters, mockedParameters, null, "", mockedActionContext); |
| 163 | + |
| 164 | + assertEquals(expectedOptions, result); |
| 165 | + verifyQuery(ACCOUNT); |
| 166 | + } |
| 167 | +} |
0 commit comments