Skip to content

Commit 8b9d713

Browse files
monikakusterivicac
authored andcommitted
684 - add tests
1 parent 47fe0b2 commit 8b9d713

File tree

3 files changed

+252
-4
lines changed

3 files changed

+252
-4
lines changed

server/libs/modules/components/quickbooks/src/test/java/com/bytechef/component/quickbooks/action/QuickbooksCreateItemActionTest.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,38 @@
1616

1717
package com.bytechef.component.quickbooks.action;
1818

19+
import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.ACCOUNT;
20+
import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.ASSET_ACCOUNT_REF;
21+
import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.EXPENSE_ACCOUNT_REF;
22+
import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.INCOME_ACCOUNT_REF;
23+
import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.INVENTORY;
24+
import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.INV_START_DATE;
1925
import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.NAME;
2026
import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.QTY_ON_HAND;
27+
import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.TYPE;
28+
import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.VALUE;
2129
import static org.junit.jupiter.api.Assertions.assertEquals;
2230
import static org.mockito.ArgumentMatchers.any;
2331
import static org.mockito.Mockito.when;
2432

2533
import com.bytechef.component.definition.Context.Http;
2634
import com.bytechef.component.definition.TypeReference;
2735
import com.bytechef.component.test.definition.MockParametersFactory;
36+
import java.time.LocalDate;
2837
import java.util.Map;
2938
import org.junit.jupiter.api.Test;
3039

40+
/**
41+
* @author Monika Kušter
42+
*/
3143
class QuickbooksCreateItemActionTest extends AbstractQuickbooksActionTest {
3244

3345
@Test
3446
void testPerform() {
35-
Map<String, Object> bodyMap = Map.of(NAME, NAME, QTY_ON_HAND, 123);
36-
37-
mockedParameters = MockParametersFactory.create(bodyMap);
47+
mockedParameters = MockParametersFactory.create(Map.of(
48+
NAME, NAME, TYPE, INVENTORY, QTY_ON_HAND, 123, EXPENSE_ACCOUNT_REF, "expense",
49+
ACCOUNT, Map.of(
50+
INCOME_ACCOUNT_REF, "income", ASSET_ACCOUNT_REF, "asset", INV_START_DATE, LocalDate.of(2020, 1, 1))));
3851

3952
when(mockedContext.http(any()))
4053
.thenReturn(mockedExecutor);
@@ -53,6 +66,11 @@ void testPerform() {
5366

5467
Http.Body body = bodyArgumentCaptor.getValue();
5568

56-
assertEquals(bodyMap, body.getContent());
69+
Map<String, Object> expectedBody = Map.of(
70+
NAME, NAME, QTY_ON_HAND, 123.0, INCOME_ACCOUNT_REF, Map.of(VALUE, "income"),
71+
ASSET_ACCOUNT_REF, Map.of(VALUE, "asset"), INV_START_DATE, "2020-01-01", EXPENSE_ACCOUNT_REF,
72+
Map.of(VALUE, "expense"));
73+
74+
assertEquals(expectedBody, body.getContent());
5775
}
5876
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.action;
18+
19+
import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.CUSTOMER;
20+
import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.CUSTOMER_REF;
21+
import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.TOTAL_AMT;
22+
import static com.bytechef.component.quickbooks.constant.QuickbooksConstants.VALUE;
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
import static org.mockito.ArgumentMatchers.any;
25+
import static org.mockito.Mockito.when;
26+
27+
import com.bytechef.component.definition.Context.Http;
28+
import com.bytechef.component.definition.TypeReference;
29+
import com.bytechef.component.test.definition.MockParametersFactory;
30+
import java.util.Map;
31+
import org.junit.jupiter.api.Test;
32+
33+
/**
34+
* @author Monika Kušter
35+
*/
36+
class QuickbooksCreatePaymentActionTest extends AbstractQuickbooksActionTest {
37+
38+
@Test
39+
void testPerform() {
40+
mockedParameters = MockParametersFactory.create(Map.of(CUSTOMER, "abc", TOTAL_AMT, 123));
41+
42+
when(mockedContext.http(any()))
43+
.thenReturn(mockedExecutor);
44+
when(mockedExecutor.body(bodyArgumentCaptor.capture()))
45+
.thenReturn(mockedExecutor);
46+
when(mockedExecutor.configuration(any()))
47+
.thenReturn(mockedExecutor);
48+
when(mockedExecutor.execute())
49+
.thenReturn(mockedResponse);
50+
when(mockedResponse.getBody(any(TypeReference.class)))
51+
.thenReturn(mockedObject);
52+
53+
Object result = QuickbooksCreatePaymentAction.perform(mockedParameters, mockedParameters, mockedContext);
54+
55+
assertEquals(mockedObject, result);
56+
57+
Http.Body body = bodyArgumentCaptor.getValue();
58+
59+
Map<String, Object> expectedBody = Map.of(TOTAL_AMT, 123.0, CUSTOMER_REF, Map.of(VALUE, "abc"));
60+
61+
assertEquals(expectedBody, body.getContent());
62+
}
63+
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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

Comments
 (0)