Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.

Commit 60ce60d

Browse files
committed
improved assertions and docs
1 parent df3f8bc commit 60ce60d

11 files changed

Lines changed: 82 additions & 70 deletions

File tree

src/main/java/com/sybit/airtable/Airtable.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ public Airtable configure(Configuration config) throws AirtableException {
136136
*/
137137
@SuppressWarnings("WeakerAccess")
138138
public Airtable configure(Configuration config, ObjectMapper objectMapper) throws AirtableException {
139+
assert config != null : "config was null";
140+
assert objectMapper != null : "objectMapper was null";
141+
139142
if(config.getApiKey() == null) {
140143
throw new AirtableException("Missing Airtable API-Key");
141144
}
@@ -157,17 +160,16 @@ public Airtable configure(Configuration config, ObjectMapper objectMapper) throw
157160

158161
// Add specific Converter for Date
159162
DateTimeConverter dtConverter = new DateConverter();
160-
ListConverter lConverter = new ListConverter();
161-
MapConverter thConverter = new MapConverter();
162-
163-
lConverter.setListClass(Attachment.class);
164-
thConverter.setMapClass(Thumbnail.class);
165163
dtConverter.setPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
166-
167164
ConvertUtils.register(dtConverter, Date.class);
165+
166+
ListConverter lConverter = new ListConverter();
167+
lConverter.setListClass(Attachment.class);
168168
ConvertUtils.register(lConverter, List.class);
169-
ConvertUtils.register(thConverter, Map.class);
170169

170+
MapConverter thConverter = new MapConverter();
171+
thConverter.setMapClass(Thumbnail.class);
172+
ConvertUtils.register(thConverter, Map.class);
171173

172174
return this;
173175
}
@@ -235,6 +237,8 @@ public Configuration getConfig() {
235237
}
236238

237239
public void setConfig(Configuration config) {
240+
assert config != null : "config was null";
241+
238242
this.config = config;
239243
setProxy(config.getEndpointUrl());
240244
}

src/main/java/com/sybit/airtable/Base.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,22 @@ public class Base {
2424

2525
private final Map<String, Table> tableMap = new HashMap<>();
2626

27-
private final String base;
27+
private final String baseName;
2828

2929
private final Airtable parent;
3030

3131

3232
/**
33-
* Create Airtable Base with given base ID.
33+
* Create Airtable Base with given baseName ID.
3434
*
35-
* @param base base ID could be found at https://airtable.com if you select your current base.
35+
* @param name base ID could be found at https://airtable.com if you select your current baseName.
3636
* @param airtable parent airtable object
3737
*/
38-
public Base(String base, Airtable airtable) {
39-
this.base = base;
38+
public Base(String name, Airtable airtable) {
39+
assert name != null : "baseName was null";
40+
assert airtable != null : "airtable was null";
41+
42+
this.baseName = name;
4043
this.parent = airtable;
4144
}
4245

@@ -54,7 +57,6 @@ public Airtable airtable() {
5457
* @return Object to access table.
5558
*/
5659
public Table table(String name) {
57-
5860
return table(name, Records.class);
5961
}
6062

@@ -65,6 +67,8 @@ public Table table(String name) {
6567
* @return Object to access table.
6668
*/
6769
public Table table(String name, Class clazz) {
70+
assert name != null : "name was null";
71+
assert clazz != null : "clazz was null";
6872

6973
if(!tableMap.containsKey(name)) {
7074
LOG.debug("Create new instance for table [" + name + "]");
@@ -77,10 +81,10 @@ public Table table(String name, Class clazz) {
7781
}
7882

7983
/**
80-
* Get base id of base.
81-
* @return base id
84+
* Get baseName id of baseName.
85+
* @return baseName id
8286
*/
8387
public String name() {
84-
return base;
88+
return baseName;
8589
}
8690
}

src/main/java/com/sybit/airtable/Configuration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
/**
1010
* Configuration settings for Airtable.
1111
* Used by class <code>Airtable</code> to configure basic settings.
12+
*
13+
* @since 0.1
1214
*/
1315
public class Configuration {
1416

src/main/java/com/sybit/airtable/GsonObjectMapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import java.util.logging.Logger;
1313

1414
/**
15+
* Default mapper based on GSON.
1516
*
17+
* @since 0.1
1618
* @author fzr
1719
*/
1820
class GsonObjectMapper implements ObjectMapper {

src/main/java/com/sybit/airtable/IOUtils.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/main/java/com/sybit/airtable/Query.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
/**
1111
* Interface for specific queries.
12+
*
13+
* @since 0.1
1214
*/
1315
@SuppressWarnings("WeakerAccess")
1416
public interface Query {

src/main/java/com/sybit/airtable/Sort.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
package com.sybit.airtable;
88

99
/**
10-
* Sortation helper.
10+
* Sorting of query results.
11+
*
12+
* @since 0.1
1113
*/
1214
public class Sort {
1315

src/main/java/com/sybit/airtable/Table.java

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@
1313
import com.mashape.unirest.request.GetRequest;
1414
import com.sybit.airtable.exception.AirtableException;
1515
import com.sybit.airtable.exception.HttpResponseExceptionHandler;
16-
import com.sybit.airtable.vo.Attachment;
17-
import com.sybit.airtable.vo.Delete;
18-
import com.sybit.airtable.vo.PostRecord;
19-
import com.sybit.airtable.vo.RecordItem;
20-
import com.sybit.airtable.vo.Records;
16+
import com.sybit.airtable.vo.*;
2117
import org.apache.commons.beanutils.BeanUtils;
18+
import org.apache.commons.beanutils.BeanUtilsBean;
2219
import org.apache.commons.beanutils.PropertyUtils;
2320
import org.apache.http.client.HttpResponseException;
2421
import org.slf4j.Logger;
@@ -27,12 +24,9 @@
2724
import java.lang.reflect.Field;
2825
import java.lang.reflect.InvocationTargetException;
2926
import java.util.ArrayList;
30-
3127
import java.util.List;
3228
import java.util.Map;
3329

34-
import org.apache.commons.beanutils.BeanUtilsBean;
35-
3630
/**
3731
* Representation Class of Airtable Tables.
3832
*
@@ -49,12 +43,15 @@ public class Table<T> {
4943

5044
/**
5145
*
52-
* @param name
53-
* @param type
46+
* @param name name of table.
47+
* @param type class to represent table row
5448
*/
5549
public Table(String name, Class<T> type) {
56-
this.type = type;
50+
assert name != null : "name was null";
51+
assert type != null : "type was null";
52+
5753
this.name = name;
54+
this.type = type;
5855
}
5956

6057
/**
@@ -78,10 +75,9 @@ public void setParent(Base parent) {
7875
}
7976

8077
/**
81-
*
82-
* If no Parameter ser all querys to null.
78+
* Select all rows of table.
8379
*
84-
* @return
80+
* @return List of all items.
8581
* @throws AirtableException
8682
*/
8783
public List<T> select() throws AirtableException, HttpResponseException {
@@ -122,8 +118,8 @@ public Integer getPageSize() {
122118
/**
123119
* Select List of data of table with defined Query Parameters.
124120
*
125-
* @param query
126-
* @return
121+
* @param query defined query
122+
* @return list of table items
127123
* @throws AirtableException
128124
*/
129125
@SuppressWarnings("WeakerAccess")
@@ -311,10 +307,10 @@ public Integer getPageSize() {
311307
}
312308

313309
/**
314-
* select only Table data with defined Fields
315-
*
316-
* @param fields
317-
* @return
310+
* Select only Table data with defined fields.
311+
*
312+
* @param fields array of requested fields.
313+
* @return list of item using only requested fields.
318314
* @throws AirtableException
319315
* @throws HttpResponseException
320316
*/
@@ -385,7 +381,7 @@ private List<T> getList(HttpResponse<Records> response) {
385381
*/
386382
public T find(String id) throws AirtableException {
387383

388-
RecordItem body = null;
384+
RecordItem body;
389385

390386
HttpResponse<RecordItem> response;
391387
try {
@@ -402,15 +398,14 @@ public T find(String id) throws AirtableException {
402398
body = response.getBody();
403399
} else {
404400
HttpResponseExceptionHandler.onResponse(response);
401+
body = null;
405402
}
406403

407404
try {
408405
return transform(body, this.type.newInstance() );
409406
} catch (InvocationTargetException | IllegalAccessException | InstantiationException e) {
410-
LOG.error(e.getMessage(), e);
407+
throw new AirtableException(e);
411408
}
412-
413-
return null;
414409
}
415410

416411
/**

src/main/java/com/sybit/airtable/exception/HttpResponseExceptionHandler.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@
77

88
import com.google.gson.Gson;
99
import com.mashape.unirest.http.HttpResponse;
10-
import com.sybit.airtable.IOUtils;
1110
import com.sybit.airtable.vo.Error;
1211

12+
/**
13+
* Handle HTTP responses and create exceptions.
14+
*
15+
* @since 0.1
16+
*/
1317
public class HttpResponseExceptionHandler {
1418

1519
public static void onResponse(HttpResponse response) throws AirtableException {
1620

17-
Integer statusCode = response.getStatus();
18-
String message = IOUtils.convertStreamToString(response.getRawBody());
21+
final Integer statusCode = response.getStatus();
22+
String message = convertStreamToString(response.getRawBody());
1923

20-
Gson gson = new Gson();
24+
final Gson gson = new Gson();
2125
Error err = gson.fromJson(message, Error.class);
2226
switch (statusCode) {
2327
case 401:
@@ -48,4 +52,9 @@ public static void onResponse(HttpResponse response) throws AirtableException {
4852
throw new AirtableException("UNDEFINED_ERROR", message, statusCode);
4953
}
5054
}
55+
56+
public static String convertStreamToString(java.io.InputStream is) {
57+
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
58+
return s.hasNext() ? s.next() : "";
59+
}
5160
}

src/test/java/com/sybit/airtable/BaseTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
package com.sybit.airtable;
77

88
import com.sybit.airtable.exception.AirtableException;
9-
import static org.junit.Assert.assertEquals;
10-
import static org.junit.Assert.assertNotNull;
119
import org.junit.Before;
1210
import org.junit.Test;
1311

12+
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertNotNull;
14+
1415
/**
1516
*
1617
* @author fzr
@@ -48,7 +49,10 @@ public void baseNameTest(){
4849
Base base = new Base("base", this.airtable);
4950
assertEquals(base.name(),"base");
5051
}
51-
52-
52+
53+
@Test(expected = java.lang.AssertionError.class )
54+
public void baseAssertationTest() {
55+
Base base = new Base(null,null);
56+
}
5357

5458
}

0 commit comments

Comments
 (0)