Skip to content

Commit

Permalink
fix(alibaba#12231): When inserting and updating configurations in the…
Browse files Browse the repository at this point in the history
… database, the time-related field values need to be set using the time obtained from the database's built-in time function.
  • Loading branch information
Bo-Qiu committed Jun 15, 2024
1 parent 6fe4d0f commit cb7a1a1
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void testInsertOrUpdateBetaOfUpdate() {
//verify update to be invoked
embeddedStorageContextHolderMockedStatic.verify(
() -> EmbeddedStorageContextHolder.addSqlContext(anyString(), eq(configInfo.getContent()), eq(configInfo.getMd5()),
eq(betaIps), eq(srcIp), eq(srcUser), any(Timestamp.class), eq(configInfo.getAppName()),
eq(betaIps), eq(srcIp), eq(srcUser), eq(configInfo.getAppName()),
eq(configInfo.getEncryptedDataKey()), eq(dataId), eq(group), eq(tenant)), times(1));

}
Expand Down Expand Up @@ -163,7 +163,7 @@ void testInsertOrUpdateBetaOfAdd() {
embeddedStorageContextHolderMockedStatic.verify(
() -> EmbeddedStorageContextHolder.addSqlContext(anyString(), eq(dataId), eq(group), eq(tenant),
eq(configInfo.getAppName()), eq(configInfo.getContent()), eq(configInfo.getMd5()), eq(betaIps), eq(srcIp),
eq(srcUser), any(Timestamp.class), any(Timestamp.class), eq(configInfo.getEncryptedDataKey())), times(1));
eq(srcUser), eq(configInfo.getEncryptedDataKey())), times(1));
}

@Test
Expand Down Expand Up @@ -240,7 +240,7 @@ void testInsertOrUpdateBetaCasOfAdd() {
embeddedStorageContextHolderMockedStatic.verify(
() -> EmbeddedStorageContextHolder.addSqlContext(anyString(), eq(dataId), eq(group), eq(tenant),
eq(configInfo.getAppName()), eq(configInfo.getContent()), eq(configInfo.getMd5()), eq(betaIps), eq(srcIp),
eq(srcUser), any(Timestamp.class), any(Timestamp.class), eq(configInfo.getEncryptedDataKey())), times(1));
eq(srcUser), eq(configInfo.getEncryptedDataKey())), times(1));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ void testInsertOrUpdateOfInsertConfigSuccess() {
//expect insert config info invoked.
embeddedStorageContextHolderMockedStatic.verify(
() -> EmbeddedStorageContextHolder.addSqlContext(anyString(), anyLong(), eq(dataId), eq(group), eq(tenant), eq(appName),
eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), any(Timestamp.class),
any(Timestamp.class), eq(desc), eq(use), eq(effect), eq(type), eq(schema), eq(encryptedDataKey)), times(1));
eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser),
eq(desc), eq(use), eq(effect), eq(type), eq(schema), eq(encryptedDataKey)), times(1));
//expect insert config tags
embeddedStorageContextHolderMockedStatic.verify(
() -> EmbeddedStorageContextHolder.addSqlContext(anyString(), anyLong(), eq("tag1"), eq(StringUtils.EMPTY), eq(dataId),
Expand Down Expand Up @@ -221,8 +221,8 @@ void testInsertOrUpdateCasOfInsertConfigSuccess() {
//expect insert config info invoked.
embeddedStorageContextHolderMockedStatic.verify(
() -> EmbeddedStorageContextHolder.addSqlContext(anyString(), anyLong(), eq(dataId), eq(group), eq(tenant), eq(appName),
eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), any(Timestamp.class),
any(Timestamp.class), eq(desc), eq(use), eq(effect), eq(type), eq(schema), eq(encryptedDatakey)), times(1));
eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser),
eq(desc), eq(use), eq(effect), eq(type), eq(schema), eq(encryptedDatakey)), times(1));
//expect insert config tags
embeddedStorageContextHolderMockedStatic.verify(
() -> EmbeddedStorageContextHolder.addSqlContext(anyString(), anyLong(), eq("tag1"), eq(StringUtils.EMPTY), eq(dataId),
Expand Down Expand Up @@ -281,7 +281,7 @@ void testInsertOrUpdateOfUpdateConfigSuccess() {

//expect update config info invoked.
embeddedStorageContextHolderMockedStatic.verify(() -> EmbeddedStorageContextHolder.addSqlContext(anyString(), eq(content),
eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), any(Timestamp.class), eq(appName), eq(desc),
eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), eq(appName), eq(desc),
eq(use), eq(effect), eq(type), eq(schema), eq(encryptedDataKey), eq(dataId), eq(group), eq(tenant)), times(1));

//expect insert config tags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ void testInsertOrUpdateTagOfAdd() {
//mock insert invoked.
embeddedStorageContextHolderMockedStatic.verify(
() -> EmbeddedStorageContextHolder.addSqlContext(anyString(), eq(dataId), eq(group), eq(tenant), eq(tag), eq(appName),
eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), any(Timestamp.class),
any(Timestamp.class)), times(1));
eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser)), times(1));
assertEquals(configInfoStateWrapper.getId(), configOperateResult.getId());
assertEquals(configInfoStateWrapper.getLastModified(), configOperateResult.getLastModified());

