Skip to content

Commit fce39e4

Browse files
committed
Refactor import + add test assertions.
1 parent 352f317 commit fce39e4

File tree

6 files changed

+223
-83
lines changed

6 files changed

+223
-83
lines changed

build.gradle

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ repositories {
1313
}
1414

1515
group = 'org.typesense'
16-
version = '0.0.9-beta4'
16+
version = '0.0.9-beta6'
1717
sourceCompatibility = 1.8
1818
targetCompatibility = 1.8
1919

@@ -23,12 +23,14 @@ shadowJar {
2323
duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
2424

2525
manifest {
26-
attributes "Main-Class": "org.typesense.api.Client"
26+
attributes(
27+
"Main-Class": "org.typesense.api.Client",
28+
"Multi-Release": "true"
29+
)
2730
}
2831

2932
// @see https://youtrack.jetbrains.com/issue/KT-25709
3033
exclude("**/*.kotlin_metadata")
31-
exclude("**/*.kotlin_builtins")
3234

3335
// remove suffix `-all` as intellij can't find the library otherwise
3436
archiveClassifier.set("")
@@ -66,10 +68,9 @@ check.dependsOn integrationTest
6668

6769
dependencies {
6870
implementation "javax.xml.bind:jaxb-api:2.3.1"
69-
implementation "org.apache.logging.log4j:log4j-api:2.19.0"
70-
implementation "org.apache.logging.log4j:log4j-core:2.19.0"
71+
implementation "com.fasterxml.jackson.core:jackson-databind:2.14.1"
7172
implementation "io.swagger.core.v3:swagger-annotations:2.0.0"
72-
implementation "com.fasterxml.jackson.module:jackson-module-jaxb-annotations:2.14.1"
73+
implementation "org.apache.logging.log4j:log4j-core:2.19.0"
7374
implementation "com.squareup.okhttp3:okhttp:4.9.1"
7475

7576
testImplementation "junit:junit:4.12"

src/main/java/org/typesense/api/ApiCall.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ <B, Q, R> R post(String endpoint, B body, Q queryParameters, Class<R> responseCl
136136
return makeRequest(endpoint, queryParameters, rb, responseClass);
137137
}
138138

139+
<Q, R> R post(String endpoint, String body, Q queryParameters, Class<R> responseClass) throws Exception {
140+
RequestBody requestBody = RequestBody.create(body, JSON);
141+
Request.Builder rb = new Request.Builder().post(requestBody);
142+
return makeRequest(endpoint, queryParameters, rb, responseClass);
143+
}
144+
139145
<Q, R> R delete(String endpoint, Q queryParameters, Class<R> responseClass) throws Exception {
140146
Request.Builder rb = new Request.Builder().delete();
141147
return makeRequest(endpoint, queryParameters, rb, responseClass);

src/main/java/org/typesense/api/Documents.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import org.typesense.model.*;
55

6+
import java.util.ArrayList;
7+
import java.util.List;
68
import java.util.Map;
9+
import java.util.Collection;
710

811
public class Documents {
912

@@ -58,19 +61,21 @@ public String import_(String document, ImportDocumentsParameters queryParameters
5861
return this.apiCall.post(this.getEndPoint("import"),document, queryParameters,String.class);
5962
}
6063

61-
public String import_(java.util.Collection<Map<String, Object>> documents, ImportDocumentsParameters queryParameters) throws Exception {
64+
public String import_(Collection<?> documents,
65+
ImportDocumentsParameters queryParameters) throws Exception {
6266
ObjectMapper mapper = new ObjectMapper();
63-
String json="";
64-
for(Map<String, Object> document : documents){
67+
List<String> jsonLines = new ArrayList<>();
68+
69+
for (Object document : documents) {
6570
try {
66-
//Convert Map to JSON
67-
json = json.concat(mapper.writeValueAsString(document) + "\n");
71+
jsonLines.add(mapper.writeValueAsString(document));
6872
} catch (Exception e) {
6973
e.printStackTrace();
7074
}
7175
}
72-
json = json.trim();
73-
return this.apiCall.post(this.getEndPoint("import"),json,queryParameters, String.class);
76+
77+
String reqBody = String.join("\n", jsonLines);
78+
return this.apiCall.post(this.getEndPoint("import"), reqBody, queryParameters, String.class);
7479
}
7580

7681
public String getEndPoint(String target){

src/main/java/org/typesense/api/FieldTypes.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ public final class FieldTypes {
77
public static final String FLOAT = "float";
88
public static final String BOOL = "bool";
99

10+
public static final String OBJECT = "object";
11+
public static final String OBJECT_ARRAY = "object[]";
12+
1013
public static final String STRING_ARRAY = "string[]";
1114
public static final String INT32_ARRAY = "int32[]";
1215
public static final String INT64_ARRAY = "int64[]";
Lines changed: 141 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package org.typesense.api;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
35
import junit.framework.TestCase;
4-
import org.typesense.api.exceptions.TypesenseError;
6+
import org.junit.Assert;
57
import org.typesense.model.*;
8+
import org.typesense.api.exceptions.ObjectNotFound;
69

7-
import java.io.File;
8-
import java.io.FileNotFoundException;
910
import java.util.ArrayList;
1011
import java.util.HashMap;
1112
import java.util.List;
1213
import java.util.Map;
13-
import java.util.Scanner;
1414

1515
public class DocumentsTest extends TestCase {
1616

@@ -21,87 +21,116 @@ public void setUp() throws Exception {
2121
super.setUp();
2222
helper = new Helper();
2323
this.client = helper.getClient();
24-
helper.createTestCollection();
25-
}
26-
27-
public void tearDown() throws Exception {
28-
super.tearDown();
2924
helper.teardown();
25+
helper.createTestCollection();
3026
}
3127

3228
public void testRetrieveDocument() throws Exception {
3329
helper.createTestDocument();
34-
System.out.println(client.collections("books").documents("1").retrieve());
30+
Map<String, Object> resp = client.collections("books").documents("1").retrieve();
31+
Assert.assertEquals(6, resp.size());
32+
Assert.assertEquals("1", resp.get("id"));
3533
}
3634

3735
public void testCreateDocument() throws Exception {
38-
39-
String[] authors = {"shakspeare","william"};
36+
String[] authors = {"shakspeare", "william"};
4037
HashMap<String, Object> hmap = new HashMap<>();
41-
hmap.put("title","Romeo and juliet");
42-
hmap.put("authors",authors);
43-
hmap.put("image_url","fgfg");
44-
hmap.put("publication_year",1666);
45-
hmap.put("ratings_count",124);
46-
hmap.put("average_rating",3.2);
47-
hmap.put("publication_year_facet","dff");
48-
hmap.put("authors_facet",authors);
49-
hmap.put("id","1");
50-
51-
System.out.println(client.collections("books").documents().create(hmap));
38+
hmap.put("title", "Romeo and juliet");
39+
hmap.put("authors", authors);
40+
hmap.put("image_url", "fgfg");
41+
hmap.put("publication_year", 1666);
42+
hmap.put("ratings_count", 124);
43+
hmap.put("average_rating", 3.2);
44+
hmap.put("publication_year_facet", "dff");
45+
hmap.put("authors_facet", authors);
46+
hmap.put("id", "1");
47+
48+
Map<String, Object> resp = client.collections("books").documents().create(hmap);
49+
Assert.assertEquals(9, resp.size());
50+
Assert.assertEquals("1", resp.get("id"));
5251
}
5352

5453
public void testUpsertDocument() throws Exception {
54+
helper.createTestDocument();
55+
56+
Map<String, Object> resp = client.collections("books").documents("1").retrieve();
57+
Assert.assertEquals("Romeo and juliet", resp.get("title"));
5558

5659
String[] authors = new String[]{"jk", "Rowling"};
5760
HashMap<String, Object> hmap = new HashMap<>();
58-
hmap.put("title","harry potter");
59-
hmap.put("authors",authors);
60-
hmap.put("image_url","fgfg");
61-
hmap.put("publication_year",2001);
62-
hmap.put("ratings_count",231);
63-
hmap.put("average_rating",5.6);
64-
hmap.put("publication_year_facet","2001");
65-
hmap.put("authors_facet",authors);
66-
hmap.put("id","3");
67-
68-
System.out.println(client.collections("books").documents().upsert(hmap));
61+
hmap.put("title", "harry potter");
62+
hmap.put("authors", authors);
63+
hmap.put("image_url", "fgfg");
64+
hmap.put("publication_year", 2001);
65+
hmap.put("ratings_count", 231);
66+
hmap.put("average_rating", 5.6);
67+
hmap.put("publication_year_facet", "2001");
68+
hmap.put("authors_facet", authors);
69+
hmap.put("id", "1");
70+
71+
resp = client.collections("books").documents().upsert(hmap);
72+
Assert.assertEquals(9, resp.size());
73+
Assert.assertEquals("1", resp.get("id"));
74+
Assert.assertEquals("harry potter", resp.get("title"));
75+
76+
// try fetching the document back
77+
resp = client.collections("books").documents("1").retrieve();
78+
Assert.assertEquals("harry potter", resp.get("title"));
79+
}
6980

81+
public void testUpdateDocument() throws Exception {
82+
helper.createTestDocument();
83+
84+
String[] authors = new String[]{"JK Rowling"};
85+
HashMap<String, Object> document = new HashMap<>();
86+
document.put("title", "harry potter");
87+
document.put("authors", authors);
88+
document.put("publication_year", 2000);
89+
document.put("id", "1");
90+
client.collections("books").documents("1").update(document);
91+
92+
// try fetching the document back
93+
Map<String, Object> resp = client.collections("books").documents("1").retrieve();
94+
Assert.assertEquals("harry potter", resp.get("title"));
95+
Assert.assertEquals(2000, resp.get("publication_year"));
7096
}
7197

7298
public void testDeleteDocument() throws Exception {
7399
helper.createTestDocument();
74-
System.out.println(client.collections("books").documents("1").delete());
100+
client.collections("books").documents("1").delete();
101+
102+
try {
103+
client.collections("books").documents("1").retrieve();
104+
fail("Delete document failed.");
105+
} catch (ObjectNotFound expectedException) {
106+
107+
}
75108
}
76109

77110
public void testDeleteDocumentByQuery() throws Exception {
78111
helper.createTestDocument();
79112
DeleteDocumentsParameters deleteDocumentsParameters = new DeleteDocumentsParameters();
80113
deleteDocumentsParameters.filterBy("publication_year:=[1666]");
81114
deleteDocumentsParameters.batchSize(10);
82-
System.out.println(client.collections("books").documents().delete(deleteDocumentsParameters));
83-
}
115+
client.collections("books").documents().delete(deleteDocumentsParameters);
116+
try {
117+
client.collections("books").documents("1").retrieve();
118+
fail("Delete document by query failed.");
119+
} catch (ObjectNotFound expectedException) {
84120

85-
public void testUpdateDocument() throws Exception {
86-
helper.createTestDocument();
87-
String[] authors = new String[]{"Shakespeare", "william"};
88-
HashMap<String , Object> document = new HashMap<>();
89-
document.put("title","Romeo and juliet");
90-
document.put("authors",authors);
91-
document.put("id","1");
92-
client.collections("books").documents("1").update(document);
93-
//System.out.println(client.collections("books").documents("1").update(document));
121+
}
94122
}
95123

96124
public void testSearchDocuments() throws Exception {
97125
helper.createTestDocument();
98126
SearchParameters searchParameters = new SearchParameters()
99-
.q("romeo")
100-
.queryBy("title,authors")
101-
.prefix("false,true");
102-
org.typesense.model.SearchResult searchResult = client.collections("books").documents().search(searchParameters);
127+
.q("romeo")
128+
.queryBy("title,authors")
129+
.prefix("false,true");
103130

104-
System.out.println(searchResult);
131+
SearchResult searchResult = client.collections("books").documents().search(searchParameters);
132+
Assert.assertEquals(1, searchResult.getFound().intValue());
133+
Assert.assertEquals(1, searchResult.getHits().size());
105134
}
106135

107136
public void testImport() throws Exception {
@@ -110,49 +139,91 @@ public void testImport() throws Exception {
110139
ImportDocumentsParameters queryParameters = new ImportDocumentsParameters();
111140
List<Map<String, Object>> documentList = new ArrayList<>();
112141

113-
document1.put("countryName","India");
114-
document1.put("capital","Delhi");
115-
document1.put("gdp",23);
116-
document2.put("countryName","Us");
117-
document2.put("capital","Washington");
118-
document2.put("gdp",233);
142+
document1.put("countryName", "India");
143+
document1.put("capital", "Delhi");
144+
document1.put("gdp", 23);
145+
document2.put("countryName", "Us");
146+
document2.put("capital", "Washington");
147+
document2.put("gdp", 233);
119148

120149
documentList.add(document1);
121150
documentList.add(document2);
122-
123151
queryParameters.action("create");
124-
System.out.println(this.client.collections("books").documents().import_(documentList, queryParameters));
152+
153+
String countriesImport = this.client.collections("books").documents()
154+
.import_(documentList, queryParameters);
155+
Assert.assertFalse(countriesImport.contains("\"success\":false"));
125156
}
126157

127158
public void testImportAsString() throws Exception {
128159
ImportDocumentsParameters queryParameters = new ImportDocumentsParameters();
129160
queryParameters.action("create");
130161
String documentList = "{\"countryName\": \"India\", \"capital\": \"Washington\", \"gdp\": 5215}\n" +
131162
"{\"countryName\": \"Iran\", \"capital\": \"London\", \"gdp\": 5215}";
132-
System.out.println(this.client.collections("books").documents().import_(documentList, queryParameters));
163+
String booksImport = this.client.collections("books").documents().import_(documentList, queryParameters);
164+
Assert.assertFalse(booksImport.contains("\"success\":false"));
133165
}
134166

135167
public void testExportDocuments() throws Exception {
136168
helper.createTestDocument();
137169
ExportDocumentsParameters exportDocumentsParameters = new ExportDocumentsParameters();
138170
exportDocumentsParameters.setExcludeFields("id,publication_year,authors");
139-
System.out.println(client.collections("books").documents().export(exportDocumentsParameters));
171+
String exportStr = client.collections("books").documents().export(exportDocumentsParameters);
172+
String expectedExportStr = "{\"average_rating\":3.2,\"ratings_count\":124,\"title\":\"Romeo and juliet\"}";
173+
Assert.assertEquals(expectedExportStr, exportStr);
140174
}
141175

142176
public void testDirtyCreate() throws Exception {
143177
helper.createTestDocument();
178+
144179
ImportDocumentsParameters queryParameters = new ImportDocumentsParameters();
145180
queryParameters.dirtyValues(ImportDocumentsParameters.DirtyValuesEnum.COERCE_OR_REJECT);
146181
queryParameters.action("upsert");
147-
String[] authors = {"shakspeare","william"};
182+
183+
String[] authors = {"shakspeare", "william"};
148184
HashMap<String, Object> hmap = new HashMap<>();
149-
hmap.put("title", 111);
150-
hmap.put("authors",authors);
151-
hmap.put("publication_year",1666);
152-
hmap.put("ratings_count",124);
153-
hmap.put("average_rating",3.2);
154-
hmap.put("id","2");
155-
156-
System.out.println(this.client.collections("books").documents().create(hmap,queryParameters));
185+
186+
hmap.put("id", "1");
187+
hmap.put("authors", authors);
188+
hmap.put("publication_year", 1666);
189+
hmap.put("ratings_count", 124);
190+
hmap.put("average_rating", 3.2);
191+
// title is sent as an integer and not as string for testing coercion
192+
hmap.put("title", 1984);
193+
194+
this.client.collections("books").documents().create(hmap, queryParameters);
195+
Map<String, Object> resp = client.collections("books").documents("1").retrieve();
196+
Assert.assertEquals("1984", resp.get("title"));
197+
}
198+
199+
public void testNestedObjectImport() throws Exception {
200+
// create collection with nested objects support
201+
List<Field> fields = new ArrayList<>();
202+
fields.add(new Field().name("address").type(FieldTypes.OBJECT).optional(true));
203+
fields.add(new Field().name("tags").type(FieldTypes.OBJECT_ARRAY).optional(true));
204+
205+
CollectionSchema collectionSchema = new CollectionSchema();
206+
collectionSchema.name("items").fields(fields).setEnableNestedFields(true);
207+
client.collections().create(collectionSchema);
208+
209+
ImportDocumentsParameters queryParameters = new ImportDocumentsParameters();
210+
queryParameters.action("create");
211+
212+
List<NestedDocument> docs = new ArrayList<>();
213+
NestedDocument doc = new NestedDocument("LA", "CA", "USA")
214+
.addTag("color", "Red")
215+
.addTag("weight", "LOW");
216+
docs.add(doc);
217+
String itemImport = this.client.collections("items").documents().import_(docs, queryParameters);
218+
Assert.assertFalse(itemImport.contains("\"success\":false"));
219+
220+
// try searching on the nested document
221+
SearchParameters searchParameters = new SearchParameters()
222+
.q("red")
223+
.queryBy("tags");
224+
225+
SearchResult searchResult = client.collections("items").documents().search(searchParameters);
226+
Assert.assertEquals(1, searchResult.getFound().intValue());
227+
Assert.assertEquals(1, searchResult.getHits().size());
157228
}
158229
}

0 commit comments

Comments
 (0)