From 46f39caf7479020c332628530c0f1164c6d81c25 Mon Sep 17 00:00:00 2001 From: Shi Hu Date: Wed, 6 Dec 2023 11:33:29 +0800 Subject: [PATCH 1/2] fixed the lost key issue --- .../com/vmware/l10n/source/dao/SourceDao.java | 19 +- .../l10n/source/dao/impl/S3SourceDaoImpl.java | 358 ++++++++++++------ .../impl/SyncLocalBundleServiceImpl.java | 46 +-- .../java/com/vmware/controller/S3Test.java | 47 ++- .../com/vmware/controller/SourceDaoTest.java | 22 +- 5 files changed, 292 insertions(+), 200 deletions(-) diff --git a/g11n-ws/vip-manager-l10n/src/main/java/com/vmware/l10n/source/dao/SourceDao.java b/g11n-ws/vip-manager-l10n/src/main/java/com/vmware/l10n/source/dao/SourceDao.java index d1e47dd49..f3e872b58 100644 --- a/g11n-ws/vip-manager-l10n/src/main/java/com/vmware/l10n/source/dao/SourceDao.java +++ b/g11n-ws/vip-manager-l10n/src/main/java/com/vmware/l10n/source/dao/SourceDao.java @@ -1,9 +1,10 @@ /* - * Copyright 2019-2022 VMware, Inc. + * Copyright 2019-2023 VMware, Inc. * SPDX-License-Identifier: EPL-2.0 */ package com.vmware.l10n.source.dao; +import java.io.IOException; import java.util.List; import com.fasterxml.jackson.core.JsonProcessingException; @@ -20,28 +21,22 @@ public interface SourceDao { /** * Get a component's source strings by reading from english resource file. - * + * * @param singleComponentDTO * the object which wraps the base information of a component - * @param filePath - * the location where the resource file is placed, can be - * configed in spring config file * @return the content of the resource file */ public String getFromBundle(SingleComponentDTO singleComponentDTO); /** * Write source strings to local resource file. - * - * @param singleComponentDTO + * + * @param componentMessagesDTO * the object which wraps the content of a component - * @param basepath - * the location where the resource file is placed, can be - * configed in spring config file * @return update result, true represents success, false represents failure. - * @throws JsonProcessingException + * @throws JsonProcessingException */ - public boolean updateToBundle(ComponentMessagesDTO componentMessagesDTO) throws JsonProcessingException; + public boolean updateToBundle(ComponentMessagesDTO componentMessagesDTO) throws IOException; /** * get the update records from bundle diff --git a/g11n-ws/vip-manager-l10n/src/main/java/com/vmware/l10n/source/dao/impl/S3SourceDaoImpl.java b/g11n-ws/vip-manager-l10n/src/main/java/com/vmware/l10n/source/dao/impl/S3SourceDaoImpl.java index fee23c9ee..0bc37af61 100644 --- a/g11n-ws/vip-manager-l10n/src/main/java/com/vmware/l10n/source/dao/impl/S3SourceDaoImpl.java +++ b/g11n-ws/vip-manager-l10n/src/main/java/com/vmware/l10n/source/dao/impl/S3SourceDaoImpl.java @@ -1,156 +1,270 @@ -//Copyright 2019-2023 VMware, Inc. -//SPDX-License-Identifier: EPL-2.0 +/* + * Copyright 2019-2023 VMware, Inc. + * SPDX-License-Identifier: EPL-2.0 + */ package com.vmware.l10n.source.dao.impl; -import java.util.ArrayList; -import java.util.List; - -import jakarta.annotation.PostConstruct; - -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Profile; -import org.springframework.stereotype.Repository; - -import com.amazonaws.services.s3.model.ListObjectsV2Request; -import com.amazonaws.services.s3.model.ListObjectsV2Result; -import com.amazonaws.services.s3.model.S3ObjectSummary; +import com.amazonaws.services.s3.model.*; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; +import com.fasterxml.jackson.databind.SerializationFeature; import com.vmware.l10n.conf.S3Cfg; import com.vmware.l10n.conf.S3Client; import com.vmware.l10n.record.model.RecordModel; import com.vmware.l10n.source.dao.SourceDao; -import com.vmware.l10n.utils.S3Util; -import com.vmware.l10n.utils.S3Util.Locker; import com.vmware.l10n.utils.SourceUtils; import com.vmware.vip.common.constants.ConstantsChar; import com.vmware.vip.common.constants.ConstantsFile; import com.vmware.vip.common.constants.ConstantsKeys; +import com.vmware.vip.common.constants.ConstantsUnicode; import com.vmware.vip.common.i18n.dto.SingleComponentDTO; +import com.vmware.vip.common.i18n.resourcefile.ResourceFilePathGetter; import com.vmware.vip.common.l10n.exception.L10nAPIException; import com.vmware.vip.common.l10n.source.dto.ComponentMessagesDTO; +import jakarta.annotation.PostConstruct; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Repository; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; @Repository @Profile("s3") public class S3SourceDaoImpl implements SourceDao { - - private static Logger logger = LoggerFactory.getLogger(S3SourceDaoImpl.class); + private static Logger logger = LoggerFactory.getLogger(S3SourceDaoImpl.class); + private static long deadLineTime = 1024*32; @Autowired private S3Client s3Client; @Autowired private S3Cfg config; - @Autowired - private S3Util s3util; - - - /** - * the path of local resource file,can be configured in spring config file - **/ - @Value("${source.bundle.file.basepath}") - private String basePath; - - @PostConstruct - private void init() { - if (basePath.startsWith("/")) { - basePath = basePath.substring(1); - }else if(basePath.startsWith(ConstantsChar.DOT+ConstantsChar.BACKSLASH)) { - basePath = basePath.replace(ConstantsChar.DOT, "").replace(ConstantsChar.BACKSLASH, ""); - } - if (!basePath.isEmpty() && !basePath.endsWith(ConstantsChar.BACKSLASH)) { - basePath += ConstantsChar.BACKSLASH; - } - basePath += ConstantsFile.L10N_BUNDLES_PATH; - basePath = basePath.replace("\\", ConstantsChar.BACKSLASH); - } - - @Override - public String getFromBundle(SingleComponentDTO componentMessagesDTO) { - logger.debug("Read content from file: {}/{}", componentMessagesDTO.getLocale(), - componentMessagesDTO.getComponent()); - - return s3util.readBundle(this.basePath, componentMessagesDTO); - } - - @Override - public boolean updateToBundle(ComponentMessagesDTO compDTO) throws JsonProcessingException { - logger.debug("[Save sources to storage]: {}/{}/{}/{}", compDTO.getProductName(), compDTO.getVersion(), - compDTO.getComponent(), compDTO.getLocale()); - - Locker locker = s3util.new Locker(this.basePath, compDTO); - if (!locker.lockFile()) { - logger.warn("failed to lock bundle file, return."); - return false; - } - - boolean bSuccess = false; - try { - boolean bExist = s3util.isBundleExist(this.basePath, compDTO); - if (bExist) { - String existingBundle = s3util.readBundle(this.basePath, compDTO); - SingleComponentDTO latestDTO = SourceUtils.mergeCacheWithBundle(compDTO, existingBundle); - bSuccess = s3util.writeBundle(this.basePath, latestDTO); - } else { - bSuccess = s3util.writeBundle(this.basePath, compDTO); - } - if (bSuccess) { - if (bExist) { - logger.debug("The bundle file {}/{} is found, update the bundle file.", compDTO.getLocale(), - compDTO.getComponent()); - - } else { - logger.debug("The bundle file {}/{} is not found, cascade create the dir, add new bundle file ", - compDTO.getLocale(), compDTO.getComponent()); - - } - } - } finally { - locker.unlockFile(); - } - - return bSuccess; - } - - @Override - public List getUpdateRecords(String productName, String version, long lastModifyTime) throws L10nAPIException{ - - List records = new ArrayList(); - ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(config.getBucketName()); - - String latestJsonFile = ConstantsFile.LOCAL_FILE_SUFFIX+ConstantsChar.UNDERLINE+ConstantsKeys.LATEST+ConstantsFile.FILE_TPYE_JSON; - StringBuilder prefix = new StringBuilder(); - prefix.append(this.basePath); - if (!StringUtils.isEmpty(productName)) { - prefix.append(productName); - prefix.append(ConstantsChar.BACKSLASH); - } - if (!StringUtils.isEmpty(version)) { - prefix.append(version); - prefix.append(ConstantsChar.BACKSLASH); - } - logger.info("begin getUpdateRecords lastModyTime: {}, prefix: {}", lastModifyTime, prefix.toString()); - req.setPrefix(prefix.toString()); - + + + /** + * the path of local resource file,can be configured in spring config file + **/ + @Value("${source.bundle.file.basepath}") + private String basePath; + + @Value("${source.sync.s3.compare.version.count:3}") + private int compareVersionNum = 3; + private ObjectWriter objectWriter = new ObjectMapper().writer(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS).withDefaultPrettyPrinter(); + + @PostConstruct + private void init() { + if (basePath.startsWith("/")) { + basePath = basePath.substring(1); + }else if(basePath.startsWith(ConstantsChar.DOT+ConstantsChar.BACKSLASH)) { + basePath = basePath.replace(ConstantsChar.DOT, "").replace(ConstantsChar.BACKSLASH, ""); + } + if (!basePath.isEmpty() && !basePath.endsWith(ConstantsChar.BACKSLASH)) { + basePath += ConstantsChar.BACKSLASH; + } + basePath += ConstantsFile.L10N_BUNDLES_PATH; + basePath = basePath.replace("\\", ConstantsChar.BACKSLASH); + } + + + @Override + public String getFromBundle(SingleComponentDTO componentMessagesDTO) { + + logger.debug("Read content from file: {}/{}", componentMessagesDTO.getLocale(), componentMessagesDTO.getComponent()); + String bundlePath = getBundleFilePath(basePath, componentMessagesDTO); + return s3Client.readObject(bundlePath); + + } + + @Override + public boolean updateToBundle(ComponentMessagesDTO componentMessagesDTO) throws IOException{ + + String bundlePath = getBundleFilePath(basePath, componentMessagesDTO); + ListVersionsRequest lvr = new ListVersionsRequest(); + lvr.setBucketName(config.getBucketName()); + lvr.setPrefix(bundlePath); + lvr.setMaxResults(compareVersionNum); + + long sleepTime = 512L; + boolean flag = true; + do { + S3Object reqS3Obj = null; + try { + reqS3Obj = s3Client.getS3Client().getObject(config.getBucketName(), bundlePath); + }catch (Exception ex){ + logger.info("create new key:{}", bundlePath); + reqS3Obj = null; + } + + String sourceVersionId = null; + String content = null; + String updatedVersionId = null; + if (reqS3Obj == null){ + content = getOrderBundleJson(componentMessagesDTO); + if (!s3Client.isObjectExist(bundlePath)){ + PutObjectResult putResult = s3Client.getS3Client().putObject(config.getBucketName(), bundlePath, content); + updatedVersionId = putResult.getVersionId(); + VersionListing versionListing = s3Client.getS3Client().listVersions(lvr); + String latestVersionId = versionListing.getVersionSummaries().get(0).getVersionId(); + if (updatedVersionId.equals(latestVersionId)) { + return true; + }else { + s3Client.getS3Client().deleteVersion(config.getBucketName(), bundlePath, updatedVersionId); + logger.warn("index 0, summarySize: {}, delete key: {}, no source version: {} ", versionListing.getVersionSummaries().size(), bundlePath, updatedVersionId); + logger.warn(content); + } + } + + }else { + sourceVersionId = reqS3Obj.getObjectMetadata().getVersionId(); + String existingBundle = null; + try { + existingBundle = convertS3Obj2Str(reqS3Obj); + } catch (IOException e) { + logger.error(e.getMessage(), e); + throw e; + } + SingleComponentDTO latestDTO = SourceUtils.mergeCacheWithBundle(componentMessagesDTO, existingBundle); + content = getOrderBundleJson(latestDTO); + VersionListing sourceVersionListing = s3Client.getS3Client().listVersions(lvr); + String preVersionId = sourceVersionListing.getVersionSummaries().get(0).getVersionId(); + if (sourceVersionId.equals(preVersionId)) { + PutObjectResult putResult = s3Client.getS3Client().putObject(config.getBucketName(), bundlePath, content); + updatedVersionId = putResult.getVersionId(); + VersionListing updatedVersionListing = s3Client.getS3Client().listVersions(lvr); + List updateVersionSummary = updatedVersionListing.getVersionSummaries(); + boolean isNotBreak = true; + for (int i = 0; i < updateVersionSummary.size(); i++) { + if (i == 0) { + if (!updatedVersionId.equals(updateVersionSummary.get(i).getVersionId())) { + s3Client.getS3Client().deleteVersion(config.getBucketName(), bundlePath, updatedVersionId); + isNotBreak = false; + logger.warn("index{}, delete key: {}, no source version: {} ",i, bundlePath, updatedVersionId); + logger.warn(content); + break; + } + } else { + if (!updateVersionSummary.get(i).getVersionId().equals(sourceVersionListing.getVersionSummaries().get(i-1).getVersionId())) { + s3Client.getS3Client().deleteVersion(config.getBucketName(), bundlePath, updatedVersionId); + logger.warn("delete key: {}, no source version: {} ", bundlePath, updatedVersionId); + logger.warn("index{}, updated {}, source{}", i, updateVersionSummary.get(i).getVersionId(), sourceVersionListing.getVersionSummaries().get(i-1).getVersionId()); + logger.warn(content); + isNotBreak = false; + break; + } + } + } + if (isNotBreak){ + return true; + } + } + } + + if (sleepTime > deadLineTime){ + logger.warn("sleep Time {}", sleepTime); + flag = false; + }else { + try { + logger.warn("sleep Time {}", sleepTime); + Thread.sleep(sleepTime); + } catch (InterruptedException e) { + logger.warn(e.getMessage(), e); + } + sleepTime = sleepTime << 1; + } + + }while(flag); + + return false; + + } + + @Override + public List getUpdateRecords(String productName, String version, long lastModifyTime) throws L10nAPIException{ + + List records = new ArrayList(); + ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(config.getBucketName()); + + String latestJsonFile = ConstantsFile.LOCAL_FILE_SUFFIX+ConstantsChar.UNDERLINE+ ConstantsKeys.LATEST+ConstantsFile.FILE_TPYE_JSON; + StringBuilder prefix = new StringBuilder(); + prefix.append(this.basePath); + if (!StringUtils.isEmpty(productName)) { + prefix.append(productName); + prefix.append(ConstantsChar.BACKSLASH); + } + if (!StringUtils.isEmpty(version)) { + prefix.append(version); + prefix.append(ConstantsChar.BACKSLASH); + } + logger.info("begin getUpdateRecords lastModyTime: {}, prefix: {}", lastModifyTime, prefix.toString()); + req.setPrefix(prefix.toString()); + ListObjectsV2Result result; do { result = s3Client.getS3Client().listObjectsV2(req); for (S3ObjectSummary oSy : result.getObjectSummaries()) { - String keyStr = oSy.getKey(); - long currentModifyTime = oSy.getLastModified().getTime(); - if(keyStr.endsWith(latestJsonFile) - && currentModifyTime>lastModifyTime) { - logger.info("Need Update:{}:{}", keyStr, currentModifyTime); - records.add(SourceUtils.parseKeyStr2Record(keyStr,this.basePath, currentModifyTime)); - } + String keyStr = oSy.getKey(); + long currentModifyTime = oSy.getLastModified().getTime(); + if(keyStr.endsWith(latestJsonFile) + && currentModifyTime>lastModifyTime) { + logger.info("Need Update:{}:{}", keyStr, currentModifyTime); + records.add(SourceUtils.parseKeyStr2Record(keyStr,this.basePath, currentModifyTime)); + } } String token = result.getNextContinuationToken(); req.setContinuationToken(token); } while (result.isTruncated()); return records; - } + } + + private static String convertS3Obj2Str(S3Object s3Obj) throws IOException { + S3ObjectInputStream s3is = s3Obj.getObjectContent(); + ByteArrayOutputStream fos = new ByteArrayOutputStream(); + byte[] read_buf = new byte[1024]; + int read_len = 0; + try { + while ((read_len = s3is.read(read_buf)) > 0) { + fos.write(read_buf, 0, read_len); + } + return fos.toString(ConstantsUnicode.UTF8); + } finally { + s3is.close(); + fos.close(); + + } + } + + private String getBundleFilePath(String basePath, SingleComponentDTO dto) { + if (StringUtils.isEmpty(dto.getComponent())) { + dto.setComponent(ConstantsFile.DEFAULT_COMPONENT); + } + return genProductVersionS3Path(basePath, dto.getProductName(), dto.getVersion()) + dto.getComponent() + + ConstantsChar.BACKSLASH + ResourceFilePathGetter.getLocalizedJSONFileName(dto.getLocale()); + } - + private String genProductVersionS3Path(String basePath, String productName, String version) { + StringBuilder path = new StringBuilder(); + path.append(basePath); + path.append(productName); + path.append(ConstantsChar.BACKSLASH); + path.append(version); + path.append(ConstantsChar.BACKSLASH); + return path.toString(); + } + private String getOrderBundleJson(SingleComponentDTO componentDTO) throws JsonProcessingException { + Map json = new HashMap<>(); + json.put(ConstantsKeys.COMPONENT, componentDTO.getComponent()); + json.put(ConstantsKeys.lOCALE, componentDTO.getLocale()); + json.put(ConstantsKeys.MESSAGES, componentDTO.getMessages()); + json.put(ConstantsKeys.ID, componentDTO.getId()); + return this.objectWriter.writeValueAsString(json); + } } diff --git a/g11n-ws/vip-manager-l10n/src/main/java/com/vmware/l10n/source/service/impl/SyncLocalBundleServiceImpl.java b/g11n-ws/vip-manager-l10n/src/main/java/com/vmware/l10n/source/service/impl/SyncLocalBundleServiceImpl.java index ab443e5a7..e3148d5df 100644 --- a/g11n-ws/vip-manager-l10n/src/main/java/com/vmware/l10n/source/service/impl/SyncLocalBundleServiceImpl.java +++ b/g11n-ws/vip-manager-l10n/src/main/java/com/vmware/l10n/source/service/impl/SyncLocalBundleServiceImpl.java @@ -4,12 +4,12 @@ */ package com.vmware.l10n.source.service.impl; -import java.io.File; -import java.io.IOException; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - +import com.vmware.l10n.source.dao.SourceDao; +import com.vmware.l10n.source.service.SyncLocalBundleService; +import com.vmware.l10n.utils.DiskQueueUtils; +import com.vmware.vip.common.l10n.exception.L10nAPIException; +import com.vmware.vip.common.l10n.source.dto.ComponentMessagesDTO; +import com.vmware.vip.common.l10n.source.dto.ComponentSourceDTO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; @@ -18,12 +18,11 @@ import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; -import com.vmware.l10n.source.dao.SourceDao; -import com.vmware.l10n.source.service.SyncLocalBundleService; -import com.vmware.l10n.utils.DiskQueueUtils; -import com.vmware.vip.common.l10n.exception.L10nAPIException; -import com.vmware.vip.common.l10n.source.dto.ComponentMessagesDTO; -import com.vmware.vip.common.l10n.source.dto.ComponentSourceDTO; +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; /** * @@ -86,7 +85,6 @@ public synchronized void mergeSourceToLocalBundle() { return; } LOGGER.debug("the source cache file size---{}", queueFiles.size()); - boolean moveFileFlag = true; for (File quefile : queueFiles) { try { Map mapObj = DiskQueueUtils.getQueueFile2Obj(quefile); @@ -101,18 +99,13 @@ public synchronized void mergeSourceToLocalBundle() { if (!updateFlag) { throw new L10nAPIException("Failed to update source:" + ehcachekey); } + } } - + processSendFilePath(this.basePath, quefile); } catch (L10nAPIException e) { - LOGGER.error(e.getMessage(), e); - moveFileFlag = false; - try { - Thread.sleep(30000); - } catch (InterruptedException ex) { - LOGGER.error(ex.getMessage(), ex); - } - DiskQueueUtils.copyFile2ExceptPath(basePath, quefile, this.activeDaoType); + LOGGER.warn(e.getMessage(), e); + LOGGER.warn("The source cache file:{} will re-update to {}", quefile.getAbsolutePath(), this.activeDaoType); break; } catch (Exception e) { LOGGER.error(e.getMessage(), e); @@ -120,15 +113,6 @@ public synchronized void mergeSourceToLocalBundle() { } } - if (moveFileFlag){ - for (File delFile : queueFiles){ - try { - processSendFilePath(this.basePath, delFile); - } catch (IOException e) { - DiskQueueUtils.moveFile2ExceptPath(basePath, delFile, LOCAL_STR); - } - } - } } diff --git a/g11n-ws/vip-manager-l10n/src/test/java/com/vmware/controller/S3Test.java b/g11n-ws/vip-manager-l10n/src/test/java/com/vmware/controller/S3Test.java index 70eda9b10..68b6aace3 100644 --- a/g11n-ws/vip-manager-l10n/src/test/java/com/vmware/controller/S3Test.java +++ b/g11n-ws/vip-manager-l10n/src/test/java/com/vmware/controller/S3Test.java @@ -1,21 +1,15 @@ /* - * Copyright 2019-2022 VMware, Inc. + * Copyright 2019-2023 VMware, Inc. * SPDX-License-Identifier: EPL-2.0 */ package com.vmware.controller; -import java.io.FileReader; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; -import java.util.stream.Stream; - +import com.fasterxml.jackson.core.JsonProcessingException; +import com.vmware.l10n.BootApplication; +import com.vmware.l10n.source.dao.SourceDao; +import com.vmware.l10n.translation.dao.SingleComponentDao; +import com.vmware.vip.common.l10n.exception.L10nAPIException; +import com.vmware.vip.common.l10n.source.dto.ComponentMessagesDTO; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; @@ -30,12 +24,17 @@ import org.springframework.test.context.junit4.SpringRunner; import org.springframework.web.context.WebApplicationContext; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.vmware.l10n.BootApplication; -import com.vmware.l10n.source.dao.SourceDao; -import com.vmware.l10n.translation.dao.SingleComponentDao; -import com.vmware.vip.common.l10n.exception.L10nAPIException; -import com.vmware.vip.common.l10n.source.dto.ComponentMessagesDTO; +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.stream.Stream; @RunWith(SpringRunner.class) @SpringBootTest(classes = BootApplication.class) @@ -74,7 +73,7 @@ public void test001() { Runnable r1 = () -> { try { sourceDao.updateToBundle(single); - } catch (JsonProcessingException e) { + } catch (IOException e) { logger.error(e.getMessage(), e); } }; @@ -84,7 +83,7 @@ public void test001() { Runnable r2 = () -> { try { sourceDao.updateToBundle(single); - } catch (JsonProcessingException e) { + } catch (IOException e) { logger.error(e.getMessage(), e); } }; @@ -123,7 +122,7 @@ public void test001() { } @Test - public void test002() throws IOException { + public void test002() throws Exception { SourceDao sourceDao = webApplicationContext.getBean(SourceDao.class); SingleComponentDao singleComponentDao = webApplicationContext.getBean(SingleComponentDao.class); String fileNamePrefix = "messages_"; @@ -156,7 +155,7 @@ public void test002() throws IOException { if (dto.getLocale().equals("latest")) { try { sourceDao.updateToBundle(dto); - } catch (JsonProcessingException e) { + } catch (IOException e) { logger.error(e.getMessage(), e); } sourceDao.getFromBundle(dto); @@ -182,7 +181,7 @@ public void test002() throws IOException { } @Test - public void test003() throws JsonProcessingException { + public void test003() throws Exception { SourceDao sourceDao = webApplicationContext.getBean(SourceDao.class); ComponentMessagesDTO single = new ComponentMessagesDTO(); diff --git a/g11n-ws/vip-manager-l10n/src/test/java/com/vmware/controller/SourceDaoTest.java b/g11n-ws/vip-manager-l10n/src/test/java/com/vmware/controller/SourceDaoTest.java index c93baff9b..8355e1cc4 100644 --- a/g11n-ws/vip-manager-l10n/src/test/java/com/vmware/controller/SourceDaoTest.java +++ b/g11n-ws/vip-manager-l10n/src/test/java/com/vmware/controller/SourceDaoTest.java @@ -1,12 +1,13 @@ /* - * Copyright 2019-2022 VMware, Inc. + * Copyright 2019-2023 VMware, Inc. * SPDX-License-Identifier: EPL-2.0 */ package com.vmware.controller; -import java.util.HashMap; -import java.util.Map; - +import com.vmware.l10n.BootApplication; +import com.vmware.l10n.source.dao.SourceDao; +import com.vmware.vip.common.l10n.source.dto.ComponentMessagesDTO; +import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; @@ -16,10 +17,9 @@ import org.springframework.test.context.junit4.SpringRunner; import org.springframework.web.context.WebApplicationContext; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.vmware.l10n.BootApplication; -import com.vmware.l10n.source.dao.SourceDao; -import com.vmware.vip.common.l10n.source.dto.ComponentMessagesDTO; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; @RunWith(SpringRunner.class) @SpringBootTest(classes = BootApplication.class) @@ -32,7 +32,7 @@ public class SourceDaoTest { @Test -public void test001() throws JsonProcessingException { +public void test001() throws IOException { SourceDao sourceDao = webApplicationContext.getBean(SourceDao.class); ComponentMessagesDTO single= new ComponentMessagesDTO(); @@ -51,8 +51,8 @@ public void test001() throws JsonProcessingException { map.put("dc.unittest.new", "this is unit test new value"); sourceDao.updateToBundle(single); - - sourceDao.getFromBundle(single); + + Assert.assertNotNull(sourceDao.getFromBundle(single)); } From 620d3737154f550f933db9dbfc8785a040672dd1 Mon Sep 17 00:00:00 2001 From: Shi Hu Date: Wed, 6 Dec 2023 11:56:06 +0800 Subject: [PATCH 2/2] code format --- .../impl/SyncLocalBundleServiceImpl.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/g11n-ws/vip-manager-l10n/src/main/java/com/vmware/l10n/source/service/impl/SyncLocalBundleServiceImpl.java b/g11n-ws/vip-manager-l10n/src/main/java/com/vmware/l10n/source/service/impl/SyncLocalBundleServiceImpl.java index e3148d5df..f4d543937 100644 --- a/g11n-ws/vip-manager-l10n/src/main/java/com/vmware/l10n/source/service/impl/SyncLocalBundleServiceImpl.java +++ b/g11n-ws/vip-manager-l10n/src/main/java/com/vmware/l10n/source/service/impl/SyncLocalBundleServiceImpl.java @@ -4,12 +4,12 @@ */ package com.vmware.l10n.source.service.impl; -import com.vmware.l10n.source.dao.SourceDao; -import com.vmware.l10n.source.service.SyncLocalBundleService; -import com.vmware.l10n.utils.DiskQueueUtils; -import com.vmware.vip.common.l10n.exception.L10nAPIException; -import com.vmware.vip.common.l10n.source.dto.ComponentMessagesDTO; -import com.vmware.vip.common.l10n.source.dto.ComponentSourceDTO; +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; @@ -18,11 +18,12 @@ import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; -import java.io.File; -import java.io.IOException; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; +import com.vmware.l10n.source.dao.SourceDao; +import com.vmware.l10n.source.service.SyncLocalBundleService; +import com.vmware.l10n.utils.DiskQueueUtils; +import com.vmware.vip.common.l10n.exception.L10nAPIException; +import com.vmware.vip.common.l10n.source.dto.ComponentMessagesDTO; +import com.vmware.vip.common.l10n.source.dto.ComponentSourceDTO; /** *