Expand Down Expand Up @@ -151,7 +150,7 @@ void testInsertOrUpdateTagOfUpdate() {
ConfigOperateResult configOperateResult = embeddedConfigInfoTagPersistService.insertOrUpdateTag(configInfo, tag, srcIp, srcUser);
//verify update to be invoked
embeddedStorageContextHolderMockedStatic.verify(() -> EmbeddedStorageContextHolder.addSqlContext(anyString(), eq(content),
eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), any(Timestamp.class), eq(appName),
eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), eq(appName),
eq(dataId), eq(group), eq(tenant), eq(tag)), times(1));
assertEquals(configInfoStateWrapper.getId(), configOperateResult.getId());
assertEquals(configInfoStateWrapper.getLastModified(), configOperateResult.getLastModified());
Expand Down Expand Up @@ -185,8 +184,7 @@ void testInsertOrUpdateTagCasOfAdd() {
//mock insert invoked.
embeddedStorageContextHolderMockedStatic.verify(
() -> EmbeddedStorageContextHolder.addSqlContext(anyString(), eq(dataId), eq(group), eq(tenant), eq(tag), eq(appName),
eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), any(Timestamp.class),
any(Timestamp.class)), times(1));
eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser)), times(1));
assertEquals(configInfoStateWrapper.getId(), configOperateResult.getId());
assertEquals(configInfoStateWrapper.getLastModified(), configOperateResult.getLastModified());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.springframework.transaction.TransactionSystemException;
import org.springframework.transaction.support.TransactionTemplate;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -50,7 +49,6 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -130,8 +128,7 @@ void testAddAggrConfigInfoOfAddNewContent() {
when(jdbcTemplate.queryForObject(anyString(), eq(new Object[] {dataId, group, tenant, datumId}), eq(String.class))).thenThrow(
new EmptyResultDataAccessException(1));
//mock insert success
when(jdbcTemplate.update(anyString(), eq(dataId), eq(group), eq(tenant), eq(datumId), eq(appName), eq(content),
any(Timestamp.class))).thenReturn(1);
when(jdbcTemplate.update(anyString(), eq(dataId), eq(group), eq(tenant), eq(datumId), eq(appName), eq(content))).thenReturn(1);

//execute
boolean result = externalConfigInfoAggrPersistService.addAggrConfigInfo(dataId, group, tenant, datumId, appName, content);
Expand All @@ -152,12 +149,10 @@ void testAddAggrConfigInfoOfUpdateNotEqualContent() {
when(jdbcTemplate.queryForObject(anyString(), eq(new Object[] {dataId, group, tenant, datumId}), eq(String.class))).thenReturn(
existContent);
//mock update success,return 1
when(jdbcTemplate.update(anyString(), eq(content), any(Timestamp.class), eq(dataId), eq(group), eq(tenant),
eq(datumId))).thenReturn(1);
when(jdbcTemplate.update(anyString(), eq(content), eq(dataId), eq(group), eq(tenant), eq(datumId))).thenReturn(1);
//mock update content
boolean result = externalConfigInfoAggrPersistService.addAggrConfigInfo(dataId, group, tenant, datumId, appName, content);
assertTrue(result);

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ void testInsertOrUpdateBetaOfUpdate() {
//verify update to be invoked
Mockito.verify(jdbcTemplate, times(1))
.update(anyString(), eq(configInfo.getContent()), eq(configInfo.getMd5()), eq(betaIps), eq(srcIp), eq(srcUser),
any(Timestamp.class), eq(configInfo.getAppName()), eq(configInfo.getEncryptedDataKey()), eq(dataId), eq(group),
eq(tenant));
eq(configInfo.getAppName()), eq(configInfo.getEncryptedDataKey()), eq(dataId), eq(group), eq(tenant));
}

@Test
Expand Down Expand Up @@ -165,8 +164,7 @@ void testInsertOrUpdateBetaOfAdd() {
//verify add to be invoked
Mockito.verify(jdbcTemplate, times(1))
.update(anyString(), eq(dataId), eq(group), eq(tenant), eq(configInfo.getAppName()), eq(configInfo.getContent()),
eq(configInfo.getMd5()), eq(betaIps), eq(srcIp), eq(srcUser), any(Timestamp.class), any(Timestamp.class),
eq(configInfo.getEncryptedDataKey()));
eq(configInfo.getMd5()), eq(betaIps), eq(srcIp), eq(srcUser), eq(configInfo.getEncryptedDataKey()));

}

