Skip to content

Commit ac206fd

Browse files
authored
Merge pull request #14 from Huijing-Xu/test-app-mesh
test: add unit test in app mesh
2 parents f77152f + 8b6a253 commit ac206fd

File tree

7 files changed

+241
-5
lines changed

7 files changed

+241
-5
lines changed

capa-spi-aws-mesh/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@
5757
<artifactId>junit-jupiter-engine</artifactId>
5858
<scope>test</scope>
5959
</dependency>
60+
<dependency>
61+
<groupId>org.mockito</groupId>
62+
<artifactId>mockito-core</artifactId>
63+
<scope>test</scope>
64+
</dependency>
6065
</dependencies>
6166

6267
<build>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package group.rxcloud.capa.spi.aws.mesh.config;
18+
19+
import group.rxcloud.capa.infrastructure.env.CapaEnvironment;
20+
import org.junit.jupiter.api.Assertions;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
24+
public class AwsRpcServiceOptionsTest {
25+
26+
private AwsRpcServiceOptions awsRpcServiceOptions;
27+
28+
private AwsRpcServiceOptions.AwsToAwsServiceOptions awsToAwsServiceOptions;
29+
30+
@BeforeEach
31+
public void setUp() {
32+
awsRpcServiceOptions = new AwsRpcServiceOptions("appId",
33+
AwsRpcServiceOptions.ServiceRpcInvokeMode.AWS_TO_AWS);
34+
35+
awsToAwsServiceOptions = new AwsRpcServiceOptions.AwsToAwsServiceOptions(
36+
"appId",
37+
8080,
38+
CapaEnvironment.DeployVpcEnvironment.FWS);
39+
awsRpcServiceOptions.setAwsToAwsServiceOptions(awsToAwsServiceOptions);
40+
}
41+
42+
@Test
43+
public void testGetAppId_Success() {
44+
String appId = awsRpcServiceOptions.getAppId();
45+
Assertions.assertEquals("appId", appId);
46+
}
47+
48+
@Test
49+
public void testGetRpcInvokeMode_Success() {
50+
AwsRpcServiceOptions.ServiceRpcInvokeMode rpcInvokeMode = awsRpcServiceOptions.getRpcInvokeMode();
51+
Assertions.assertEquals(AwsRpcServiceOptions.ServiceRpcInvokeMode.AWS_TO_AWS, rpcInvokeMode);
52+
}
53+
54+
@Test
55+
public void testGetAwsToAwsServiceOptions_Success() {
56+
AwsRpcServiceOptions.AwsToAwsServiceOptions awsToAwsServiceOptions
57+
= awsRpcServiceOptions.getAwsToAwsServiceOptions();
58+
59+
Assertions.assertEquals("appId", awsToAwsServiceOptions.getServiceId());
60+
Assertions.assertEquals(8080, awsToAwsServiceOptions.getServicePort());
61+
Assertions.assertEquals(CapaEnvironment.DeployVpcEnvironment.FWS, awsToAwsServiceOptions.getServiceEnv());
62+
}
63+
64+
@Test
65+
public void testEquals_SuccessWhenTrue() {
66+
boolean result = awsRpcServiceOptions.equals(awsRpcServiceOptions);
67+
Assertions.assertTrue(result);
68+
69+
AwsRpcServiceOptions newServiceOptions = new AwsRpcServiceOptions("appId",
70+
AwsRpcServiceOptions.ServiceRpcInvokeMode.AWS_TO_AWS);
71+
newServiceOptions.setAwsToAwsServiceOptions(awsToAwsServiceOptions);
72+
73+
boolean newResult = awsRpcServiceOptions.equals(newServiceOptions);
74+
Assertions.assertTrue(newResult);
75+
}
76+
77+
@Test
78+
public void testEquals_SuccessWhenFalse() {
79+
boolean result = awsRpcServiceOptions.equals(new AwsSpiOptionsLoader());
80+
Assertions.assertFalse(result);
81+
82+
AwsRpcServiceOptions newServiceOptions = new AwsRpcServiceOptions("appId",
83+
AwsRpcServiceOptions.ServiceRpcInvokeMode.AWS_TO_AWS);
84+
boolean newResult = awsRpcServiceOptions.equals(newServiceOptions);
85+
Assertions.assertFalse(newResult);
86+
}
87+
88+
@Test
89+
public void testHashCode_Success() {
90+
int hashCode = awsRpcServiceOptions.hashCode();
91+
Assertions.assertNotNull(hashCode);
92+
}
93+
94+
@Test
95+
public void testToString_Success() {
96+
String toString = awsRpcServiceOptions.toString();
97+
Assertions.assertNotNull(toString);
98+
}
99+
}

capa-spi-aws-mesh/src/test/java/group/rxcloud/capa/spi/aws/mesh/env/AwsRpcEnvironmentTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package group.rxcloud.capa.spi.aws.mesh.env;
1818

