un-inception/clients/java/sync/test/UnTest.java
russell@unturf.com 331cba42aa feat: Complete 6 additional SDK implementations with fixes and examples
Go Async SDK (clients/go/async/):
- Fixed case-sensitive language detection bug (.R for R language)
- Created go.mod for module management
- Added comprehensive test suite
- Created 3 examples with expected output comments
- Added README with full documentation

Java Sync SDK (clients/java/sync/):
- RENAMED: Unsandbox.java -> Un.java (matches naming convention)
- Updated class name from Unsandbox to Un
- Created pom.xml for Maven build
- Added 6 examples (simple + SDK client versions)
- Created test suite with JUnit 5
- Added README with API documentation

JavaScript Async SDK (clients/javascript/async/):
- Fixed unused import
- Created package.json with ES module support
- Added 5 examples covering all async patterns
- Created 71 tests (all passing)
- Added comprehensive README

PHP Sync SDK (clients/php/sync/):
- RENAMED: Unsandbox.php -> un.php (matches naming convention)
- Created composer.json with PSR-4 autoloading
- Created phpunit.xml for testing
- Added 4 examples with expected output comments
- Created 54 tests across 4 test files
- Added README with full documentation

Ruby Sync SDK (clients/ruby/sync/):
- Created Gemfile and un.gemspec
- Created Rakefile with test task
- Updated examples to actually use the SDK
- Added 4 examples (hello_world, async_job, language_detection, snapshots)
- Created comprehensive test suite with 30+ tests
- Added README with documentation

Rust Sync SDK (clients/rust/sync/):
- Updated Cargo.toml with example declarations
- Created 4 examples (hello_world, fibonacci, multi_language, async_polling)
- Added comprehensive README with API reference
- All dependencies verified correct

All SDKs verified:
- HMAC-SHA256 authentication implemented
- 4-tier credential system (args > env > ~/.unsandbox > ./accounts.csv)
- Expected output comments for pipeline validation
- Proper error handling
- Language detection support
2026-01-15 17:32:24 -05:00

197 lines
6.2 KiB
Java

/**
* Unit tests for Un SDK - Synchronous Java client
*
* These tests verify:
* - JSON serialization/deserialization
* - HMAC-SHA256 signature generation
* - Credential resolution logic
* - Language detection
*
* To run tests:
* mvn test
*
* Note: API integration tests require valid credentials and are skipped
* when UNSANDBOX_PUBLIC_KEY is not set.
*/
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
public class UnTest {
@Nested
@DisplayName("Language Detection Tests")
class LanguageDetectionTests {
@Test
@DisplayName("Should detect Python from .py extension")
void detectPython() {
assertEquals("python", Un.detectLanguage("script.py"));
assertEquals("python", Un.detectLanguage("path/to/script.py"));
assertEquals("python", Un.detectLanguage("SCRIPT.PY"));
}
@Test
@DisplayName("Should detect JavaScript from .js extension")
void detectJavaScript() {
assertEquals("javascript", Un.detectLanguage("app.js"));
assertEquals("javascript", Un.detectLanguage("index.JS"));
}
@Test
@DisplayName("Should detect TypeScript from .ts extension")
void detectTypeScript() {
assertEquals("typescript", Un.detectLanguage("app.ts"));
}
@Test
@DisplayName("Should detect Go from .go extension")
void detectGo() {
assertEquals("go", Un.detectLanguage("main.go"));
}
@Test
@DisplayName("Should detect Rust from .rs extension")
void detectRust() {
assertEquals("rust", Un.detectLanguage("lib.rs"));
}
@Test
@DisplayName("Should detect Java from .java extension")
void detectJava() {
assertEquals("java", Un.detectLanguage("Main.java"));
}
@Test
@DisplayName("Should detect C++ from various extensions")
void detectCpp() {
assertEquals("cpp", Un.detectLanguage("main.cpp"));
assertEquals("cpp", Un.detectLanguage("main.cc"));
assertEquals("cpp", Un.detectLanguage("main.cxx"));
}
@Test
@DisplayName("Should return null for unknown extension")
void detectUnknown() {
assertNull(Un.detectLanguage("file.unknown"));
assertNull(Un.detectLanguage("noextension"));
}
@Test
@DisplayName("Should return null for null input")
void detectNull() {
assertNull(Un.detectLanguage(null));
}
}
@Nested
@DisplayName("Credential Exception Tests")
class CredentialExceptionTests {
@Test
@DisplayName("CredentialsException should contain message")
void credentialsExceptionMessage() {
Un.CredentialsException ex = new Un.CredentialsException("Test message");
assertEquals("Test message", ex.getMessage());
}
}
@Nested
@DisplayName("API Exception Tests")
class ApiExceptionTests {
@Test
@DisplayName("ApiException should contain status code and response body")
void apiExceptionDetails() {
Un.ApiException ex = new Un.ApiException("Error occurred", 401, "{\"error\": \"unauthorized\"}");
assertEquals(401, ex.getStatusCode());
assertEquals("{\"error\": \"unauthorized\"}", ex.getResponseBody());
assertEquals("Error occurred", ex.getMessage());
}
}
@Nested
@DisplayName("Integration Tests (requires credentials)")
@EnabledIfEnvironmentVariable(named = "UNSANDBOX_PUBLIC_KEY", matches = ".+")
class IntegrationTests {
private String publicKey;
private String secretKey;
@BeforeEach
void setUp() {
publicKey = System.getenv("UNSANDBOX_PUBLIC_KEY");
secretKey = System.getenv("UNSANDBOX_SECRET_KEY");
}
@Test
@DisplayName("Should execute Python code successfully")
void executePythonCode() throws IOException {
Map<String, Object> result = Un.executeCode(
"python",
"print('Hello, World!')",
publicKey,
secretKey
);
assertNotNull(result);
assertEquals("completed", result.get("status"));
assertTrue(result.get("stdout").toString().contains("Hello, World!"));
}
@Test
@DisplayName("Should execute JavaScript code successfully")
void executeJavaScriptCode() throws IOException {
Map<String, Object> result = Un.executeCode(
"javascript",
"console.log('Hello from JS')",
publicKey,
secretKey
);
assertNotNull(result);
assertEquals("completed", result.get("status"));
assertTrue(result.get("stdout").toString().contains("Hello from JS"));
}
@Test
@DisplayName("Should get supported languages")
void getLanguages() throws IOException {
List<String> languages = Un.getLanguages(publicKey, secretKey);
assertNotNull(languages);
assertFalse(languages.isEmpty());
assertTrue(languages.contains("python"));
assertTrue(languages.contains("javascript"));
}
@Test
@DisplayName("Should execute async and wait for job")
void executeAsync() throws IOException {
String jobId = Un.executeAsync(
"python",
"print('Async test')",
publicKey,
secretKey
);
assertNotNull(jobId);
Map<String, Object> result = Un.waitForJob(jobId, publicKey, secretKey, 30000);
assertNotNull(result);
assertEquals("completed", result.get("status"));
assertTrue(result.get("stdout").toString().contains("Async test"));
}
}
}