Skip to content

Commit c12c8c5

Browse files
author
Marvin Zhang
committed
chore: updated examples
1 parent a338c06 commit c12c8c5

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

src/main/java/io/crawlab/example/Main.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.HashMap;
66
import java.util.Arrays;
77

8+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
89
import io.crawlab.sdk.CrawlabSDK;
910

1011
public class Main {
@@ -24,20 +25,15 @@ public static void main(String[] args) {
2425
dataList.add(Map.of("name", "Jane", "age", 30));
2526
CrawlabSDK.saveItem(dataList);
2627

27-
// Passing Dynamic Arguments
28+
// Passing a Map directly
2829
Map<String, Object> dataMap = new HashMap<>();
2930
dataMap.put("name", "John");
3031
dataMap.put("age", 25);
3132
dataMap.put("skills", Arrays.asList("Java", "Python"));
3233
CrawlabSDK.saveItem(dataMap);
3334

34-
// Using varargs for multiple items
35-
CrawlabSDK.saveItem(
36-
Map.of("name", "John", "age", 25),
37-
Map.of("name", "Jane", "age", 30)
38-
);
39-
4035
// Using custom objects
36+
@JsonSerialize
4137
class Person {
4238
private String name;
4339
private int age;
@@ -46,8 +42,15 @@ public Person(String name, int age) {
4642
this.name = name;
4743
this.age = age;
4844
}
49-
}
5045

46+
public String getName() {
47+
return name;
48+
}
49+
50+
public int getAge() {
51+
return age;
52+
}
53+
}
5154
Person person1 = new Person("John", 25);
5255
Person person2 = new Person("Jane", 30);
5356
CrawlabSDK.saveItem(person1, person2);

src/test/java/io/crawlab/sdk/CrawlabSDKTest.java

+29-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.crawlab.sdk;
22

3+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
34
import org.junit.jupiter.api.Test;
45
import org.junit.jupiter.api.BeforeEach;
56

@@ -19,31 +20,53 @@ void setUp() {
1920

2021
@Test
2122
void testSaveItemWithMap() {
22-
Map<String, Object> data = Map.of("name", "John", "age", 25);
23-
CrawlabSDK.saveItem(data);
23+
Map<String, Object> data1 = Map.of("name", "John", "age", 25);
24+
Map<String, Object> data2 = Map.of("name", "Jane", "age", 30);
25+
CrawlabSDK.saveItem(data1, data2);
2426
String output = outputStreamCaptor.toString().trim();
2527

2628
assertTrue(output.contains("\"type\":\"data\""));
2729
assertTrue(output.contains("\"payload\":["));
2830
assertTrue(output.contains("\"name\":\"John\""));
2931
assertTrue(output.contains("\"age\":25"));
32+
assertTrue(output.contains("\"name\":\"Jane\""));
33+
assertTrue(output.contains("\"age\":30"));
3034
assertTrue(output.contains("\"ipc\":true"));
3135
}
3236

3337
@Test
3438
void testSaveItemWithClass() {
35-
TestData data = new TestData("John", 25);
36-
CrawlabSDK.saveItem(data);
39+
Person data1 = new Person("John", 25);
40+
Person data2 = new Person("Jane", 30);
41+
CrawlabSDK.saveItem(data1, data2);
3742
String output = outputStreamCaptor.toString().trim();
3843

3944
assertTrue(output.contains("\"type\":\"data\""));
4045
assertTrue(output.contains("\"payload\":["));
4146
assertTrue(output.contains("\"name\":\"John\""));
4247
assertTrue(output.contains("\"age\":25"));
48+
assertTrue(output.contains("\"name\":\"Jane\""));
49+
assertTrue(output.contains("\"age\":30"));
4350
assertTrue(output.contains("\"ipc\":true"));
4451
}
4552

4653
// Helper class for testing object serialization
47-
private record TestData(String name, int age) {
54+
@JsonSerialize
55+
class Person {
56+
private String name;
57+
private int age;
58+
59+
public Person(String name, int age) {
60+
this.name = name;
61+
this.age = age;
62+
}
63+
64+
public String getName() {
65+
return name;
66+
}
67+
68+
public int getAge() {
69+
return age;
70+
}
4871
}
49-
}
72+
}

0 commit comments

Comments
 (0)