19-
2019
import org.junit.jupiter.api.Assertions;
2120
import org.junit.jupiter.api.Test;
2221

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package group.rxcloud.capa.spi.aws.mesh.http;
18+
19+
import group.rxcloud.capa.component.http.HttpResponse;
20+
import group.rxcloud.capa.infrastructure.exceptions.CapaException;
21+
import group.rxcloud.capa.spi.aws.mesh.config.AwsRpcServiceOptions;
22+
import group.rxcloud.capa.spi.aws.mesh.config.AwsSpiOptionsLoader;
23+
import group.rxcloud.capa.spi.aws.mesh.http.serializer.BaijiSSJsonObjectSerializer;
24+
import group.rxcloud.cloudruntimes.utils.TypeRef;
25+
import okhttp3.OkHttpClient;
26+
import org.junit.jupiter.api.Assertions;
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Test;
29+
30+
import java.util.HashMap;
31+
import java.util.concurrent.CompletableFuture;
32+
33+
public class AwsCapaHttpTest {
34+
35+
private OkHttpClient okHttpClient;
36+
37+
private BaijiSSJsonObjectSerializer baijiSSJsonObjectSerializer;
38+
39+
private AwsCapaHttp awsCapaHttp;
40+
41+
@BeforeEach
42+
public void setUp() {
43+
okHttpClient = new OkHttpClient.Builder().build();
44+
45+
baijiSSJsonObjectSerializer = new BaijiSSJsonObjectSerializer();
46+
47+
awsCapaHttp = new AwsCapaHttp(okHttpClient, baijiSSJsonObjectSerializer);
48+
}
49+
50+
@Test
51+
public void testInvokeSpiApi_Success() {
52+
AwsSpiOptionsLoader awsSpiOptionsLoader = new AwsSpiOptionsLoader();
53+
AwsRpcServiceOptions awsRpcServiceOptions = awsSpiOptionsLoader.loadRpcServiceOptions("appId");
54+
55+
CompletableFuture<HttpResponse<String>> responseCompletableFuture = awsCapaHttp.invokeSpiApi("appId",
56+
"method",
57+
"requestData",
58+
new HashMap<>(),
59+
TypeRef.STRING,
60+
awsRpcServiceOptions);
61+
62+
responseCompletableFuture.cancel(true);
63+
}
64+
65+
@Test
66+
public void testInvokeSpiApi_FailWhenServiceIdIsNull() {
67+
AwsSpiOptionsLoader awsSpiOptionsLoader = new AwsSpiOptionsLoader();
68+
AwsRpcServiceOptions awsRpcServiceOptions = awsSpiOptionsLoader.loadRpcServiceOptions("");
69+
70+
Assertions.assertThrows(CapaException.class, () -> {
71+
awsCapaHttp.invokeSpiApi("appId",
72+
"method",
73+
"requestData",
74+
new HashMap<>(),
75+
TypeRef.STRING,
76+
awsRpcServiceOptions);
77+
});
78+
}
79+
}

capa-spi-aws-mesh/src/test/java/group/rxcloud/capa/spi/aws/mesh/http/AwsCapaSerialzerProviderTest.java renamed to capa-spi-aws-mesh/src/test/java/group/rxcloud/capa/spi/aws/mesh/http/serializer/AwsCapaSerializerProviderTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package group.rxcloud.capa.spi.aws.mesh.http;
18-
17+
package group.rxcloud.capa.spi.aws.mesh.http.serializer;
1918

2019
import group.rxcloud.capa.infrastructure.serializer.CapaObjectSerializer;
21-
import group.rxcloud.capa.spi.aws.mesh.http.serializer.AwsCapaSerializerProvider;
2220
import org.junit.jupiter.api.Assertions;
2321
import org.junit.jupiter.api.Test;
2422

25-
public class AwsCapaSerialzerProviderTest {
23+
public class AwsCapaSerializerProviderTest {
2624

2725
@Test
2826
public void testGetSerializerOrDefault_Success() {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package group.rxcloud.capa.spi.aws.mesh.http.serializer;
18+
19+
import group.rxcloud.cloudruntimes.utils.TypeRef;
20+
import org.junit.jupiter.api.Assertions;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.io.IOException;
25+
26+
public class BaijiSSJsonObjectSerializerTest {
27+
28+
private BaijiSSJsonObjectSerializer baijiSSJsonObjectSerializer;
29+
30+
@BeforeEach
31+
public void setUp() {
32+
baijiSSJsonObjectSerializer = new BaijiSSJsonObjectSerializer();
33+
}
34+
35+
@Test
36+
public void testSerializeAndDeserialize_Success() throws IOException {
37+
byte[] serializes = baijiSSJsonObjectSerializer.serialize("serialize");
38+
39+
String deserialize = baijiSSJsonObjectSerializer.deserialize(serializes, TypeRef.STRING);
40+
41+
Assertions.assertEquals("serialize", deserialize);
42+
}
43+
44+
@Test
45+
public void testGetContentType_Success() {
46+
String contentType = baijiSSJsonObjectSerializer.getContentType();
47+
Assertions.assertEquals("application/bjjson", contentType);
48+
}
49+
}

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<capa.version>1.0.4.RELEASE</capa.version>
7474
<aws-sdk.version>2.17.40</aws-sdk.version>
7575
<junit.version>5.3.1</junit.version>
76+
<mockito-core.version>3.6.0</mockito-core.version>
7677
<checkstyle.version>3.1.2</checkstyle.version>
7778
<checkstyle.skip>false</checkstyle.skip>
7879
<apache.rat.version>0.13</apache.rat.version>
@@ -130,6 +131,12 @@
130131
<version>${junit.version}</version>
131132
<scope>test</scope>
132133
</dependency>
134+
<dependency>
135+
<groupId>org.mockito</groupId>
136+
<artifactId>mockito-core</artifactId>
137+
<version>${mockito-core.version}</version>
138+
<scope>test</scope>
139+
</dependency>
133140
</dependencies>
134141
</dependencyManagement>
135142

0 commit comments

Comments
 (0)