Skip to content

Commit dce41fc

Browse files
author
Gabriele Barcella
committed
test: added unit tests
1 parent 62e8b59 commit dce41fc

13 files changed

+821
-65
lines changed

src/test/java/it/fattureincloud/sdk/api/PriceListsApiTest.java

Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,30 @@
1313

1414
package it.fattureincloud.sdk.api;
1515

16+
import it.fattureincloud.sdk.ApiClient;
1617
import it.fattureincloud.sdk.ApiException;
1718
import it.fattureincloud.sdk.model.GetPriceListItemsResponse;
19+
import it.fattureincloud.sdk.model.GetReceiptResponse;
1820
import it.fattureincloud.sdk.model.ListPriceListsResponse;
21+
import it.fattureincloud.sdk.model.PaymentAccount;
22+
import it.fattureincloud.sdk.model.PriceList;
23+
import it.fattureincloud.sdk.model.PriceListItem;
24+
import it.fattureincloud.sdk.model.PriceListPricesType;
25+
import it.fattureincloud.sdk.model.PriceListType;
26+
import it.fattureincloud.sdk.model.Receipt;
27+
import it.fattureincloud.sdk.model.ReceiptItemsListItem;
28+
import it.fattureincloud.sdk.model.ReceiptType;
29+
import okhttp3.*;
30+
import org.mockito.Mockito;
31+
1932
import org.junit.jupiter.api.Disabled;
2033
import org.junit.jupiter.api.Test;
2134

