Skip to content

Commit

Permalink
optimize: increase seata-core module unit test coverage (apache#6250)
Browse files Browse the repository at this point in the history
  • Loading branch information
lightClouds917 committed Jan 11, 2024
1 parent cd5d816 commit 77bfc08
Show file tree
Hide file tree
Showing 15 changed files with 858 additions and 2 deletions.
2 changes: 1 addition & 1 deletion changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6081](https://github.com/apache/incubator-seata/pull/6081)] add `test-os.yml` for testing the OS
- [[#6125](https://github.com/apache/incubator-seata/pull/6125)] unbind xid in TransactionTemplateTest
- [[#6157](https://github.com/apache/incubator-seata/pull/6157)] increase common module unit test coverage
- [[#6250](https://github.com/apache/incubator-seata/pull/6250)] increase seata-core module unit test coverage

Thanks to these contributors for their code commits. Please report an unintended omission.

Expand All @@ -91,5 +92,4 @@ Thanks to these contributors for their code commits. Please report an unintended
- [jsbxyyx](https://github.com/jsbxyyx)
- [liuqiufeng](https://github.com/liuqiufeng)


Also, we receive many valuable issues, questions and advices from our community. Thanks for you all.
2 changes: 1 addition & 1 deletion changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
- [[#6081](https://github.com/apache/incubator-seata/pull/6081)] 添加 `test-os.yml` 用于测试seata在各种操作系统下的运行情况
- [[#6125](https://github.com/apache/incubator-seata/pull/6125)] TransactionTemplateTest单测unbind xid
- [[#6157](https://github.com/apache/incubator-seata/pull/6157)] 增加common模块单测覆盖率
- [[#6250](https://github.com/apache/incubator-seata/pull/6250)] 增加seata-core模块单测覆盖率

非常感谢以下 contributors 的代码贡献。若有无意遗漏,请报告。

Expand All @@ -91,5 +92,4 @@
- [jsbxyyx](https://github.com/jsbxyyx)
- [liuqiufeng](https://github.com/liuqiufeng)


同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。
54 changes: 54 additions & 0 deletions core/src/test/java/io/seata/core/auth/DefaultAuthSignerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.seata.core.auth;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* The DefaultAuthSigner Test
*/
public class DefaultAuthSignerTest {
@Test
public void testGetRamSignNotNull() {
String data = "testGroup,127.0.0.1,1702564471650";
String key = "exampleEncryptKey";
String expectedSign = "6g9nMk6BRLFxl7bf5ZfWaEZvGdho3JBmwvx5rqgSUCE=";
DefaultAuthSigner signer = new DefaultAuthSigner();
String sign = signer.sign(data, key);
Assertions.assertEquals(expectedSign, sign);
}

@Test
public void testGetRamSignNull() {
String data = null;
String key = "exampleEncryptKey";
DefaultAuthSigner signer = new DefaultAuthSigner();
String sign = signer.sign(data, key);
Assertions.assertNull(sign);
}

@Test
public void testGetSignVersion() {
DefaultAuthSigner signer = new DefaultAuthSigner();
String expectedVersion = "V4";
String actualVersion = signer.getSignVersion();

// Assert the returned version matches the expected version
Assertions.assertEquals(expectedVersion, actualVersion);
}
}
88 changes: 88 additions & 0 deletions core/src/test/java/io/seata/core/auth/RamSignAdapterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.seata.core.auth;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* The RamSignAdapter Test
*/
public class RamSignAdapterTest {
@Test
public void testGetDateSigningKey() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
String secret = "mySecret";
String date = "20220101";
String signMethod = "HmacSHA256";
byte[] expectArray = new byte[]{-96, 108, 42, 75, -59, 121, -63, 108, -3, -126, 67, 3, 118, 2, 39, 59, -68, -37, -98, 122, -25, -120, 77, 56, -70, 24, -115, 33, 125, -128, -10, -26};

RamSignAdapter adapter = new RamSignAdapter();
// Use reflection to access the private method
Method getDateSigningKeyMethod = RamSignAdapter.class.getDeclaredMethod("getDateSigningKey", String.class, String.class, String.class);
getDateSigningKeyMethod.setAccessible(true);
byte[] signingKey = (byte[]) getDateSigningKeyMethod.invoke(adapter, secret, date, signMethod);
Assertions.assertEquals(32, signingKey.length);
Assertions.assertArrayEquals(expectArray, signingKey);
}

@Test
public void testGetRegionSigningKey() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
String secret = "mySecret";
String date = "20220101";
String region = "cn-beijing";
String signMethod = "HmacSHA256";
byte[] expectArray = new byte[]{-40, 5, 2, 41, -48, 82, 10, -102, 125, -24, -44, -83, 127, 6, -85, 93, -26, 88, -88, 65, 56, 79, -5, -66, 65, -106, 19, -64, -85, 103, -32, 110};

RamSignAdapter adapter = new RamSignAdapter();
// Use reflection to access the private method
Method getRegionSigningKeyMethod = RamSignAdapter.class.getDeclaredMethod("getRegionSigningKey", String.class, String.class, String.class, String.class);
getRegionSigningKeyMethod.setAccessible(true);
byte[] signingKey = (byte[]) getRegionSigningKeyMethod.invoke(adapter, secret, date, region, signMethod);
Assertions.assertEquals(32, signingKey.length);
Assertions.assertArrayEquals(expectArray, signingKey);
}

@Test
public void testGetProductSigningKey() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
String secret = "mySecret";
String date = "20220101";
String region = "cn-beijing";
String productCode = "seata";
String signMethod = "HmacSHA256";
byte[] expectArray = new byte[]{62, 98, -65, 30, -8, -3, 66, -111, 0, 123, 126, 78, -30, -74, 55, -79, 101, -18, -97, -5, 78, -19, -17, 0, 88, 30, -92, 108, 103, 87, 49, -22};

RamSignAdapter adapter = new RamSignAdapter();
Method getProductSigningKeyMethod = RamSignAdapter.class.getDeclaredMethod("getProductSigningKey", String.class, String.class, String.class, String.class, String.class);
getProductSigningKeyMethod.setAccessible(true);
byte[] signingKey = (byte[]) getProductSigningKeyMethod.invoke(adapter, secret, date, region, productCode, signMethod);
Assertions.assertEquals(32, signingKey.length);
Assertions.assertArrayEquals(expectArray, signingKey);
}

@Test
public void testGetRamSign() {
String encryptText = "testGroup,127.0.0.1,1702564471650";
String encryptKey = "exampleEncryptKey";
String expectedSign = "6g9nMk6BRLFxl7bf5ZfWaEZvGdho3JBmwvx5rqgSUCE=";
String actualSign = RamSignAdapter.getRamSign(encryptText, encryptKey);
// Assert the generated sign matches the expected sign
Assertions.assertEquals(expectedSign, actualSign);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.seata.core.compressor;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* The CompressorType Test
*/
public class CompressorTypeTest {
@Test
public void testGetByCode() {
int code = 1;
CompressorType expectedType = CompressorType.GZIP;
CompressorType actualType = CompressorType.getByCode(code);
// Assert the returned type matches the expected type
Assertions.assertEquals(expectedType, actualType);
}
@Test
public void testGetByName() {
String name = "gzip";
CompressorType expectedType = CompressorType.GZIP;
CompressorType actualType = CompressorType.getByName(name);
// Assert the returned type matches the expected type
Assertions.assertEquals(expectedType, actualType);
}
}
18 changes: 18 additions & 0 deletions core/src/test/java/io/seata/core/context/ContextCoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

/**
Expand Down Expand Up @@ -68,6 +70,22 @@ public void testGet() {
load.remove(NOT_EXIST_KEY);
}

/**
* Test entries.
*/
@Test
public void testEntries() {
ContextCore load = ContextCoreLoader.load();
load.put(FIRST_KEY, FIRST_VALUE);
load.put(SECOND_KEY, FIRST_VALUE);
Map<String, Object> entries = load.entries();
assertThat(entries.get(FIRST_KEY)).isEqualTo(FIRST_VALUE);
assertThat(entries.get(SECOND_KEY)).isEqualTo(FIRST_VALUE);
load.remove(FIRST_KEY);
load.remove(SECOND_KEY);
load.remove(NOT_EXIST_KEY);
}

/**
* Test remove.
*/
Expand Down
66 changes: 66 additions & 0 deletions core/src/test/java/io/seata/core/context/RootContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

/**
Expand Down Expand Up @@ -57,6 +59,70 @@ public void testGetXID() {
assertThat(RootContext.getXID()).isNull();
}

/**
* Test set timeout.
*/
@Test
public void testSetTimeout() {
RootContext.setTimeout(100);
assertThat(RootContext.getTimeout()).isEqualTo(100);
RootContext.setTimeout(null);
assertThat(RootContext.getTimeout()).isEqualTo(null);
}

/**
* Test get timeout.
*/
@Test
public void testGetTimeout() {
RootContext.setTimeout(100);
assertThat(RootContext.getTimeout()).isEqualTo(100);
RootContext.setTimeout(null);
assertThat(RootContext.getTimeout()).isEqualTo(null);
}

/**
* Test bind global lock flag.
*/
@Test
public void testBindGlobalLockFlag() {
RootContext.bindGlobalLockFlag();
assertThat(RootContext.requireGlobalLock()).isEqualTo(true);
}

/**
* Test unbind global lock flag.
*/
@Test
public void testUnBindGlobalLockFlag() {
RootContext.bindGlobalLockFlag();
assertThat(RootContext.requireGlobalLock()).isEqualTo(true);
RootContext.unbindGlobalLockFlag();
assertThat(RootContext.requireGlobalLock()).isEqualTo(false);
}

/**
* Test require global lock.
*/
@Test
public void testRequireGlobalLock() {
RootContext.bindGlobalLockFlag();
assertThat(RootContext.requireGlobalLock()).isEqualTo(true);
RootContext.unbindGlobalLockFlag();
assertThat(RootContext.requireGlobalLock()).isEqualTo(false);
}

/**
* Test entries.
*/
@Test
public void testEntries() {
RootContext.bind(DEFAULT_XID);
Map<String, Object> entries = RootContext.entries();
assertThat(entries.get(RootContext.KEY_XID)).isEqualTo(DEFAULT_XID);
RootContext.unbind();
}

/**
* Test bind and unbind branchType.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.seata.core.context;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/**
* The type Thread local context core test.
*/
public class ThreadLocalContextCoreTest {
private static ThreadLocalContextCore contextCore ;


@BeforeAll
public static void setUp() {
contextCore = new ThreadLocalContextCore();
}
@Test
public void testPutAndGet() {
// Test putting and getting a value
contextCore.put("key", "value");
assertEquals("value", contextCore.get("key"));
contextCore.remove("key");
}

@Test
public void testRemove() {
// Test putting and removing a value
contextCore.put("key", "value");
assertEquals("value", contextCore.remove("key"));
assertNull(contextCore.get("key"));
}

@Test
public void testEntries() {
// Test getting all entries
contextCore.put("key1", "value1");
contextCore.put("key2", "value2");
contextCore.put("key3", "value3");
assertEquals(3, contextCore.entries().size());
assertTrue(contextCore.entries().containsKey("key1"));
assertTrue(contextCore.entries().containsKey("key2"));
assertTrue(contextCore.entries().containsKey("key3"));
contextCore.remove("key1");
contextCore.remove("key2");
contextCore.remove("key3");
assertNull(contextCore.get("key1"));
assertNull(contextCore.get("key2"));
assertNull(contextCore.get("key3"));
}
}
Loading

0 comments on commit 77bfc08

Please sign in to comment.