Skip to content

Commit

Permalink
Add tests. Get TestDtos from live server and move to match naming for…
Browse files Browse the repository at this point in the history
… packages.
  • Loading branch information
Layoric committed Oct 24, 2024
1 parent aeb2a4f commit a0b82a7
Show file tree
Hide file tree
Showing 5 changed files with 4,675 additions and 1,858 deletions.
3 changes: 1 addition & 2 deletions src/AndroidClient/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ allprojects {
}
}
}
// This code is where all the magic happens and fixes the error.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

package net.servicestack.client;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.GsonBuilder;
import junit.framework.TestCase;
import net.servicestack.client.dto.*;
import net.servicestack.client.tests.TestDtos;

public class GsonTests extends TestCase {

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

Gson gson = new Gson();
Gson gson = new GsonBuilder()
.setFieldNamingStrategy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.create();

HelloResponse response = gson.fromJson(json, HelloResponse.class);
TestDtos.HelloResponse response = gson.fromJson(json, TestDtos.HelloResponse.class);

assertEquals("World", response.getResult());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
import net.servicestack.client.JsonServiceClient;

import net.servicestack.client.WebServiceException;
import net.servicestack.client.tests.TestDtos;
import test.dtos.*;

import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Optional;

public class JsonServiceClientTests extends TestCase {

Expand Down Expand Up @@ -93,4 +97,99 @@ public void test_can_change_basePath() {
client.setBasePath("/api/");
assertEquals("https://test.servicestack.net/api/", client.getReplyUrl());
}

public void test_Can_Post_file_with_Request() {
try {
TestDtos.SpeechToText request = new TestDtos.SpeechToText();
request.setTag("ztag");
request.setRefId("zid");

byte[] fileBytes = "Hello World".getBytes(StandardCharsets.UTF_8);
FileUpload[] files = new FileUpload[]{
new FileUpload(
"audio", // fieldName
"test.txt", // fileName
"text/plain", // contentType
fileBytes // data
)
};

TestDtos.GenerationResponse response = client.postFilesWithRequest(
"/api/SpeechToText",
request,
files,
TestDtos.GenerationResponse.class
);

List<TestDtos.TextOutput> outputs = response.getTextOutputs();

assertNotNull("Response should not be null", response);
assertNotNull("Text outputs should not be null", outputs);
assertEquals("Should match expected output", "audio, Audio 11, test.txt, text/plain", outputs.get(0).getText());
assertEquals("Should match expected tag", "Tag ztag", outputs.get(1).getText());
assertEquals("Should match expected refId", "RefId zid", outputs.get(2).getText());

} catch (Exception e) {
fail("Error during test: " + e.getMessage());
}
}

public void test_Can_Post_Multiple_files_with_Request() {
try {
TestDtos.TestFileUploads request = new TestDtos.TestFileUploads();
request.setId(1);
request.setRefId("zid");

byte[] textFileBytes = "Hello World".getBytes(StandardCharsets.UTF_8);
byte[] markdownFileBytes = "## Heading".getBytes(StandardCharsets.UTF_8);

FileUpload[] files = new FileUpload[]{
new FileUpload(
"audio", // fieldName
"test.txt", // fileName
"text/plain", // contentType
textFileBytes // data
),
new FileUpload(
"content", // fieldName
"test.md", // fileName
"text/markdown", // contentType
markdownFileBytes // data
)
};

TestDtos.TestFileUploadsResponse response = client.postFilesWithRequest(
"/api/TestFileUploads",
request,
files,
TestDtos.TestFileUploadsResponse.class
);

assertNotNull("Response should not be null", response);
assertEquals("Id should match", Optional.of(1), response.getId());
assertEquals("RefId should match", "zid", response.getRefId());
assertEquals("Should have correct number of files", 2, response.getFiles().size());

// Verify first file
TestDtos.UploadInfo file1 = response.getFiles().get(0);
assertEquals("First file name should match", "audio", file1.getName());
assertEquals("First filename should match", "test.txt", file1.getFileName());
assertEquals("First file content length should match", Optional.of("Hello World".length()), file1.getContentLength());
assertEquals("First file content type should match", "text/plain", file1.getContentType());

// Verify second file
TestDtos.UploadInfo file2 = response.getFiles().get(1);
assertEquals("Second file name should match", "content", file2.getName());
assertEquals("Second filename should match", "test.md", file2.getFileName());
assertEquals("Second file content length should match", Optional.of("## Heading".length()), file2.getContentLength());
assertEquals("Second file content type should match", "text/markdown", file2.getContentType());

} catch (Exception e) {
fail("Error during test: " + e.getMessage());
}
}

// Async versions would not be needed in Java as the API is already based on blocking calls
// If async behavior is needed, it would typically be handled by the calling code using
// CompletableFuture or other async patterns external to these tests
}
Loading

0 comments on commit a0b82a7

Please sign in to comment.