Expand Down Expand Up @@ -195,7 +193,7 @@ void testInsertOrUpdateBetaOfException() {

// mock update throw CannotGetJdbcConnectionException
when(jdbcTemplate.update(anyString(), eq(configInfo.getContent()), eq(configInfo.getMd5()), eq(betaIps), eq(srcIp), eq(srcUser),
any(Timestamp.class), eq(configInfo.getAppName()), eq(configInfo.getEncryptedDataKey()), eq(dataId), eq(group),
eq(configInfo.getAppName()), eq(configInfo.getEncryptedDataKey()), eq(dataId), eq(group),
eq(tenant))).thenThrow(new CannotGetJdbcConnectionException("mock fail"));
//execute of update& expect.
try {
Expand All @@ -210,7 +208,7 @@ void testInsertOrUpdateBetaOfException() {
eq(CONFIG_INFO_STATE_WRAPPER_ROW_MAPPER))).thenReturn(null);
//mock add throw CannotGetJdbcConnectionException
when(jdbcTemplate.update(anyString(), eq(dataId), eq(group), eq(tenant), eq(configInfo.getAppName()), eq(configInfo.getContent()),
eq(configInfo.getMd5()), eq(betaIps), eq(srcIp), eq(srcUser), any(Timestamp.class), any(Timestamp.class),
eq(configInfo.getMd5()), eq(betaIps), eq(srcIp), eq(srcUser),
eq(configInfo.getEncryptedDataKey()))).thenThrow(new CannotGetJdbcConnectionException("mock fail add"));
//execute of add& expect.
try {
Expand Down Expand Up @@ -307,8 +305,7 @@ void testInsertOrUpdateBetaCasOfAdd() {
//verify add to be invoked
Mockito.verify(jdbcTemplate, times(1))
.update(anyString(), eq(dataId), eq(group), eq(tenant), eq(configInfo.getAppName()), eq(configInfo.getContent()),
eq(configInfo.getMd5()), eq(betaIps), eq(srcIp), eq(srcUser), any(Timestamp.class), any(Timestamp.class),
eq(configInfo.getEncryptedDataKey()));
eq(configInfo.getMd5()), eq(betaIps), eq(srcIp), eq(srcUser), eq(configInfo.getEncryptedDataKey()));

}

Expand Down Expand Up @@ -353,7 +350,7 @@ void testInsertOrUpdateBetaCasOfException() {
//mock add throw CannotGetJdbcConnectionException
when(jdbcTemplate.update(anyString(), eq(dataId), eq(group), eq(tenant), eq(configInfo.getAppName()), eq(configInfo.getContent()),
eq(MD5Utils.md5Hex(configInfo.getContent(), Constants.PERSIST_ENCODE)), eq(betaIps), eq(srcIp), eq(srcUser),
any(Timestamp.class), any(Timestamp.class), eq(configInfo.getEncryptedDataKey()))).thenThrow(
eq(configInfo.getEncryptedDataKey()))).thenThrow(
new CannotGetJdbcConnectionException("mock fail add"));

//execute of add& expect.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,6 @@ void testCreatePsForInsertConfigInfo() throws SQLException {
externalConfigInfoPersistService.createPsForInsertConfigInfo(srcIp, srcUser, configInfo, configAdvanceInfo, mockConnection,
configInfoMapper);
Mockito.verify(preparedStatement, times(14)).setString(anyInt(), anyString());
Mockito.verify(preparedStatement, times(2)).setTimestamp(anyInt(), any(Timestamp.class));

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void testInsertOrUpdateTagOfAdd() {
configInfoStateWrapper.setLastModified(System.currentTimeMillis());
configInfoStateWrapper.setId(234567890L);
String tag = "tag123";
Mockito.when(jdbcTemplate.queryForObject(anyString(), eq(new Object[] {dataId, group, tenant, tag}),
Mockito.when(jdbcTemplate.queryForObject(anyString(), eq(new Object[]{dataId, group, tenant, tag}),
eq(CONFIG_INFO_STATE_WRAPPER_ROW_MAPPER))).thenThrow(new EmptyResultDataAccessException(1))
.thenReturn(configInfoStateWrapper);
String srcIp = "ip345678";
Expand All @@ -124,10 +124,10 @@ void testInsertOrUpdateTagOfAdd() {
//verify insert to be invoked
Mockito.verify(jdbcTemplate, times(1))
.update(anyString(), eq(dataId), eq(group), eq(tenant), eq(tag), eq(appName), eq(configInfo.getContent()),
eq(configInfo.getMd5()), eq(srcIp), eq(srcUser), any(Timestamp.class), any(Timestamp.class));
eq(configInfo.getMd5()), eq(srcIp), eq(srcUser));
assertEquals(configInfoStateWrapper.getId(), configOperateResult.getId());
assertEquals(configInfoStateWrapper.getLastModified(), configOperateResult.getLastModified());

}

@Test
Expand All @@ -152,7 +152,7 @@ void testInsertOrUpdateTagOfUpdate() {
ConfigOperateResult configOperateResult = externalConfigInfoTagPersistService.insertOrUpdateTag(configInfo, tag, srcIp, srcUser);
//verify update to be invoked
Mockito.verify(jdbcTemplate, times(1))
.update(anyString(), eq(configInfo.getContent()), eq(configInfo.getMd5()), eq(srcIp), eq(srcUser), any(Timestamp.class),
.update(anyString(), eq(configInfo.getContent()), eq(configInfo.getMd5()), eq(srcIp), eq(srcUser),
eq(appName), eq(dataId), eq(group), eq(tenant), eq(tag));
assertEquals(configInfoStateWrapper.getId(), configOperateResult.getId());
assertEquals(configInfoStateWrapper.getLastModified(), configOperateResult.getLastModified());
Expand Down Expand Up @@ -184,8 +184,7 @@ void testInsertOrUpdateTagCasOfAdd() {
//verify insert to be invoked
Mockito.verify(jdbcTemplate, times(1))
.update(anyString(), eq(dataId), eq(group), eq(tenant), eq(tag), eq(appName), eq(configInfo.getContent()),
eq(MD5Utils.md5Hex(configInfo.getContent(), Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser),
any(Timestamp.class), any(Timestamp.class));
eq(MD5Utils.md5Hex(configInfo.getContent(), Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser));
assertEquals(configInfoStateWrapper.getId(), configOperateResult.getId());
assertEquals(configInfoStateWrapper.getLastModified(), configOperateResult.getLastModified());

Expand Down Expand Up @@ -253,11 +252,10 @@ void testInsertOrUpdateTagCasOfException() {
assertEquals("state query throw exception", e.getMessage());
}
//mock get state return null,and execute add throw CannotGetJdbcConnectionException
Mockito.when(jdbcTemplate.queryForObject(anyString(), eq(new Object[] {dataId, group, tenant, tag}),
Mockito.when(jdbcTemplate.queryForObject(anyString(), eq(new Object[]{dataId, group, tenant, tag}),
eq(CONFIG_INFO_STATE_WRAPPER_ROW_MAPPER))).thenReturn(null);
Mockito.when(jdbcTemplate.update(anyString(), eq(dataId), eq(group), eq(tenant), eq(tag), eq(appName), eq(configInfo.getContent()),
eq(MD5Utils.md5Hex(configInfo.getContent(), Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), any(Timestamp.class),
any(Timestamp.class))).thenThrow(new CannotGetJdbcConnectionException("throw exception add config tag"));
eq(MD5Utils.md5Hex(configInfo.getContent(), Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser))).thenThrow(new CannotGetJdbcConnectionException("throw exception add config tag"));
try {
externalConfigInfoTagPersistService.insertOrUpdateTagCas(configInfo, tag, srcIp, srcUser);
assertTrue(false);
Expand Down

0 comments on commit cb7a1a1

Please sign in to comment.