diff --git a/pom.xml b/pom.xml index 78e9f440d..f952129f8 100644 --- a/pom.xml +++ b/pom.xml @@ -40,11 +40,37 @@ 4.13.1 test + + org.jacoco + jacoco-maven-plugin + 0.8.7 + + + org.mockito + mockito-core + 2.23.0 + test + + + org.powermock + powermock-api-mockito2 + 2.0.2 + test + + + org.powermock + powermock-module-junit4 + 2.0.2 + test + UTF-8 -Xdoclint:none + 3.0.0-M5 + 8 + 8 @@ -121,6 +147,41 @@ + + org.jacoco + jacoco-maven-plugin + 0.8.7 + + + default-prepare-agent + + prepare-agent + + + + default-report + + report + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 8 + 8 + + + + + + + + + diff --git a/src/test/java/com/tencentcloudapi/unit/common/AbstractClientInitTest.java b/src/test/java/com/tencentcloudapi/unit/common/AbstractClientInitTest.java new file mode 100644 index 000000000..e8692537d --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/common/AbstractClientInitTest.java @@ -0,0 +1,48 @@ +package com.tencentcloudapi.unit.common; + +import com.tencentcloudapi.common.AbstractClient; +import com.tencentcloudapi.common.Credential; +import com.tencentcloudapi.common.profile.ClientProfile; +import com.tencentcloudapi.common.profile.HttpProfile; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + + +public class AbstractClientInitTest { + private Credential credential; + private ClientProfile clientProfile; + + @Before + public void setUp() { + credential = + new Credential( + System.getenv("TENCENTCLOUD_SECRET_ID"), System.getenv("TENCENTCLOUD_SECRET_KEY")); + clientProfile = new ClientProfile(); + + HttpProfile httpProfile = new HttpProfile(); + clientProfile.setHttpProfile(httpProfile); + clientProfile.setSignMethod("HmacSHA256"); + } + + @Test + public void testAbstractClientWithEndpointVersionCredentialRegion() { + String endpoint = "example.com"; + String version = "v1"; + String region = "ap-guangzhou"; + AbstractClient client = new AbstractClient(endpoint, version, credential, region) { + }; + + client.setRegion(region); + client.setClientProfile(clientProfile); + client.setCredential(credential); + assertEquals(region, client.getRegion()); + assertNotNull(client.getCredential()); + assertNotNull(client.getClientProfile()); + + } + + +} + diff --git a/src/test/java/com/tencentcloudapi/unit/common/AbstractClientInternalRequestTest.java b/src/test/java/com/tencentcloudapi/unit/common/AbstractClientInternalRequestTest.java new file mode 100644 index 000000000..eab57e985 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/common/AbstractClientInternalRequestTest.java @@ -0,0 +1,59 @@ +package com.tencentcloudapi.unit.common; + +import com.tencentcloudapi.common.AbstractClient; +import com.tencentcloudapi.common.AbstractModel; +import com.tencentcloudapi.common.Credential; +import com.tencentcloudapi.common.profile.ClientProfile; +import org.junit.Before; +import org.junit.Test; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.reflect.Whitebox; + +import java.util.HashMap; + +class Client extends AbstractClient { + public Client(String endpoint, String version, Credential credential, String region) { + super(endpoint, version, credential, region); + } + + public Client(String endpoint, String version, Credential credential, String region, ClientProfile profile) { + super(endpoint, version, credential, region, profile); + } +} + +public class AbstractClientInternalRequestTest { + private Credential credential; + private ClientProfile clientProfile; + private String endpoint = "example.com"; + private String version = "v1"; + private String region = "ap-guangzhou"; + + @Before + public void setUp() { + credential = + new Credential( + System.getenv("TENCENTCLOUD_SECRET_ID"), System.getenv("TENCENTCLOUD_SECRET_KEY"), "123e4567-e89b-12d3-a456-426614174000"); + clientProfile = new ClientProfile(); + } + + @Test + public void testAbstractInternalRequest() throws Exception { + clientProfile.setBackupEndpoint("example.com"); + Client client = new Client(endpoint, version, credential, region, clientProfile); + AbstractModel req = new AbstractModel() { + @Override + protected void toMap(HashMap map, String prefix) { + } + }; + String actionName = "ActionName"; + + Client client1 = PowerMockito.spy(client); + PowerMockito.doReturn(null).when(client1, "internalRequestRaw", req, actionName); + try { + Whitebox.invokeMethod(client1, "internalRequest", req, actionName); + } catch (Exception e) { + System.out.println(e); + } + } +} + diff --git a/src/test/java/com/tencentcloudapi/unit/common/AbstractClientPrivateTest.java b/src/test/java/com/tencentcloudapi/unit/common/AbstractClientPrivateTest.java new file mode 100644 index 000000000..c5aab57ed --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/common/AbstractClientPrivateTest.java @@ -0,0 +1,284 @@ +package com.tencentcloudapi.unit.common; + +import com.tencentcloudapi.common.*; +import com.tencentcloudapi.common.exception.TencentCloudSDKException; +import com.tencentcloudapi.common.http.HttpConnection; +import com.tencentcloudapi.common.profile.ClientProfile; +import com.tencentcloudapi.common.profile.HttpProfile; +import okhttp3.Headers; +import okhttp3.Protocol; +import okhttp3.Request; +import okhttp3.Response; +import org.junit.Before; +import org.junit.Test; +import org.powermock.reflect.Whitebox; + +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.X509TrustManager; +import java.io.IOException; +import java.net.InetAddress; +import java.net.Socket; +import java.net.UnknownHostException; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import java.util.HashMap; +import java.util.Map; + +import static com.tencentcloudapi.common.profile.Language.ZH_CN; + + +public class AbstractClientPrivateTest { + private Credential credential; + private ClientProfile clientProfile; + private AbstractClient client; + private String endpoint = "example.com"; + private String version = "v1"; + private String region = "ap-guangzhou"; + + @Before + public void setUp() { + credential = + new Credential( + System.getenv("TENCENTCLOUD_SECRET_ID"), System.getenv("TENCENTCLOUD_SECRET_KEY"), "123e4567-e89b-12d3-a456-426614174000"); + clientProfile = new ClientProfile(); + } + + @Test + public void testAbstractClientGetHeaders() throws Exception { + clientProfile.setUnsignedPayload(true); + clientProfile.setLanguage(ZH_CN); + client = new AbstractClient(endpoint, version, credential, region, clientProfile) { + }; + + HashMap map = Whitebox.invokeMethod(client, "getHeaders"); + assert "123e4567-e89b-12d3-a456-426614174000" == map.get("X-TC-Token"); + assert "zh-CN" == map.get("X-TC-Language"); + assert "UNSIGNED-PAYLOAD" == map.get("X-TC-Content-SHA256"); + } + + @Test + public void testAbstractClientTrySetProxy() throws Exception { + HttpProfile httpProfile = new HttpProfile(); + httpProfile.setProxyHost("127.0.0.1"); + httpProfile.setProxyPort(80); + httpProfile.setProxyUsername("user"); + httpProfile.setProxyPassword("pwd"); + clientProfile.setHttpProfile(httpProfile); + client = new AbstractClient(endpoint, version, credential, region, clientProfile) { + }; + + HttpConnection conn = new HttpConnection(10, 10, 10); + Object result = Whitebox.invokeMethod(client, "trySetProxy", conn); + assert result == null; + + httpProfile.setProxyHost(""); + clientProfile.setHttpProfile(httpProfile); + client = new AbstractClient(endpoint, version, credential, region, clientProfile) { + }; + result = Whitebox.invokeMethod(client, "trySetProxy", conn); + assert result == null; + + httpProfile.setProxyHost("127.0.0.1"); + httpProfile.setProxyUsername(""); + clientProfile.setHttpProfile(httpProfile); + client = new AbstractClient(endpoint, version, credential, region, clientProfile) { + }; + result = Whitebox.invokeMethod(client, "trySetProxy", conn); + assert result == null; + } + + @Test + public void testAbstractClientTrySetSSLSocketFactory() throws Exception { + HttpProfile httpProfile = new HttpProfile(); + SSLSocketFactory factory = new SSLSocketFactory() { + @Override + public String[] getDefaultCipherSuites() { + return new String[0]; + } + + @Override + public String[] getSupportedCipherSuites() { + return new String[0]; + } + + @Override + public Socket createSocket(Socket socket, String s, int i, boolean b) throws IOException { + return null; + } + + @Override + public Socket createSocket(String s, int i) throws IOException, UnknownHostException { + return null; + } + + @Override + public Socket createSocket(String s, int i, InetAddress inetAddress, int i1) throws IOException, UnknownHostException { + return null; + } + + @Override + public Socket createSocket(InetAddress inetAddress, int i) throws IOException { + return null; + } + + @Override + public Socket createSocket(InetAddress inetAddress, int i, InetAddress inetAddress1, int i1) throws IOException { + return null; + } + }; + httpProfile.setSslSocketFactory(factory); + X509TrustManager trustManager = new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { + } + + @Override + public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + }; + httpProfile.setX509TrustManager(trustManager); + clientProfile.setHttpProfile(httpProfile); + + HttpConnection conn = new HttpConnection(10, 10, 10); + client = new AbstractClient(endpoint, version, credential, region, clientProfile) { + }; + + Whitebox.invokeMethod(client, "trySetSSLSocketFactory", conn); + } + + @Test + public void testAbstractClientTrySetHostnameVerifier() throws Exception { + HttpProfile httpProfile = new HttpProfile(); + httpProfile.setHostnameVerifier((s, sslSession) -> true); + clientProfile.setHttpProfile(httpProfile); + client = new AbstractClient(endpoint, version, credential, region, clientProfile) { + }; + + HttpConnection conn = new HttpConnection(10, 10, 10); + Whitebox.invokeMethod(client, "trySetHostnameVerifier", conn); + } + + @Test + public void testAbstractClientTrySetRegionBreaker() throws Exception { + clientProfile.setBackupEndpoint("example.com"); + client = new AbstractClient(endpoint, version, credential, region, clientProfile) { + }; + + Whitebox.invokeMethod(client, "trySetRegionBreaker"); + } + + @Test + public void testAbstractClientProcessResponseSSE() throws Exception { + // 创建一个Response实例 + Request request = new Request.Builder() + .url("https://example.com") + .build(); + + Headers headers = new Headers.Builder() + .add("Content-Type", "application/json") + .build(); + + Response.Builder builder = new Response.Builder() + .request(request) // 设置请求对象 + .protocol(Protocol.HTTP_2) // 设置协议版本,这里以HTTP/2为例 + .code(200) // 设置响应码 + .message("OK") // 设置响应消息 + .headers(headers) // 设置响应头 + .body(null) // 设置响应体,这里假设响应体为空 + .networkResponse(null) // 设置网络响应,这里假设没有网络响应 + .cacheResponse(null) // 设置缓存响应,这里假设没有缓存响应 + .priorResponse(null) // 设置之前的响应,这里假设没有之前的响应 + .sentRequestAtMillis(System.currentTimeMillis()) // 设置请求发送的时间戳 + .receivedResponseAtMillis(System.currentTimeMillis()); // 设置响应接收的时间戳 + builder.header("X-TC-RequestId", "123e4567-e89b-12d3"); + + Response resp = builder.build(); + // 创建Token实例 + CircuitBreaker circuitBreaker = new CircuitBreaker(); + CircuitBreaker.Token token = circuitBreaker.allow(); + + client = new AbstractClient(endpoint, version, credential, region, clientProfile) { + }; + Class typeOfT = CommonSSEResponse.class; + + Whitebox.invokeMethod(client, "processResponseSSE", resp, typeOfT, token); + + try { + Whitebox.invokeMethod(client, "processResponseSSE", resp, SSEResponseModel.class, token); + } catch (Exception e) {} + + CommonSSEResponse sse = new CommonSSEResponse(); + sse.setRequestId("123e4567-e89b-12d3"); + assert sse.getRequestId().equals("123e4567-e89b-12d3"); + } + + @Test + public void testAbstractClientGetMultipartPayload() throws Exception { + AbstractModel request = new AbstractModel() { + @Override + protected void toMap(HashMap map, String prefix) { + } + + @Override + protected HashMap getMultipartRequestParams() { + HashMap map = new HashMap<>(); + map.put("key", "value".getBytes()); + return map; + } + + @Override + protected String[] getBinaryParams() { + String[] strings = new String[1]; + strings[0] = "key"; + return strings; + } + }; + String boundary = "boundary"; + client = new AbstractClient(endpoint, version, credential, region, clientProfile) { + }; + + Whitebox.invokeMethod(client, "getMultipartPayload", request, boundary); + } + + @Test + public void testAbstractClientRetry() { + client = new AbstractClient(endpoint, version, credential, region, clientProfile) { + }; + client.setRegionBreaker(new CircuitBreaker()); + client.getRegionBreaker(); + + try { + client.retry(null, -1); + } catch (TencentCloudSDKException e) { + assert e.getMessage().contains("The number of retryTimes supported is 0 to 10."); + } + + class getRegionBreakerRequest extends AbstractModel { + getRegionBreakerRequest() { + } + + @Override + protected void toMap(HashMap map, String prefix) { + } + } + try { + client.retry(new getRegionBreakerRequest(), 0); + } catch (TencentCloudSDKException e) { + assert e.getMessage().contains("ClientSideError"); + } + } +} + + + + + + + + + diff --git a/src/test/java/com/tencentcloudapi/unit/common/AbstractModelTest.java b/src/test/java/com/tencentcloudapi/unit/common/AbstractModelTest.java new file mode 100644 index 000000000..b98303774 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/common/AbstractModelTest.java @@ -0,0 +1,68 @@ +package com.tencentcloudapi.unit.common; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import com.tencentcloudapi.common.AbstractModel; +import org.junit.Test; +import org.powermock.reflect.Whitebox; + +import java.util.HashMap; +import java.util.Map; + +import static junit.framework.Assert.assertTrue; + +public class AbstractModelTest { + @Test + public void fromJsonStringTest() { + TestModel testModel = new TestModel(); + TestModel.fromJsonString("{}", TestModel.class); + } + + @Test + public void getMultipartRequestParamsTest() throws Exception { + TestModel testModel = new TestModel(); + Map map = Whitebox.invokeMethod(testModel, "getMultipartRequestParams"); + assertTrue(map == null || map.isEmpty()); + } + + @Test + public void setParamSimpleTest() throws Exception { + TestModel testModel = new TestModel(); + String key = "key"; + Map map = new HashMap<>(); + Whitebox.invokeMethod(testModel, "setParamSimple", map, key, 123); + } + + @Test + public void setParamArraySimpleTest() throws Exception { + TestModel testModel = new TestModel(); + String key = "key"; + Map map = new HashMap<>(); + String[] V = new String[]{"a", "b"}; + Whitebox.invokeMethod(testModel, "setParamArraySimple", map, key, V); + } + + @Test + public void setParamObjTest() throws Exception { + TestModel testModel = new TestModel(); + String prefix = "prefix"; + Map map = new HashMap<>(); + Whitebox.invokeMethod(testModel, "setParamObj", map, prefix, testModel); + } + + @Test + public void setParamArrayObjTest() throws Exception { + TestModel testModel = new TestModel(); + String prefix = "prefix"; + Map map = new HashMap<>(); + TestModel[] array = new TestModel[]{testModel}; + Whitebox.invokeMethod(testModel, "setParamArrayObj", map, prefix, array); + } +} + +class TestModel extends AbstractModel { + @Override + protected void toMap(HashMap map, String prefix) { + } +} \ No newline at end of file diff --git a/src/test/java/com/tencentcloudapi/unit/common/CredentialTest.java b/src/test/java/com/tencentcloudapi/unit/common/CredentialTest.java new file mode 100644 index 000000000..2982ecd28 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/common/CredentialTest.java @@ -0,0 +1,41 @@ +package com.tencentcloudapi.unit.common; + +import com.tencentcloudapi.common.Credential; +import com.tencentcloudapi.common.exception.TencentCloudSDKException; +import org.junit.Test; +import org.powermock.reflect.Whitebox; + +public class CredentialTest { + @Test + public void testCredential() throws Exception { + class CredentialUpdater implements Credential.Updater { + @Override + public void update(Credential credential) throws TencentCloudSDKException { + throw new TencentCloudSDKException("test"); + } + } + CredentialUpdater updater = new CredentialUpdater(); + Credential credential = new Credential("secretId", "secretKey", "token", updater); + credential.getUpdater(); + credential.setUpdater(new CredentialUpdater()); + credential.setSecretId("secretId"); + credential.setSecretKey("secretKey"); + credential.setToken("token"); + + try { + Whitebox.invokeMethod(credential, "tryUpdate"); + } catch (Exception e) { + assert e.getMessage().contains("test"); + } + + Credential cred = new Credential(); + class testUpdater implements Credential.Updater { + @Override + public void update(Credential credential) throws TencentCloudSDKException { + } + } + testUpdater testUpdater = new testUpdater(); + Credential cred1 = new Credential("secretId", "secretKey", "token", testUpdater); + Whitebox.invokeMethod(cred1, "tryUpdate"); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/common/HttpConnectionTest.java b/src/test/java/com/tencentcloudapi/unit/common/HttpConnectionTest.java new file mode 100644 index 000000000..697e9ad9f --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/common/HttpConnectionTest.java @@ -0,0 +1,76 @@ +package com.tencentcloudapi.unit.common; + +import com.tencentcloudapi.common.exception.TencentCloudSDKException; +import com.tencentcloudapi.common.http.HttpConnection; +import okhttp3.Headers; +import org.junit.Test; + +import javax.net.ssl.SSLSocketFactory; +import java.io.IOException; +import java.net.InetAddress; +import java.net.Socket; +import java.net.UnknownHostException; + +public class HttpConnectionTest { + @Test + public void testGetRequest() throws IOException { + HttpConnection httpConnection = new HttpConnection(1, 1, 1); + try { + httpConnection.getRequest(""); + } catch (TencentCloudSDKException e) { + e.getMessage().contains("URL scheme 'http' or 'https' but no colon was found"); + } + } + + @Test + public void testGetRequestWithHeaders() throws IOException { + HttpConnection httpConnection = new HttpConnection(1, 1, 1); + Headers headers = new Headers.Builder().build(); + try { + httpConnection.getRequest("", headers); + } catch (TencentCloudSDKException e) { + e.getMessage().contains("URL scheme 'http' or 'https' but no colon was found"); + } + } + + @Test + public void testPostRequest() throws IOException { + HttpConnection httpConnection = new HttpConnection(1, 1, 1); + try { + httpConnection.postRequest("", "body"); + } catch (TencentCloudSDKException e) { + e.getMessage().contains("URL scheme 'http' or 'https' but no colon was found"); + } + } + + @Test + public void testPostRequestWithHeaders() throws IOException, TencentCloudSDKException { + HttpConnection httpConnection = new HttpConnection(1, 1, 1); + Headers headers = new Headers.Builder() + .add("Content-Type", "application/json") // 添加Content-Type头部字段 + .add("User-Agent", "OkHttp/3.12.12") // 添加User-Agent头部字段 + .build(); + httpConnection.postRequest("https://www.example.com", "body", headers); + + try { + httpConnection.postRequest("", "body", headers); + } catch (TencentCloudSDKException e) { + e.getMessage().contains("URL scheme 'http' or 'https' but no colon was found"); + } + } + + @Test + public void testPostRequestWithArrayBody() throws IOException, TencentCloudSDKException { + HttpConnection httpConnection = new HttpConnection(1, 1, 1); + Headers headers = new Headers.Builder() + .add("Content-Type", "application/json") // 添加Content-Type头部字段 + .add("User-Agent", "OkHttp/3.12.12") // 添加User-Agent头部字段 + .build(); + byte[] body = "body".getBytes(); + try { + httpConnection.postRequest("", body, headers); + } catch (TencentCloudSDKException e) { + e.getMessage().contains("URL scheme 'http' or 'https' but no colon was found"); + } + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/common/HttpProfileTest.java b/src/test/java/com/tencentcloudapi/unit/common/HttpProfileTest.java new file mode 100644 index 000000000..ed117f1f5 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/common/HttpProfileTest.java @@ -0,0 +1,25 @@ +package com.tencentcloudapi.unit.common; + +import com.tencentcloudapi.common.profile.HttpProfile; +import org.junit.Test; + +public class HttpProfileTest { + @Test + public void testHttpProfile() { + HttpProfile profile = new HttpProfile(); + profile.setEndpoint("https://unit.tencentcloudapi.com"); + assert profile.getEndpoint().equals("https://unit.tencentcloudapi.com"); + profile.setReadTimeout(10000); + assert profile.getReadTimeout() == 10000; + profile.setWriteTimeout(10000); + assert profile.getWriteTimeout() == 10000; + profile.setConnTimeout(10000); + assert profile.getConnTimeout() == 10000; + profile.setProtocol("http"); + assert profile.getProtocol().equals("http"); + profile.setRootDomain("unit.tencentcloudapi.com"); + assert profile.getRootDomain().equals("unit.tencentcloudapi.com"); + profile.setApigwEndpoint("https://unit.tencentcloudapi.com"); + assert profile.getApigwEndpoint().equals("https://unit.tencentcloudapi.com"); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/common/OIDCRoleArnProviderTest.java b/src/test/java/com/tencentcloudapi/unit/common/OIDCRoleArnProviderTest.java new file mode 100644 index 000000000..45ad7c8aa --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/common/OIDCRoleArnProviderTest.java @@ -0,0 +1,17 @@ +package com.tencentcloudapi.unit.common; + +import com.tencentcloudapi.common.exception.TencentCloudSDKException; +import com.tencentcloudapi.common.provider.OIDCRoleArnProvider; +import org.junit.Test; +import org.powermock.reflect.Whitebox; + + +public class OIDCRoleArnProviderTest { + @Test + public void testOIDCRoleArnProviderInit() throws TencentCloudSDKException { + OIDCRoleArnProvider oidcRoleArnProvider = new OIDCRoleArnProvider("ap-shanghai", "", "", "", "", 0L); + oidcRoleArnProvider.ExpirationReservationTime = 0L; + Whitebox.setInternalState(oidcRoleArnProvider,"isTke",false); + + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/common/RegionTest.java b/src/test/java/com/tencentcloudapi/unit/common/RegionTest.java new file mode 100644 index 000000000..5977eb1a5 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/common/RegionTest.java @@ -0,0 +1,34 @@ +package com.tencentcloudapi.unit.common; + +import com.tencentcloudapi.common.profile.Region; +import org.junit.Test; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; + +public class RegionTest { + @Test + public void testRegion() { + String[] expectedRegions = { + "ap-bangkok", "ap-beijing", "ap-chengdu", "ap-chongqing", "ap-guangzhou", "ap-guangzhou-open", + "ap-hongkong", "ap-mumbai", "ap-seoul", "ap-shanghai", "ap-shanghai-fsi", "ap-shenzhen-fsi", + "ap-singapore", "ap-tokyo", "eu-frankfurt", "eu-moscow", "na-ashburn", "na-siliconvalley", "na-toronto" + }; + for (String expectedRegion : expectedRegions) { + boolean found = false; + for (Region region : Region.values()) { + if (region.getValue().equals(expectedRegion)) { + found = true; + break; + } + } + assertTrue("Expected region " + expectedRegion + " to be present", found); + } + } + + @Test + public void testRegionValue() { + assertEquals("ap-bangkok", Region.Bangkok.getValue()); + assertEquals("ap-bangkok", Region.Bangkok.toString()); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/common/SSEResponseModelTest.java b/src/test/java/com/tencentcloudapi/unit/common/SSEResponseModelTest.java new file mode 100644 index 000000000..dfd812c0c --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/common/SSEResponseModelTest.java @@ -0,0 +1,81 @@ +package com.tencentcloudapi.unit.common; + +import com.tencentcloudapi.common.SSEResponseModel; +import com.tencentcloudapi.common.exception.TencentCloudSDKException; +import okhttp3.*; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.mockito.Mockito; +import org.powermock.reflect.Whitebox; + +import java.util.*; + +public class SSEResponseModelTest { + @Test + public void SSETest() throws Exception { + SSEResponseModel.SSE sse = new SSEResponseModel.SSE(); + sse.Id = "123"; + sse.Event = "123"; + sse.Data = "2024"; + sse.Retry = 0l; + assert sse.toString().equals("Id=123 Event=123 Retry=0 Data=2024"); + } + + @Test + public void SSEIteratorTest() throws Exception { + SSEModelTest sse = new SSEModelTest(); + Map map = new HashMap(); + Whitebox.invokeMethod(sse, "toMap", map, "prefix"); + + Request request = new Request.Builder() + .url("https://example.com") + .build(); + // 创建一个MediaType实例,指定响应体的媒体类型为JSON + MediaType jsonMediaType = MediaType.parse("application/json; charset=utf-8"); + + // 创建一个字符串作为响应体的内容 + String responseBodyContent = "{\"key\":\"value\"}"; + + // 使用ResponseBody.create方法创建一个ResponseBody实例 + ResponseBody responseBody = ResponseBody.create(jsonMediaType, responseBodyContent); + Response.Builder builder = new Response.Builder() + .request(request) // 设置请求对象 + .protocol(Protocol.HTTP_2) // 设置协议版本,这里以HTTP/2为例 + .code(200) // 设置响应码 + .message("OK"); // 设置响应消息; + Response response = builder.build(); + Whitebox.setInternalState(sse, "response", response); + + Iterator iterator; + try { + iterator = sse.iterator(); + } catch (RuntimeException e) { + assert e.getMessage().contains("Response body should not be null"); + } + builder.body(responseBody); + response = builder.build(); + Whitebox.setInternalState(sse, "response", response); + iterator = sse.iterator(); + + iterator.hasNext(); + iterator.remove(); + try { + iterator.next(); + }catch (Exception e){ + assert e.getMessage().contains("No line found"); + } + Whitebox.invokeMethod(sse, "close"); + } +} + +class SSEModelTest extends SSEResponseModel { + @Override + public String getRequestId() { + return ""; + } + + @Override + public void setRequestId(String requestId) { + } +} \ No newline at end of file diff --git a/src/test/java/com/tencentcloudapi/unit/common/SignTest.java b/src/test/java/com/tencentcloudapi/unit/common/SignTest.java new file mode 100644 index 000000000..1ca194a3a --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/common/SignTest.java @@ -0,0 +1,24 @@ +package com.tencentcloudapi.unit.common; + +import com.tencentcloudapi.common.Sign; +import com.tencentcloudapi.common.exception.TencentCloudSDKException; +import org.junit.Test; + +public class SignTest { + @Test + public void testSign() throws TencentCloudSDKException { + Sign sign = new Sign(); + try { + sign.sign("1", "1", "1"); + } catch (TencentCloudSDKException e) { + assert e.getMessage().contains("1 not available"); + } + } + + @Test + public void testSha256Hex() throws TencentCloudSDKException { + Sign sign = new Sign(); + String s = sign.sha256Hex("1"); + assert s.equals("6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b"); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/ChatCompletionsRequestTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/ChatCompletionsRequestTest.java new file mode 100644 index 000000000..333b46e32 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/ChatCompletionsRequestTest.java @@ -0,0 +1,50 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.ChatCompletionsRequest; +import org.junit.Test; + +import java.util.HashMap; + +public class ChatCompletionsRequestTest { + @Test + public void testChatCompletionsRequest() { + ChatCompletionsRequest request = new ChatCompletionsRequest(); + request.setModel("model"); + assert request.getModel().equals("model"); + + request.setMessages(new com.tencentcloudapi.hunyuan.v20230901.models.Message[]{new com.tencentcloudapi.hunyuan.v20230901.models.Message()}); + assert request.getMessages().length == 1; + + request.setStream(true); + assert request.getStream() == true; + + request.setStreamModeration(true); + assert request.getStreamModeration() == true; + + request.setTopP(1.0f); + assert request.getTopP() == 1.0f; + + request.setTemperature(1.0f); + assert request.getTemperature() == 1.0f; + + request.setEnableEnhancement(true); + assert request.getEnableEnhancement() == true; + + request.setTools(new com.tencentcloudapi.hunyuan.v20230901.models.Tool[]{new com.tencentcloudapi.hunyuan.v20230901.models.Tool()}); + assert request.getTools().length == 1; + + request.setToolChoice("toolChoice"); + assert request.getToolChoice().equals("toolChoice"); + + request.setCustomTool(new com.tencentcloudapi.hunyuan.v20230901.models.Tool()); + request.getCustomTool(); + + request.setSearchInfo(true); + assert request.getSearchInfo() == true; + + new ChatCompletionsRequest(request); + + HashMap map = new HashMap<>(); + request.toMap(map, ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/ChatCompletionsResponseTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/ChatCompletionsResponseTest.java new file mode 100644 index 000000000..a96829d02 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/ChatCompletionsResponseTest.java @@ -0,0 +1,45 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.ChatCompletionsResponse; +import com.tencentcloudapi.hunyuan.v20230901.models.Choice; +import com.tencentcloudapi.hunyuan.v20230901.models.ErrorMsg; +import org.junit.Test; + +import java.util.HashMap; + +public class ChatCompletionsResponseTest { + @Test + public void testChatCompletionsResponse() { + ChatCompletionsResponse resp = new ChatCompletionsResponse(); + resp.setCreated(1L); + assert resp.getCreated() == 1L; + + resp.setUsage(null); + assert resp.getUsage() == null; + + resp.setNote("note"); + assert resp.getNote() == "note"; + + resp.setId("id"); + assert resp.getId() == "id"; + + resp.setChoices(new Choice[]{new Choice()}); + assert resp.getChoices().length == 1; + + resp.setErrorMsg(null); + assert resp.getErrorMsg() == null; + + resp.setModerationLevel("moderationLevel"); + assert resp.getModerationLevel() == "moderationLevel"; + + resp.setSearchInfo(null); + assert resp.getSearchInfo() == null; + + resp.setRequestId("requestId"); + assert resp.getRequestId() == "requestId"; + + new ChatCompletionsResponse(resp); + + resp.toMap(new HashMap(), ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/ChoiceTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/ChoiceTest.java new file mode 100644 index 000000000..c0cbbbadf --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/ChoiceTest.java @@ -0,0 +1,23 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.Choice; +import com.tencentcloudapi.hunyuan.v20230901.models.Delta; +import com.tencentcloudapi.hunyuan.v20230901.models.Message; +import org.junit.Test; + +public class ChoiceTest { + @Test + public void testChoice() { + Choice choice = new Choice(); + choice.setFinishReason("finishReason"); + assert choice.getFinishReason().equals("finishReason"); + + choice.setDelta(new Delta()); + choice.getDelta(); + + choice.setMessage(new Message()); + choice.getMessage().setRole("role"); + + new Choice(choice); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/ContentTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/ContentTest.java new file mode 100644 index 000000000..4ddeb8deb --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/ContentTest.java @@ -0,0 +1,26 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.Content; +import com.tencentcloudapi.hunyuan.v20230901.models.ImageUrl; +import org.junit.Test; + +import java.util.HashMap; + +public class ContentTest { + @Test + public void testContent() { + Content content = new Content(); + content.setType("test"); + assert content.getType().equals("test"); + + content.setText("test"); + assert content.getText().equals("test"); + + content.setImageUrl(new ImageUrl()); + content.getImageUrl().setUrl("test"); + assert content.getImageUrl().getUrl().equals("test"); + + new Content(content); + content.toMap(new HashMap<>(), ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/DeltaTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/DeltaTest.java new file mode 100644 index 000000000..bfb8cca57 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/DeltaTest.java @@ -0,0 +1,25 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.Delta; +import com.tencentcloudapi.hunyuan.v20230901.models.ToolCall; +import org.junit.Test; + +import java.util.HashMap; + +public class DeltaTest { + @Test + public void testDelta() { + Delta delta = new Delta(); + delta.setRole("test"); + assert delta.getRole().equals("test"); + + delta.setContent("test"); + assert delta.getContent().equals("test"); + + delta.setToolCalls(new ToolCall[]{new ToolCall()}); + assert delta.getToolCalls().length == 1; + + new Delta(delta); + delta.toMap(new HashMap<>(), ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/EmbeddingDataTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/EmbeddingDataTest.java new file mode 100644 index 000000000..899031360 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/EmbeddingDataTest.java @@ -0,0 +1,24 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.EmbeddingData; +import org.junit.Test; + +import java.util.HashMap; + +public class EmbeddingDataTest { + @Test + public void testEmbeddingData() { + EmbeddingData embeddingData = new EmbeddingData(); + embeddingData.setEmbedding(new Float[]{1.0f, 2.0f, 3.0f}); + assert embeddingData.getEmbedding().length == 3; + + embeddingData.setIndex(1L); + assert embeddingData.getIndex() == 1L; + + embeddingData.setObject("test"); + assert embeddingData.getObject().equals("test"); + + new EmbeddingData(embeddingData); + embeddingData.toMap(new HashMap<>(), ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/EmbeddingUsageTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/EmbeddingUsageTest.java new file mode 100644 index 000000000..2d5471ae1 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/EmbeddingUsageTest.java @@ -0,0 +1,18 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.EmbeddingUsage; +import org.junit.Test; + +public class EmbeddingUsageTest { + @Test + public void testEmbeddingUsage() { + EmbeddingUsage usage = new EmbeddingUsage(); + usage.setPromptTokens(10L); + assert usage.getPromptTokens() == 10L; + + usage.setTotalTokens(100L); + assert usage.getTotalTokens() == 100L; + + new EmbeddingUsage(usage); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/ErrorMsgTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/ErrorMsgTest.java new file mode 100644 index 000000000..c74c9ddd7 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/ErrorMsgTest.java @@ -0,0 +1,21 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.ErrorMsg; +import org.junit.Test; + +import java.util.HashMap; + +public class ErrorMsgTest { + @Test + public void testErrorMsg() { + ErrorMsg errorMsg = new ErrorMsg(); + errorMsg.setMsg("msg"); + assert errorMsg.getMsg().equals("msg"); + + errorMsg.setCode(1L); + assert errorMsg.getCode() == 1L; + + new ErrorMsg(errorMsg); + errorMsg.toMap(new HashMap<>(), "prefix"); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/GetEmbeddingRequestTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/GetEmbeddingRequestTest.java new file mode 100644 index 000000000..93c1b8823 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/GetEmbeddingRequestTest.java @@ -0,0 +1,18 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.GetEmbeddingRequest; +import org.junit.Test; + +import java.util.HashMap; + +public class GetEmbeddingRequestTest { + @Test + public void testGetEmbeddingRequest() { + GetEmbeddingRequest request = new GetEmbeddingRequest(); + request.setInput("test"); + assert request.getInput().equals("test"); + + new GetEmbeddingRequest(request); + request.toMap(new HashMap<>(), ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/GetEmbeddingResponseTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/GetEmbeddingResponseTest.java new file mode 100644 index 000000000..5a0ca910f --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/GetEmbeddingResponseTest.java @@ -0,0 +1,26 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.EmbeddingData; +import com.tencentcloudapi.hunyuan.v20230901.models.EmbeddingUsage; +import com.tencentcloudapi.hunyuan.v20230901.models.GetEmbeddingResponse; +import org.junit.Test; + +import java.util.HashMap; + +public class GetEmbeddingResponseTest { + @Test + public void testGetEmbeddingResponse() { + GetEmbeddingResponse response = new GetEmbeddingResponse(); + response.setData(new EmbeddingData[]{}); + assert response.getData().length == 0; + + response.setUsage(new EmbeddingUsage()); + response.getUsage().setPromptTokens(1L); + + response.setRequestId("test"); + assert response.getRequestId().equals("test"); + + new GetEmbeddingResponse(response); + response.toMap(new HashMap<>(), ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/GetTokenCountRequestTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/GetTokenCountRequestTest.java new file mode 100644 index 000000000..11bf036b0 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/GetTokenCountRequestTest.java @@ -0,0 +1,18 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.GetTokenCountRequest; +import org.junit.Test; + +import java.util.HashMap; + +public class GetTokenCountRequestTest { + @Test + public void testGetTokenCountRequest() { + GetTokenCountRequest request = new GetTokenCountRequest(); + request.setPrompt("test"); + assert request.getPrompt().equals("test"); + + new GetTokenCountRequest(request); + request.toMap(new HashMap(), ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/GetTokenCountResponseTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/GetTokenCountResponseTest.java new file mode 100644 index 000000000..2213a8dc5 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/GetTokenCountResponseTest.java @@ -0,0 +1,30 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.GetTokenCountResponse; +import org.junit.Test; + +import java.util.HashMap; + +public class GetTokenCountResponseTest { + @Test + public void TestGetTokenCountResponse() { + GetTokenCountResponse response = new GetTokenCountResponse(); + response.setTokenCount(1L); + assert response.getTokenCount() == 1L; + + response.setTokenCount(2L); + assert response.getTokenCount() == 2L; + + response.setTokens(new String[]{"a", "b", "c"}); + assert response.getTokens().length == 3; + + response.setCharacterCount(1L); + assert response.getCharacterCount() == 1L; + + response.setRequestId("123"); + assert response.getRequestId().equals("123"); + + new GetTokenCountResponse(response); + response.toMap(new HashMap(), ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/LogoParamTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/LogoParamTest.java new file mode 100644 index 000000000..c82d9670a --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/LogoParamTest.java @@ -0,0 +1,22 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.LogoParam; +import com.tencentcloudapi.hunyuan.v20230901.models.LogoRect; +import org.junit.Test; + +public class LogoParamTest { + @Test + public void testLogoParam() { + LogoParam logoParam = new LogoParam(); + logoParam.setLogoUrl("https://hunyuan.com"); + assert logoParam.getLogoUrl().equals("https://hunyuan.com"); + + logoParam.setLogoImage("logo"); + assert logoParam.getLogoImage().equals("logo"); + + logoParam.setLogoRect(new LogoRect()); + logoParam.getLogoRect().setX(10L); + + new LogoParam(logoParam); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/LogoRectTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/LogoRectTest.java new file mode 100644 index 000000000..da361e0d3 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/LogoRectTest.java @@ -0,0 +1,27 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.LogoRect; +import org.junit.Test; + +import java.util.HashMap; + +public class LogoRectTest { + @Test + public void testLogoRect() { + LogoRect logoRect = new LogoRect(); + logoRect.setX(1L); + assert logoRect.getX() == 1L; + + logoRect.setY(1L); + assert logoRect.getY() == 1L; + + logoRect.setWidth(2L); + assert logoRect.getWidth() == 2L; + + logoRect.setHeight(2L); + assert logoRect.getHeight() == 2L; + + new LogoRect(logoRect); + logoRect.toMap(new HashMap<>(), ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/MessageTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/MessageTest.java new file mode 100644 index 000000000..3a35edf7d --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/MessageTest.java @@ -0,0 +1,29 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.Content; +import com.tencentcloudapi.hunyuan.v20230901.models.Message; +import com.tencentcloudapi.hunyuan.v20230901.models.ToolCall; +import org.junit.Test; + +public class MessageTest { + @Test + public void testMessage() { + Message message = new Message(); + message.setRole("role"); + assert message.getRole().equals("role"); + + message.setContent("content"); + assert message.getContent().equals("content"); + + message.setContents(new Content[]{new Content()}); + assert message.getContents().length == 1; + + message.setToolCallId("toolCallId"); + assert message.getToolCallId().equals("toolCallId"); + + message.setToolCalls(new ToolCall[]{new ToolCall()}); + assert message.getToolCalls().length == 1; + + new Message(message); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/QueryHunyuanImageJobRequestTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/QueryHunyuanImageJobRequestTest.java new file mode 100644 index 000000000..b59880b28 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/QueryHunyuanImageJobRequestTest.java @@ -0,0 +1,18 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.QueryHunyuanImageJobRequest; +import org.junit.Test; + +import java.util.HashMap; + +public class QueryHunyuanImageJobRequestTest { + @Test + public void testQueryHunyuanImageJobRequest() { + QueryHunyuanImageJobRequest request = new QueryHunyuanImageJobRequest(); + request.setJobId("jobId"); + assert request.getJobId().equals("jobId"); + + new QueryHunyuanImageJobRequest(request); + request.toMap(new HashMap<>(), ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/QueryHunyuanImageJobResponseTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/QueryHunyuanImageJobResponseTest.java new file mode 100644 index 000000000..391abd7b1 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/QueryHunyuanImageJobResponseTest.java @@ -0,0 +1,39 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.QueryHunyuanImageJobResponse; +import org.junit.Test; + +import java.util.HashMap; + +public class QueryHunyuanImageJobResponseTest { + @Test + public void testQueryHunyuanImageJobResponse() { + QueryHunyuanImageJobResponse response = new QueryHunyuanImageJobResponse(); + response.setJobStatusCode("1"); + assert response.getJobStatusCode().equals("1"); + + response.setJobStatusMsg("排队中"); + assert response.getJobStatusMsg().equals("排队中"); + + response.setJobErrorCode("1"); + assert response.getJobErrorCode().equals("1"); + + response.setJobErrorMsg("处理失败"); + assert response.getJobErrorMsg().equals("处理失败"); + + response.setResultImage(new String[]{"1"}); + assert response.getResultImage()[0].equals("1"); + + response.setResultDetails(new String[]{"1"}); + assert response.getResultDetails()[0].equals("1"); + + response.setRevisedPrompt(new String[]{"1"}); + assert response.getRevisedPrompt()[0].equals("1"); + + response.setRequestId("requestId"); + assert response.getRequestId().equals("requestId"); + + new QueryHunyuanImageJobResponse(response); + response.toMap(new HashMap<>(), ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/SearchInfoTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/SearchInfoTest.java new file mode 100644 index 000000000..17caf361f --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/SearchInfoTest.java @@ -0,0 +1,19 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.SearchInfo; +import com.tencentcloudapi.hunyuan.v20230901.models.SearchResult; +import org.junit.Test; + +import java.util.HashMap; + +public class SearchInfoTest { + @Test + public void testSearchInfo() { + SearchInfo searchInfo = new SearchInfo(); + searchInfo.setSearchResults(new SearchResult[]{new SearchResult(), new SearchResult()}); + assert searchInfo.getSearchResults().length == 2; + + new SearchInfo(searchInfo); + searchInfo.toMap(new HashMap<>(), ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/SearchResultTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/SearchResultTest.java new file mode 100644 index 000000000..94b6f5641 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/SearchResultTest.java @@ -0,0 +1,24 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.SearchResult; +import org.junit.Test; + +import java.util.HashMap; + +public class SearchResultTest { + @Test + public void testSearchResult() { + SearchResult searchResult = new SearchResult(); + searchResult.setIndex(1L); + assert searchResult.getIndex() == 1L; + + searchResult.setTitle("title"); + assert searchResult.getTitle().equals("title"); + + searchResult.setUrl("url"); + assert searchResult.getUrl().equals("url"); + + new SearchResult(searchResult); + searchResult.toMap(new HashMap<>(), "prefix"); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/SubmitHunyuanImageJobRequestTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/SubmitHunyuanImageJobRequestTest.java new file mode 100644 index 000000000..df736f5a3 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/SubmitHunyuanImageJobRequestTest.java @@ -0,0 +1,40 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.LogoParam; +import com.tencentcloudapi.hunyuan.v20230901.models.SubmitHunyuanImageJobRequest; +import org.junit.Test; + +import java.util.HashMap; + +public class SubmitHunyuanImageJobRequestTest { + @Test + public void testBuildRequest() { + SubmitHunyuanImageJobRequest request = new SubmitHunyuanImageJobRequest(); + request.setPrompt("test"); + assert request.getPrompt().equals("test"); + + request.setStyle("test"); + assert request.getStyle().equals("test"); + + request.setResolution("768:768(1:1)"); + assert request.getResolution().equals("768:768(1:1)"); + + request.setNum(1L); + assert request.getNum() == 1L; + + request.setSeed(9L); + assert request.getSeed() == 9L; + + request.setRevise(1L); + assert request.getRevise() == 1L; + + request.setLogoAdd(1L); + assert request.getLogoAdd() == 1L; + + request.setLogoParam(new LogoParam()); + request.getLogoParam().setLogoUrl("test"); + + new SubmitHunyuanImageJobRequest(request); + request.toMap(new HashMap(), ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/SubmitHunyuanImageJobResponseTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/SubmitHunyuanImageJobResponseTest.java new file mode 100644 index 000000000..3f931ff77 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/SubmitHunyuanImageJobResponseTest.java @@ -0,0 +1,20 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.SubmitHunyuanImageJobResponse; +import org.junit.Test; + +import java.util.HashMap; + +public class SubmitHunyuanImageJobResponseTest { + @Test + public void testSubmitHunyuanImageJobResponse() { + SubmitHunyuanImageJobResponse response = new SubmitHunyuanImageJobResponse(); + response.setRequestId("requestId"); + assert response.getRequestId().equals("requestId"); + + response.setJobId("jobId"); + assert response.getJobId().equals("jobId"); + new SubmitHunyuanImageJobResponse(response); + response.toMap(new HashMap<>(), "prefix"); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/TextToImageLiteRequestTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/TextToImageLiteRequestTest.java new file mode 100644 index 000000000..069cf66bc --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/TextToImageLiteRequestTest.java @@ -0,0 +1,37 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.LogoParam; +import com.tencentcloudapi.hunyuan.v20230901.models.TextToImageLiteRequest; +import org.junit.Test; + +import java.util.HashMap; + +public class TextToImageLiteRequestTest { + @Test + public void testTextToImageLiteRequest() { + TextToImageLiteRequest request = new TextToImageLiteRequest(); + request.setPrompt("test"); + assert request.getPrompt().equals("test"); + + request.setNegativePrompt("test"); + assert request.getNegativePrompt().equals("test"); + + request.setStyle("style"); + assert request.getStyle().equals("style"); + + request.setResolution("768:1024(3:4)"); + assert request.getResolution().equals("768:1024(3:4)"); + + request.setLogoAdd(1L); + assert request.getLogoAdd() == 1L; + + request.setLogoParam(new LogoParam()); + request.getLogoParam(); + + request.setRspImgType("png"); + assert request.getRspImgType().equals("png"); + + new TextToImageLiteRequest(request); + request.toMap(new HashMap<>(), ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/TextToImageLiteResponseTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/TextToImageLiteResponseTest.java new file mode 100644 index 000000000..219ae63f8 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/TextToImageLiteResponseTest.java @@ -0,0 +1,21 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.TextToImageLiteResponse; +import org.junit.Test; + +import java.util.HashMap; + +public class TextToImageLiteResponseTest { + @Test + public void testTextToImageLiteResponse() { + TextToImageLiteResponse response = new TextToImageLiteResponse(); + response.setRequestId("requestId"); + assert response.getRequestId().equals("requestId"); + + response.setResultImage("resultImage"); + assert response.getResultImage().equals("resultImage"); + + new TextToImageLiteResponse(response); + response.toMap(new HashMap<>(), ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/ToolCallFunctionTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/ToolCallFunctionTest.java new file mode 100644 index 000000000..4711190f1 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/ToolCallFunctionTest.java @@ -0,0 +1,21 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.ToolCallFunction; +import org.junit.Test; + +import java.util.HashMap; + +public class ToolCallFunctionTest { + @Test + public void testToolCallFunction() { + ToolCallFunction toolCallFunction = new ToolCallFunction(); + toolCallFunction.setName("test"); + assert toolCallFunction.getName().equals("test"); + + toolCallFunction.setArguments("test"); + assert toolCallFunction.getArguments().equals("test"); + + new ToolCallFunction(toolCallFunction); + toolCallFunction.toMap(new HashMap<>(), ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/ToolCallTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/ToolCallTest.java new file mode 100644 index 000000000..7c343c3bd --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/ToolCallTest.java @@ -0,0 +1,25 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.ToolCall; +import com.tencentcloudapi.hunyuan.v20230901.models.ToolCallFunction; +import org.junit.Test; + +import java.util.HashMap; + +public class ToolCallTest { + @Test + public void testToolCall() { + ToolCall toolCall = new ToolCall(); + toolCall.setId("111"); + assert toolCall.getId().equals("111"); + + toolCall.setType("test"); + assert toolCall.getType().equals("test"); + + toolCall.setFunction(new ToolCallFunction()); + toolCall.getFunction().setName("test"); + + new ToolCall(toolCall); + toolCall.toMap(new HashMap<>(), ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/ToolFunctionTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/ToolFunctionTest.java new file mode 100644 index 000000000..9a9885339 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/ToolFunctionTest.java @@ -0,0 +1,24 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.ToolFunction; +import org.junit.Test; + +import java.util.HashMap; + +public class ToolFunctionTest { + @Test + public void testToolFunction() { + ToolFunction toolFunction = new ToolFunction(); + toolFunction.setName("test"); + assert toolFunction.getName().equals("test"); + + toolFunction.setParameters("test"); + assert toolFunction.getParameters().equals("test"); + + toolFunction.setDescription("test"); + assert toolFunction.getDescription().equals("test"); + + new ToolFunction(toolFunction); + toolFunction.toMap(new HashMap<>(), ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/ToolTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/ToolTest.java new file mode 100644 index 000000000..a26e0ca8a --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/ToolTest.java @@ -0,0 +1,19 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.Tool; +import com.tencentcloudapi.hunyuan.v20230901.models.ToolFunction; +import org.junit.Test; + +public class ToolTest { + @Test + public void testTool() { + Tool tool = new Tool(); + tool.setType("type"); + assert tool.getType().equals("type"); + + tool.setFunction(new ToolFunction()); + tool.getFunction().setName("name"); + + new Tool(tool); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuan/UsageTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuan/UsageTest.java new file mode 100644 index 000000000..2622934d8 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuan/UsageTest.java @@ -0,0 +1,24 @@ +package com.tencentcloudapi.unit.hunyuan; + +import com.tencentcloudapi.hunyuan.v20230901.models.Usage; +import org.junit.Test; + +import java.util.HashMap; + +public class UsageTest { + @Test + public void testUsage() { + Usage usage = new Usage(); + usage.setPromptTokens(1L); + assert usage.getPromptTokens() == 1L; + + usage.setCompletionTokens(2L); + assert usage.getCompletionTokens() == 2L; + + usage.setTotalTokens(3L); + assert usage.getTotalTokens() == 3L; + + new Usage(usage); + usage.toMap(new HashMap<>(), ""); + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuanclient/HunyuanClientTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuanclient/HunyuanClientTest.java new file mode 100644 index 000000000..bd8a8a06b --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuanclient/HunyuanClientTest.java @@ -0,0 +1,93 @@ +package com.tencentcloudapi.unit.hunyuanclient; + +import com.tencentcloudapi.common.AbstractModel; +import com.tencentcloudapi.common.Credential; +import com.tencentcloudapi.common.exception.TencentCloudSDKException; +import com.tencentcloudapi.common.profile.ClientProfile; +import com.tencentcloudapi.common.profile.HttpProfile; +import com.tencentcloudapi.hunyuan.v20230901.HunyuanClient; +import com.tencentcloudapi.hunyuan.v20230901.models.*; +import org.junit.Test; +import org.powermock.reflect.Whitebox; + + +public class HunyuanClientTest { + @Test + public void testChatCompletions() throws TencentCloudSDKException { + Credential cred = + new Credential( + System.getenv("TENCENTCLOUD_SECRET_ID"), System.getenv("TENCENTCLOUD_SECRET_KEY")); + HttpProfile httpProfile = new HttpProfile(); + ClientProfile clientProfile = new ClientProfile(); + clientProfile.setHttpProfile(httpProfile); + HunyuanClient client = new HunyuanClient(cred, "ap-guangzhou", clientProfile); + + ChatCompletionsRequest req = new ChatCompletionsRequest(); + Whitebox.setInternalState(req, "Model", "hunyuan-pro"); + Message message = new Message(); + Whitebox.setInternalState(message, "Role", "user"); + Whitebox.setInternalState(message, "Content", "hello"); + Message[] messages = new Message[1]; + messages[0] = message; + Whitebox.setInternalState(req, "Messages", messages); + + ChatCompletionsResponse response = client.ChatCompletions(req); + assert response != null; + } + + @Test + public void testGetEmbedding() throws TencentCloudSDKException { + Credential cred = + new Credential( + System.getenv("TENCENTCLOUD_SECRET_ID"), System.getenv("TENCENTCLOUD_SECRET_KEY")); + HunyuanClient client = new HunyuanClient(cred, "ap-guangzhou"); + + GetEmbeddingRequest req = new GetEmbeddingRequest(); + req.setInput("hello"); + + GetEmbeddingResponse response = client.GetEmbedding(req); + assert response != null; + } + + @Test + public void testGetTokenCount() throws TencentCloudSDKException { + Credential cred = + new Credential( + System.getenv("TENCENTCLOUD_SECRET_ID"), System.getenv("TENCENTCLOUD_SECRET_KEY")); + HunyuanClient client = new HunyuanClient(cred, "ap-guangzhou"); + + GetTokenCountRequest req = new GetTokenCountRequest(); + req.setPrompt("hello"); + + GetTokenCountResponse response = client.GetTokenCount(req); + assert response != null; + } + + @Test + public void testQueryHunyuanImageJobResponse() throws TencentCloudSDKException { + Credential cred = + new Credential( + System.getenv("TENCENTCLOUD_SECRET_ID"), System.getenv("TENCENTCLOUD_SECRET_KEY")); + HunyuanClient client = new HunyuanClient(cred, "ap-guangzhou"); + + SubmitHunyuanImageJobRequest req = new SubmitHunyuanImageJobRequest(); + req.setPrompt("snow and fire"); + + SubmitHunyuanImageJobResponse response = client.SubmitHunyuanImageJob(req); + assert response != null; + } + + @Test + public void testTextToImageLite() throws TencentCloudSDKException { + Credential cred = + new Credential( + System.getenv("TENCENTCLOUD_SECRET_ID"), System.getenv("TENCENTCLOUD_SECRET_KEY")); + HunyuanClient client = new HunyuanClient(cred, "ap-guangzhou"); + + TextToImageLiteRequest req = new TextToImageLiteRequest(); + req.setPrompt("mountains and rivers"); + + TextToImageLiteResponse response = client.TextToImageLite(req); + assert response != null; + } +} diff --git a/src/test/java/com/tencentcloudapi/unit/hunyuanclient/HunyuanErrorCodeTest.java b/src/test/java/com/tencentcloudapi/unit/hunyuanclient/HunyuanErrorCodeTest.java new file mode 100644 index 000000000..6ed8d5bc2 --- /dev/null +++ b/src/test/java/com/tencentcloudapi/unit/hunyuanclient/HunyuanErrorCodeTest.java @@ -0,0 +1,31 @@ +package com.tencentcloudapi.unit.hunyuanclient; + +import com.tencentcloudapi.hunyuan.v20230901.HunyuanErrorCode; +import org.junit.Test; +import org.powermock.reflect.Whitebox; + +import static org.junit.Assert.assertEquals; + +public class HunyuanErrorCodeTest { + @Test + public void testErrorCode() throws Exception { + assertEquals("FailedOperation", HunyuanErrorCode.FAILEDOPERATION.getValue()); + assertEquals("FailedOperation.EngineRequestTimeout", HunyuanErrorCode.FAILEDOPERATION_ENGINEREQUESTTIMEOUT.getValue()); + assertEquals("FailedOperation.EngineServerError", HunyuanErrorCode.FAILEDOPERATION_ENGINESERVERERROR.getValue()); + assertEquals("FailedOperation.EngineServerLimitExceeded",HunyuanErrorCode.FAILEDOPERATION_ENGINESERVERLIMITEXCEEDED.getValue()); + assertEquals("FailedOperation.FreeResourcePackExhausted", HunyuanErrorCode.FAILEDOPERATION_FREERESOURCEPACKEXHAUSTED.getValue()); + assertEquals("FailedOperation.ImageDecodeFailed", HunyuanErrorCode.FAILEDOPERATION_IMAGEDECODEFAILED.getValue()); + assertEquals("FailedOperation.ImageDownloadError", HunyuanErrorCode.FAILEDOPERATION_IMAGEDOWNLOADERROR.getValue()); + assertEquals("FailedOperation.ResourcePackExhausted", HunyuanErrorCode.FAILEDOPERATION_RESOURCEPACKEXHAUSTED.getValue()); + assertEquals("FailedOperation.ServiceNotActivated", HunyuanErrorCode.FAILEDOPERATION_SERVICENOTACTIVATED.getValue()); + assertEquals("FailedOperation.ServiceStop", HunyuanErrorCode.FAILEDOPERATION_SERVICESTOP.getValue()); + assertEquals("FailedOperation.ServiceStopArrears", HunyuanErrorCode.FAILEDOPERATION_SERVICESTOPARREARS.getValue()); + assertEquals("InternalError", HunyuanErrorCode.INTERNALERROR.getValue()); + assertEquals("InvalidParameter", HunyuanErrorCode.INVALIDPARAMETER.getValue()); + assertEquals("InvalidParameterValue", HunyuanErrorCode.INVALIDPARAMETERVALUE.getValue()); + assertEquals("InvalidParameterValue.Model", HunyuanErrorCode.INVALIDPARAMETERVALUE_MODEL.getValue()); + assertEquals("InvalidParameterValue.ParameterValueError", HunyuanErrorCode.INVALIDPARAMETERVALUE_PARAMETERVALUEERROR.getValue()); + assertEquals("LimitExceeded", HunyuanErrorCode.LIMITEXCEEDED.getValue()); + assertEquals("OperationDenied.ImageIllegalDetected", HunyuanErrorCode.OPERATIONDENIED_IMAGEILLEGALDETECTED.getValue()); + } +}