35+
import static org.junit.jupiter.api.Assertions.assertEquals;
36+
37+
import java.io.IOException;
38+
import java.math.BigDecimal;
39+
import java.time.LocalDate;
2240
import java.util.ArrayList;
2341
import java.util.HashMap;
2442
import java.util.List;
@@ -30,7 +48,30 @@
3048
@Disabled
3149
public class PriceListsApiTest {
3250

33-
private final PriceListsApi api = new PriceListsApi();
51+
private static PriceListsApi mockApi(final String serializedBody, final Call remoteCall)
52+
throws IOException {
53+
final OkHttpClient okHttpClient = Mockito.mock(OkHttpClient.class);
54+
55+
Response.Builder builder =
56+
new Response.Builder()
57+
.request(new Request.Builder().url("https://api-v2.fattureincloud.it").build())
58+
.protocol(Protocol.HTTP_1_1)
59+
.code(200)
60+
.message("");
61+
if (serializedBody != null) {
62+
builder =
63+
builder.body(ResponseBody.create(serializedBody, MediaType.parse("application/json")));
64+
}
65+
66+
final Response response = builder.build();
67+
68+
Mockito.when(remoteCall.execute()).thenReturn(response);
69+
Mockito.when(okHttpClient.newCall(Mockito.any())).thenReturn(remoteCall);
70+
71+
ApiClient client = new ApiClient(okHttpClient);
72+
73+
return new PriceListsApi(client);
74+
}
3475

3576
/**
3677
* Get PriceList Items List
@@ -40,11 +81,26 @@ public class PriceListsApiTest {
4081
* @throws ApiException if the Api call fails
4182
*/
4283
@Test
43-
public void getPriceListItemsTest() throws ApiException {
44-
Integer companyId = null;
45-
String priceListId = null;
84+
public void getPriceListItemsTest() throws ApiException, IOException {
85+
String result =
86+
"{\"data\":{1:{\"price\":3.5}}}";
87+
88+
Call mockCall = Mockito.mock(Call.class);
89+
PriceListsApi api = mockApi(result, mockCall);
90+
91+
Integer companyId = 2;
92+
String priceListId = "1";
93+
94+
GetPriceListItemsResponse expected =
95+
new GetPriceListItemsResponse()
96+
.data(new HashMap<String, PriceListItem>() {{
97+
put("1", new PriceListItem().price(BigDecimal.valueOf(3.5)));
98+
}});
99+
46100
GetPriceListItemsResponse response = api.getPriceListItems(companyId, priceListId);
47-
// TODO: test validations
101+
assertEquals(expected, response.getData());
102+
Mockito.verify(mockCall, Mockito.only()).execute();
103+
48104
}
49105

50106
/**
@@ -55,10 +111,31 @@ public void getPriceListItemsTest() throws ApiException {
55111
* @throws ApiException if the Api call fails
56112
*/
57113
@Test
58-
public void getPriceListsTest() throws ApiException {
59-
Integer companyId = null;
114+
public void getPriceListsTest() throws ApiException, IOException {
115+
String result =
116+
"{\"data\":[{\"id\":\"10\",\"name\":\"Listino 1\",\"prices_type\":\"net\",\"is_default\":true,\"valid_from\":\"2023-01-01\",\"valid_to\":\"2023-12-31\",\"type\":\"sell\"}]}";
117+
118+
Call mockCall = Mockito.mock(Call.class);
119+
PriceListsApi api = mockApi(result, mockCall);
120+
121+
Integer companyId = 2;
122+
123+
ListPriceListsResponse expected =
124+
new ListPriceListsResponse()
125+
.addDataItem(
126+
new PriceList()
127+
.id("10")
128+
.name("Listino 1")
129+
.pricesType(PriceListPricesType.NET)
130+
.isDefault(true)
131+
.validFrom("2023-01-01")
132+
.validTo("2023-12-31")
133+
.type(PriceListType.SELL));
134+
60135
ListPriceListsResponse response = api.getPriceLists(companyId);
61-
// TODO: test validations
136+
assertEquals(expected, response.getData());
137+
Mockito.verify(mockCall, Mockito.only()).execute();
138+
62139
}
63140

64141
}

src/test/java/it/fattureincloud/sdk/model/EntityClientPreCreateInfoTest.java

Lines changed: 148 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,92 +13,231 @@
1313

1414
package it.fattureincloud.sdk.model;
1515

16+
import com.google.gson.Gson;
1617
import com.google.gson.TypeAdapter;
1718
import com.google.gson.annotations.JsonAdapter;
1819
import com.google.gson.annotations.SerializedName;
1920
import com.google.gson.stream.JsonReader;
2021
import com.google.gson.stream.JsonWriter;
22+
23+
import it.fattureincloud.sdk.JSON;
2124
import it.fattureincloud.sdk.model.PaymentAccount;
2225
import it.fattureincloud.sdk.model.PaymentMethod;
2326
import it.fattureincloud.sdk.model.PriceList;
2427
import it.fattureincloud.sdk.model.VatType;
28+
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import static org.junit.jupiter.api.Assertions.assertFalse;
31+
2532
import java.io.IOException;
2633
import java.math.BigDecimal;
2734
import java.util.ArrayList;
2835
import java.util.Arrays;
2936
import java.util.List;
3037
import org.openapitools.jackson.nullable.JsonNullable;
38+
import org.junit.jupiter.api.BeforeEach;
3139
import org.junit.jupiter.api.Disabled;
3240
import org.junit.jupiter.api.Test;
3341

3442
/**
3543
* Model tests for EntityClientPreCreateInfo
3644
*/
3745
public class EntityClientPreCreateInfoTest {
38-
private final EntityClientPreCreateInfo model = new EntityClientPreCreateInfo();
46+
private EntityClientPreCreateInfo model;
47+
48+
@BeforeEach
49+
public void init() {
50+
model =
51+
new EntityClientPreCreateInfo()
52+
.countriesList(Arrays.asList("Italia", "Marocco"))
53+
.paymentMethodsList(
54+
Arrays.asList(new PaymentMethod().id(1), new PaymentMethod().id(2)))
55+
.paymentAccountsList(
56+
Arrays.asList(new PaymentAccount().id(1), new PaymentAccount().id(2)))
57+
.vatTypesList(Arrays.asList(new VatType().id(1), new VatType().id(2)))
58+
.priceLists(Arrays.asList(new PriceList().id("12345")));
59+
}
3960

4061
/**
4162
* Model tests for EntityClientPreCreateInfo
4263
*/
4364
@Test
4465
public void testEntityClientPreCreateInfo() {
45-
// TODO: test EntityClientPreCreateInfo
66+
JSON jsonManager = new JSON();
67+
Gson gson = jsonManager.getGson();
68+
String json = gson.toJson(model);
69+
String str =
70+
"{\"countries_list\":[\"Italia\",\"Marocco\"],\"payment_methods_list\":[{\"id\":1,\"type\":\"standard\"},{\"id\":2,\"type\":\"standard\"}],\"payment_accounts_list\":[{\"id\":1,\"type\":\"standard\"},{\"id\":2,\"type\":\"standard\"}],\"vat_types_list\":[{\"id\":1},{\"id\":2}],\"price_lists\":[{\"id\":\"12345\"}]}";
71+
assertEquals(str, json);
72+
EntityClientPreCreateInfo generated = gson.fromJson(str, EntityClientPreCreateInfo.class);
73+
assertEquals(model, generated);
74+
75+
Object o = model;
76+
assertEquals(model, o);
77+
assertFalse(model.equals(null));
78+
assertFalse(model.equals(Integer.getInteger("5")));
4679
}
4780

4881
/**
4982
* Test the property 'countriesList'
5083
*/
5184
@Test
5285
public void countriesListTest() {
53-
// TODO: test countriesList
86+
assertEquals(model.getCountriesList(), Arrays.asList("Italia", "Marocco"));
87+
model.setCountriesList(Arrays.asList("Italia", "Marocco", "Francia"));
88+
assertEquals(model.getCountriesList(), Arrays.asList("Italia", "Marocco", "Francia"));
89+
EntityClientPreCreateInfo a = model.countriesList(Arrays.asList("Italia", "Marocco"));
90+
EntityClientPreCreateInfo expected =
91+
new EntityClientPreCreateInfo()
92+
.countriesList(Arrays.asList("Italia", "Marocco"))
93+
.paymentMethodsList(
94+
Arrays.asList(new PaymentMethod().id(1), new PaymentMethod().id(2)))
95+
.paymentAccountsList(
96+
Arrays.asList(new PaymentAccount().id(1), new PaymentAccount().id(2)))
97+
.vatTypesList(Arrays.asList(new VatType().id(1), new VatType().id(2)))
98+
.priceLists(Arrays.asList(new PriceList().id("12345")));
99+
assertEquals(expected, a);
54100
}
55101

56102
/**
57103
* Test the property 'paymentMethodsList'
58104
*/
59105
@Test
60106
public void paymentMethodsListTest() {
61-
// TODO: test paymentMethodsList
107+
assertEquals(1, model.getPaymentMethodsList().get(0).getId());
108+
assertEquals(2, model.getPaymentMethodsList().get(1).getId());
109+
model.setPaymentMethodsList(
110+
Arrays.asList(new PaymentMethod().id(3), new PaymentMethod().id(4)));
111+
assertEquals(3, model.getPaymentMethodsList().get(0).getId());
112+
assertEquals(4, model.getPaymentMethodsList().get(1).getId());
113+
EntityClientPreCreateInfo a = model.paymentMethodsList(
114+
Arrays.asList(new PaymentMethod().id(1), new PaymentMethod().id(2)));
115+
EntityClientPreCreateInfo expected =
116+
new EntityClientPreCreateInfo()
117+
.countriesList(Arrays.asList("Italia", "Marocco"))
118+
.paymentMethodsList(
119+
Arrays.asList(new PaymentMethod().id(1), new PaymentMethod().id(2)))
120+
.paymentAccountsList(
121+
Arrays.asList(new PaymentAccount().id(1), new PaymentAccount().id(2)))
122+
.vatTypesList(Arrays.asList(new VatType().id(1), new VatType().id(2)))
123+
.priceLists(Arrays.asList(new PriceList().id("12345")));
124+
assertEquals(expected, a);
62125
}
63126

64127
/**
65128
* Test the property 'paymentAccountsList'
66129
*/
67130
@Test
68131
public void paymentAccountsListTest() {
69-
// TODO: test paymentAccountsList
132+
assertEquals(1, model.getPaymentAccountsList().get(0).getId());
133+
assertEquals(2, model.getPaymentAccountsList().get(1).getId());
134+
model.setPaymentAccountsList(
135+
Arrays.asList(new PaymentAccount().id(3), new PaymentAccount().id(4)));
136+
assertEquals(3, model.getPaymentAccountsList().get(0).getId());
137+
assertEquals(4, model.getPaymentAccountsList().get(1).getId());
138+
EntityClientPreCreateInfo a = model.paymentAccountsList(
139+
Arrays.asList(new PaymentAccount().id(1), new PaymentAccount().id(2)));
140+
EntityClientPreCreateInfo expected =
141+
new EntityClientPreCreateInfo()
142+
.countriesList(Arrays.asList("Italia", "Marocco"))
143+
.paymentMethodsList(
144+
Arrays.asList(new PaymentMethod().id(1), new PaymentMethod().id(2)))
145+
.paymentAccountsList(
146+
Arrays.asList(new PaymentAccount().id(1), new PaymentAccount().id(2)))
147+
.vatTypesList(Arrays.asList(new VatType().id(1), new VatType().id(2)))
148+
.priceLists(Arrays.asList(new PriceList().id("12345")));
149+
assertEquals(expected, a);
70150
}
71151

72152
/**
73153
* Test the property 'vatTypesList'
74154
*/
75155
@Test
76156
public void vatTypesListTest() {
77-
// TODO: test vatTypesList
157+
assertEquals(1, model.getVatTypesList().get(0).getId());
158+
assertEquals(2, model.getVatTypesList().get(1).getId());
159+
model.setVatTypesList(Arrays.asList(new VatType().id(3), new VatType().id(4)));
160+
assertEquals(3, model.getVatTypesList().get(0).getId());
161+
assertEquals(4, model.getVatTypesList().get(1).getId());
162+
EntityClientPreCreateInfo a = model.vatTypesList(
163+
Arrays.asList(new VatType().id(1), new VatType().id(2)));
164+
EntityClientPreCreateInfo expected =
165+
new EntityClientPreCreateInfo()
166+
.countriesList(Arrays.asList("Italia", "Marocco"))
167+
.paymentMethodsList(
168+
Arrays.asList(new PaymentMethod().id(1), new PaymentMethod().id(2)))
169+
.paymentAccountsList(
170+
Arrays.asList(new PaymentAccount().id(1), new PaymentAccount().id(2)))
171+
.vatTypesList(Arrays.asList(new VatType().id(1), new VatType().id(2)))
172+
.priceLists(Arrays.asList(new PriceList().id("12345")));
173+
assertEquals(expected, a);
78174
}
79175

80176
/**
81177
* Test the property 'priceLists'
82178
*/
83179
@Test
84180
public void priceListsTest() {
85-
// TODO: test priceLists
181+
assertEquals("12345", model.getPriceLists().get(0).getId());
182+
model.setPriceLists(Arrays.asList(new PriceList().id("54321")));
183+
assertEquals("54321", model.getPriceLists().get(0).getId());
184+
EntityClientPreCreateInfo a = model.priceLists(
185+
Arrays.asList(new PriceList().id("12345")));
186+
EntityClientPreCreateInfo expected =
187+
new EntityClientPreCreateInfo()
188+
.countriesList(Arrays.asList("Italia", "Marocco"))
189+
.paymentMethodsList(
190+
Arrays.asList(new PaymentMethod().id(1), new PaymentMethod().id(2)))
191+
.paymentAccountsList(
192+
Arrays.asList(new PaymentAccount().id(1), new PaymentAccount().id(2)))
193+
.vatTypesList(Arrays.asList(new VatType().id(1), new VatType().id(2)))
194+
.priceLists(Arrays.asList(new PriceList().id("12345")));
195+
assertEquals(expected, a);
86196
}
87197

88198
/**
89199
* Test the property 'limit'
90200
*/
91201
@Test
92202
public void limitTest() {
93-
// TODO: test limit
203+
assertEquals(null, model.getLimit());
204+
model.setLimit(new BigDecimal("1000.50"));
205+
assertEquals(new BigDecimal("1000.50"), model.getLimit());
206+
EntityClientPreCreateInfo a = model.limit(new BigDecimal("500.25"));
207+
EntityClientPreCreateInfo expected =
208+
new EntityClientPreCreateInfo()
209+
.countriesList(Arrays.asList("Italia", "Marocco"))
210+
.paymentMethodsList(
211+
Arrays.asList(new PaymentMethod().id(1), new PaymentMethod().id(2)))
212+
.paymentAccountsList(
213+
Arrays.asList(new PaymentAccount().id(1), new PaymentAccount().id(2)))
214+
.vatTypesList(Arrays.asList(new VatType().id(1), new VatType().id(2)))
215+
.priceLists(Arrays.asList(new PriceList().id("12345")))
216+
.limit(new BigDecimal("500.25"));
217+
assertEquals(expected, a);
94218
}
95219

96220
/**
97221
* Test the property 'usage'
98222
*/
99223
@Test
100224
public void usageTest() {
101-
// TODO: test usage
225+
assertEquals(null, model.getUsage());
226+
model.setUsage(new BigDecimal("200.75"));
227+
assertEquals(new BigDecimal("200.75"), model.getUsage());
228+
EntityClientPreCreateInfo a = model.usage(new BigDecimal("150.50"));
229+
EntityClientPreCreateInfo expected =
230+
new EntityClientPreCreateInfo()
231+
.countriesList(Arrays.asList("Italia", "Marocco"))
232+
.paymentMethodsList(
233+
Arrays.asList(new PaymentMethod().id(1), new PaymentMethod().id(2)))
234+
.paymentAccountsList(
235+
Arrays.asList(new PaymentAccount().id(1), new PaymentAccount().id(2)))
236+
.vatTypesList(Arrays.asList(new VatType().id(1), new VatType().id(2)))
237+
.priceLists(Arrays.asList(new PriceList().id("12345")))
238+
.usage(new BigDecimal("150.50"));
239+
assertEquals(expected, a);
102240
}
241+
103242

104243
}

0 commit comments

Comments
 (0)