Skip to content

Commit a0b82a7

Browse files
committed
Add tests. Get TestDtos from live server and move to match naming for packages.
1 parent aeb2a4f commit a0b82a7

File tree

5 files changed

+4675
-1858
lines changed

5 files changed

+4675
-1858
lines changed

src/AndroidClient/build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,4 @@ allprojects {
3939
}
4040
}
4141
}
42-
// This code is where all the magic happens and fixes the error.
43-
}
42+
}

src/AndroidClient/client/src/test/java/net/servicestack/client/GsonTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
package net.servicestack.client;
44

5+
import com.google.gson.FieldNamingPolicy;
56
import com.google.gson.Gson;
67

78
import java.util.ArrayList;
89
import java.util.List;
910

11+
import com.google.gson.GsonBuilder;
1012
import junit.framework.TestCase;
11-
import net.servicestack.client.dto.*;
13+
import net.servicestack.client.tests.TestDtos;
1214

1315
public class GsonTests extends TestCase {
1416

@@ -117,9 +119,11 @@ public void test_Can_serialize_nested_classes() {
117119
public void test_Can_deserialize_Hello() {
118120
String json = "{\"Result\":\"World\"}\n";
119121

120-
Gson gson = new Gson();
122+
Gson gson = new GsonBuilder()
123+
.setFieldNamingStrategy(FieldNamingPolicy.UPPER_CAMEL_CASE)
124+
.create();
121125

122-
HelloResponse response = gson.fromJson(json, HelloResponse.class);
126+
TestDtos.HelloResponse response = gson.fromJson(json, TestDtos.HelloResponse.class);
123127

124128
assertEquals("World", response.getResult());
125129
}

src/AndroidClient/client/src/test/java/net/servicestack/client/JsonServiceClientTests.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
import net.servicestack.client.JsonServiceClient;
88

99
import net.servicestack.client.WebServiceException;
10+
import net.servicestack.client.tests.TestDtos;
1011
import test.dtos.*;
1112

1213
import java.net.HttpURLConnection;
14+
import java.nio.charset.StandardCharsets;
1315
import java.util.Calendar;
1416
import java.util.Date;
17+
import java.util.List;
18+
import java.util.Optional;
1519

1620
public class JsonServiceClientTests extends TestCase {
1721

@@ -93,4 +97,99 @@ public void test_can_change_basePath() {
9397
client.setBasePath("/api/");
9498
assertEquals("https://test.servicestack.net/api/", client.getReplyUrl());
9599
}
100+
101+
public void test_Can_Post_file_with_Request() {
102+
try {
103+
TestDtos.SpeechToText request = new TestDtos.SpeechToText();
104+
request.setTag("ztag");
105+
request.setRefId("zid");
106+
107+
byte[] fileBytes = "Hello World".getBytes(StandardCharsets.UTF_8);
108+
FileUpload[] files = new FileUpload[]{
109+
new FileUpload(
110+
"audio", // fieldName
111+
"test.txt", // fileName
112+
"text/plain", // contentType
113+
fileBytes // data
114+
)
115+
};
116+
117+
TestDtos.GenerationResponse response = client.postFilesWithRequest(
118+
"/api/SpeechToText",
119+
request,
120+
files,
121+
TestDtos.GenerationResponse.class
122+
);
123+
124+
List<TestDtos.TextOutput> outputs = response.getTextOutputs();
125+
126+
assertNotNull("Response should not be null", response);
127+
assertNotNull("Text outputs should not be null", outputs);
128+
assertEquals("Should match expected output", "audio, Audio 11, test.txt, text/plain", outputs.get(0).getText());
129+
assertEquals("Should match expected tag", "Tag ztag", outputs.get(1).getText());
130+
assertEquals("Should match expected refId", "RefId zid", outputs.get(2).getText());
131+
132+
} catch (Exception e) {
133+
fail("Error during test: " + e.getMessage());
134+
}
135+
}
136+
137+
public void test_Can_Post_Multiple_files_with_Request() {
138+
try {
139+
TestDtos.TestFileUploads request = new TestDtos.TestFileUploads();
140+
request.setId(1);
141+
request.setRefId("zid");
142+
143+
byte[] textFileBytes = "Hello World".getBytes(StandardCharsets.UTF_8);
144+
byte[] markdownFileBytes = "## Heading".getBytes(StandardCharsets.UTF_8);
145+
146+
FileUpload[] files = new FileUpload[]{
147+
new FileUpload(
148+
"audio", // fieldName
149+
"test.txt", // fileName
150+
"text/plain", // contentType
151+
textFileBytes // data
152+
),
153+
new FileUpload(
154+
"content", // fieldName
155+
"test.md", // fileName
156+
"text/markdown", // contentType
157+
markdownFileBytes // data
158+
)
159+
};
160+
161+
TestDtos.TestFileUploadsResponse response = client.postFilesWithRequest(
162+
"/api/TestFileUploads",
163+
request,
164+
files,
165+
TestDtos.TestFileUploadsResponse.class
166+
);
167+
168+
assertNotNull("Response should not be null", response);
169+
assertEquals("Id should match", Optional.of(1), response.getId());
170+
assertEquals("RefId should match", "zid", response.getRefId());
171+
assertEquals("Should have correct number of files", 2, response.getFiles().size());
172+
173+
// Verify first file
174+
TestDtos.UploadInfo file1 = response.getFiles().get(0);
175+
assertEquals("First file name should match", "audio", file1.getName());
176+
assertEquals("First filename should match", "test.txt", file1.getFileName());
177+
assertEquals("First file content length should match", Optional.of("Hello World".length()), file1.getContentLength());
178+
assertEquals("First file content type should match", "text/plain", file1.getContentType());
179+
180+
// Verify second file
181+
TestDtos.UploadInfo file2 = response.getFiles().get(1);
182+
assertEquals("Second file name should match", "content", file2.getName());
183+
assertEquals("Second filename should match", "test.md", file2.getFileName());
184+
assertEquals("Second file content length should match", Optional.of("## Heading".length()), file2.getContentLength());
185+
assertEquals("Second file content type should match", "text/markdown", file2.getContentType());
186+
187+
} catch (Exception e) {
188+
fail("Error during test: " + e.getMessage());
189+
}
190+
}
191+
192+
// Async versions would not be needed in Java as the API is already based on blocking calls
193+
// If async behavior is needed, it would typically be handled by the calling code using
194+
// CompletableFuture or other async patterns external to these tests
96195
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy