Skip to content

Commit cc7805e

Browse files
committed
add bulk api specifications for all clients
1 parent 7e164bb commit cc7805e

File tree

9 files changed

+403
-8
lines changed

9 files changed

+403
-8
lines changed

core/src/main/scala/app/softnetwork/elastic/client/bulk/package.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ package object bulk {
5959
else 0.0
6060

6161
def hasFailures: Boolean = failedCount > 0
62+
63+
def totalCount: Int = successCount + failedCount
6264
}
6365

6466
sealed trait DocumentResult {

core/src/test/scala/app/softnetwork/elastic/client/file/FileSourceSpec.scala

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,119 @@ class FileSourceSpec extends AnyWordSpec with Matchers with ScalaFutures with Be
118118
result.last should include("Bob")
119119
}
120120

121+
"read JSON Array file with nested arrays and objects" in {
122+
val tempFile = File.createTempFile("test-nested", ".json")
123+
tempFile.deleteOnExit()
124+
125+
val writer = new java.io.PrintWriter(tempFile)
126+
writer.println("""[
127+
| { "uuid": "A12", "name": "Homer Simpson", "birthDate": "1967-11-21", "childrenCount": 0 },
128+
| { "uuid": "A14", "name": "Moe Szyslak", "birthDate": "1967-11-21", "childrenCount": 0 },
129+
| { "uuid": "A16", "name": "Barney Gumble2", "birthDate": "1969-05-09",
130+
| "children": [
131+
| { "parentId": "A16", "name": "Steve Gumble", "birthDate": "1999-05-09" },
132+
| { "parentId": "A16", "name": "Josh Gumble", "birthDate": "2002-05-09" }
133+
| ],
134+
| "childrenCount": 2
135+
| }
136+
|]""".stripMargin)
137+
writer.close()
138+
139+
val result = FileSourceFactory
140+
.fromFile(tempFile.getAbsolutePath, format = JsonArray)
141+
.runWith(Sink.seq)
142+
.futureValue
143+
144+
result should have size 3
145+
146+
// Verify first element (no children)
147+
result.head should include("Homer Simpson")
148+
result.head should include("A12")
149+
result.head should include("childrenCount")
150+
151+
// Verify second element (no children)
152+
result(1) should include("Moe Szyslak")
153+
result(1) should include("A14")
154+
155+
// Verify third element (with nested children array)
156+
val thirdElement = result(2)
157+
thirdElement should include("Barney Gumble2")
158+
thirdElement should include("A16")
159+
thirdElement should include("children")
160+
thirdElement should include("Steve Gumble")
161+
thirdElement should include("Josh Gumble")
162+
thirdElement should include("parentId")
163+
164+
// Parse and validate nested structure
165+
166+
val mapper = JacksonConfig.objectMapper
167+
168+
val jsonNode = mapper.readTree(thirdElement)
169+
jsonNode.get("uuid").asText() shouldBe "A16"
170+
jsonNode.get("childrenCount").asInt() shouldBe 2
171+
172+
val children = jsonNode.get("children")
173+
children.isArray shouldBe true
174+
children.size() shouldBe 2
175+
176+
children.get(0).get("name").asText() shouldBe "Steve Gumble"
177+
children.get(0).get("parentId").asText() shouldBe "A16"
178+
179+
children.get(1).get("name").asText() shouldBe "Josh Gumble"
180+
children.get(1).get("parentId").asText() shouldBe "A16"
181+
}
182+
183+
"handle JSON Array with empty nested arrays" in {
184+
val tempFile = File.createTempFile("test-empty-nested", ".json")
185+
tempFile.deleteOnExit()
186+
187+
val writer = new java.io.PrintWriter(tempFile)
188+
writer.println("""[
189+
| { "uuid": "A12", "name": "Test", "children": [] }
190+
|]""".stripMargin)
191+
writer.close()
192+
193+
val result = FileSourceFactory
194+
.fromFile(tempFile.getAbsolutePath, format = JsonArray)
195+
.runWith(Sink.seq)
196+
.futureValue
197+
198+
result should have size 1
199+
result.head should include("children")
200+
result.head should include("[]")
201+
}
202+
203+
"handle deeply nested JSON structures" in {
204+
val tempFile = File.createTempFile("test-deep-nested", ".json")
205+
tempFile.deleteOnExit()
206+
207+
val writer = new java.io.PrintWriter(tempFile)
208+
writer.println("""[
209+
| {
210+
| "level1": {
211+
| "level2": {
212+
| "level3": {
213+
| "data": "deep value",
214+
| "array": [1, 2, 3]
215+
| }
216+
| }
217+
| }
218+
| }
219+
|]""".stripMargin)
220+
writer.close()
221+
222+
val result = FileSourceFactory
223+
.fromFile(tempFile.getAbsolutePath, format = JsonArray)
224+
.runWith(Sink.seq)
225+
.futureValue
226+
227+
result should have size 1
228+
result.head should include("deep value")
229+
result.head should include("level1")
230+
result.head should include("level2")
231+
result.head should include("level3")
232+
}
233+
121234
"read Parquet file" in {
122235
// Create temporary Parquet file
123236
val tempDir = Files.createTempDirectory("parquet-test").toFile
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package app.softnetwork.elastic.client
2+
3+
import app.softnetwork.elastic.client.spi.JestClientSpi
4+
import app.softnetwork.elastic.scalatest.EmbeddedElasticTestKit
5+
6+
class JestBulkApiSpec extends BulkApiSpec with EmbeddedElasticTestKit {
7+
override lazy val client: BulkApi = new JestClientSpi().client(elasticConfig)
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package app.softnetwork.elastic.client
2+
3+
import app.softnetwork.elastic.client.spi.RestHighLevelClientSpi
4+
import app.softnetwork.elastic.scalatest.EmbeddedElasticTestKit
5+
6+
class RestHighLevelBulkApiSpec extends BulkApiSpec with EmbeddedElasticTestKit {
7+
override lazy val client: BulkApi = new RestHighLevelClientSpi().client(elasticConfig)
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package app.softnetwork.elastic.client
2+
3+
import app.softnetwork.elastic.client.spi.RestHighLevelClientSpi
4+
import app.softnetwork.elastic.scalatest.ElasticDockerTestKit
5+
6+
class RestHighLevelBulkApiSpec extends BulkApiSpec with ElasticDockerTestKit {
7+
override lazy val client: BulkApi = new RestHighLevelClientSpi().client(elasticConfig)
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package app.softnetwork.elastic.client
2+
3+
import app.softnetwork.elastic.client.spi.JavaClientSpi
4+
import app.softnetwork.elastic.scalatest.ElasticDockerTestKit
5+
6+
class JavaBulkApiSpec extends BulkApiSpec with ElasticDockerTestKit {
7+
override lazy val client: BulkApi = new JavaClientSpi().client(elasticConfig)
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package app.softnetwork.elastic.client
2+
3+
import app.softnetwork.elastic.client.spi.JavaClientSpi
4+
import app.softnetwork.elastic.scalatest.ElasticDockerTestKit
5+
6+
class JavaBulkApiSpec extends BulkApiSpec with ElasticDockerTestKit {
7+
override lazy val client: BulkApi = new JavaClientSpi().client(elasticConfig)
8+
}

0 commit comments

Comments
 (0)