This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
/
RestInvoicesApiTest.java
187 lines (147 loc) · 6.1 KB
/
RestInvoicesApiTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package io.konik.sdk.invoices;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.junit.Test;
import org.unitils.thirdparty.org.apache.commons.io.IOUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.LowLevelHttpRequest;
import com.google.api.client.http.LowLevelHttpResponse;
import com.google.api.client.testing.http.MockHttpTransport;
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
import io.konik.sdk.ZinvoiceApiConfig;
import io.konik.sdk.http.ZinvoiceHttpClient;
public class RestInvoicesApiTest {
private final ZinvoiceApiConfig apiConfig = new ZinvoiceApiConfig(UUID.randomUUID().toString(), "http://localhost");
private final ObjectMapper objectMapper = new ObjectMapper();
private final ZinvoiceHttpClient outboxMockHttpClient = new ZinvoiceHttpClient(apiConfig,
mockHttpRequestFactory(new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
StringWriter writer = new StringWriter();
IOUtils.copy(getClass().getResourceAsStream("/json/RestInvoicesApiTest_outbox.json"), writer, "UTF-8");
String json = writer.toString();
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.setStatusCode(200);
response.setContentType("application/json");
response.setContent(json);
return response;
}
}), objectMapper);
private final ZinvoiceHttpClient inboxMockHttpClient = new ZinvoiceHttpClient(apiConfig,
mockHttpRequestFactory(new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
StringWriter writer = new StringWriter();
IOUtils.copy(getClass().getResourceAsStream("/json/RestInvoicesApiTest_inbox.json"), writer, "UTF-8");
String json = writer.toString();
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.setStatusCode(200);
response.setContentType("application/json");
response.setContent(json);
return response;
}
}), objectMapper);
private HttpRequestFactory mockHttpRequestFactory(final MockLowLevelHttpRequest request) {
return new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
return request;
}
}.createRequestFactory();
}
@Test
public void shouldReturn4ItemsInOutbox() {
//given:
InvoicesApi invoicesApi = new RestInvoicesApi(outboxMockHttpClient);
//when:
List<InvoicesListElement> response = invoicesApi.outbox();
//then:
assertThat(response).hasSize(4);
}
@Test
public void shouldReturnCorrectFirstInboxElement() {
//given:
InvoicesApi invoicesApi = new RestInvoicesApi(outboxMockHttpClient);
//when:
List<InvoicesListElement> response = invoicesApi.outbox();
//then:
assertThat(response.get(0).getId()).isEqualTo("e7508df79bc24ceb9f8ac8f98e718e67");
}
@Test
public void shouldParseStatusEnumCorrectly() {
//given:
InvoicesApi invoicesApi = new RestInvoicesApi(outboxMockHttpClient);
//when:
List<InvoicesListElement> response = invoicesApi.outbox();
//then:
assertThat(response.get(0).getStatus()).isEqualTo("PRINTED");
}
@Test
public void shouldParseCreatedDateCorrectly() throws ParseException {
//given:
InvoicesApi invoicesApi = new RestInvoicesApi(outboxMockHttpClient);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date expectedCreatedDate = dateFormat.parse("2015-07-31 09:12");
//when:
List<InvoicesListElement> response = invoicesApi.outbox();
//then:
assertThat(response.get(0).getCreatedDate()).isEqualTo(expectedCreatedDate);
}
@Test
public void shouldParsePerformanceDateCorrectly() throws ParseException {
//given:
InvoicesApi invoicesApi = new RestInvoicesApi(outboxMockHttpClient);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date expectedPerformanceDate = dateFormat.parse("2015-07-31");
//when:
List<InvoicesListElement> response = invoicesApi.outbox();
//then:
assertThat(response.get(0).getPerformanceDate()).isEqualTo(expectedPerformanceDate);
}
@Test
public void shouldReturn4ItemsInInbox() {
//given:
InvoicesApi invoicesApi = new RestInvoicesApi(inboxMockHttpClient);
//when:
List<InvoicesListElement> response = invoicesApi.inbox();
//then:
assertThat(response).hasSize(3);
}
@Test
public void shouldParseREJECTEDStatusForTheFirstInvoiceInInbox() {
//given:
InvoicesApi invoicesApi = new RestInvoicesApi(inboxMockHttpClient);
//when:
List<InvoicesListElement> response = invoicesApi.inbox();
//then:
assertThat(response.get(0).getStatus()).isEqualTo("REJECTED");
}
@Test
public void shouldParseAmountCorrectly() {
//given:
InvoicesApi invoicesApi = new RestInvoicesApi(inboxMockHttpClient);
//when:
List<InvoicesListElement> response = invoicesApi.inbox();
//then:
assertThat(response.get(0).getAmount()).isEqualTo(BigDecimal.valueOf(235.62));
}
@Test
public void shouldParseCurrencyCorrectly() {
//given:
InvoicesApi invoicesApi = new RestInvoicesApi(inboxMockHttpClient);
//when:
List<InvoicesListElement> response = invoicesApi.inbox();
//then:
assertThat(response.get(0).getCurrency()).isEqualTo("EUR");
}
}