Skip to content

Commit

Permalink
Extract enum EntityType from EntityFileRelation (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
olivershen-wow authored Mar 1, 2023
1 parent f530d16 commit 17d8e39
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
package com.microsoft.hydralab.agent.service;

import com.microsoft.hydralab.common.entity.common.AndroidTestUnit;
import com.microsoft.hydralab.common.entity.common.EntityType;
import com.microsoft.hydralab.common.entity.common.TestRun;
import com.microsoft.hydralab.common.entity.common.EntityFileRelation;
import com.microsoft.hydralab.common.entity.common.TestTask;
import com.microsoft.hydralab.common.repository.AndroidTestUnitRepository;
import com.microsoft.hydralab.common.repository.TestRunRepository;
Expand Down Expand Up @@ -44,7 +44,7 @@ public void saveTestTaskData(TestTask testTask) {

List<AndroidTestUnit> list = new ArrayList<>();
for (TestRun testRun : testRuns) {
attachmentService.saveRelations(testRun.getId(), EntityFileRelation.EntityType.TEST_RESULT, testRun.getAttachments());
attachmentService.saveRelations(testRun.getId(), EntityType.TEST_RESULT, testRun.getAttachments());

List<AndroidTestUnit> testUnitList = testRun.getTestUnitList();
list.addAll(testUnitList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public void onDeviceOffline(TestTask testTask) {

private StorageFileInfo saveFileToBlob(File file, File folder, Logger logger) {
StorageFileInfo storageFileInfo = new StorageFileInfo(file, "test/result/" + folder.getParentFile().getName() + "/" + folder.getName(), StorageFileInfo.FileType.COMMON_FILE);
return attachmentService.addFileInfo(storageFileInfo, file, EntityFileRelation.EntityType.TEST_RESULT, logger);
return attachmentService.addFileInfo(storageFileInfo, file, EntityType.TEST_RESULT, logger);
}

private void processAndSaveDeviceTestResultBlobUrl(TestRun result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public Result add(@CurrentSecurityContext SysUser requestor,
File tempAppFile = attachmentService.verifyAndSaveFile(appFile, CENTER_FILE_BASE_DIR + relativePath, false, null, new String[]{FILE_SUFFIX.APK_FILE, FILE_SUFFIX.IPA_FILE});
StorageFileInfo appBlobFile = new StorageFileInfo(tempAppFile, relativePath, StorageFileInfo.FileType.APP_FILE);
//Upload app file
appBlobFile = attachmentService.addAttachment(testFileSet.getId(), EntityFileRelation.EntityType.APP_FILE_SET, appBlobFile, tempAppFile, logger);
appBlobFile = attachmentService.addAttachment(testFileSet.getId(), EntityType.APP_FILE_SET, appBlobFile, tempAppFile, logger);
JSONObject appFileParser = appBlobFile.getFileParser();
testFileSet.setAppName(appFileParser.getString(ParserKey.APP_NAME));
testFileSet.setPackageName(appFileParser.getString(ParserKey.PKG_NAME));
Expand All @@ -123,7 +123,7 @@ public Result add(@CurrentSecurityContext SysUser requestor,

StorageFileInfo testAppBlobFile = new StorageFileInfo(tempTestAppFile, relativePath, StorageFileInfo.FileType.TEST_APP_FILE);
//Upload app file
testAppBlobFile = attachmentService.addAttachment(testFileSet.getId(), EntityFileRelation.EntityType.APP_FILE_SET, testAppBlobFile, tempTestAppFile, logger);
testAppBlobFile = attachmentService.addAttachment(testFileSet.getId(), EntityType.APP_FILE_SET, testAppBlobFile, tempTestAppFile, logger);
testFileSet.getAttachments().add(testAppBlobFile);
}

Expand Down Expand Up @@ -214,7 +214,7 @@ public Result uploadAgentPackage(@RequestParam("packageFile") MultipartFile pack
try {
File savedPkg = attachmentService.verifyAndSaveFile(packageFile, parentDir, false, null, new String[]{FILE_SUFFIX.JAR_FILE});
StorageFileInfo storageFileInfo = new StorageFileInfo(savedPkg, fileRelativePath, StorageFileInfo.FileType.AGENT_PACKAGE);
return Result.ok(attachmentService.addFileInfo(storageFileInfo, savedPkg, EntityFileRelation.EntityType.AGENT_PACKAGE, logger));
return Result.ok(attachmentService.addFileInfo(storageFileInfo, savedPkg, EntityType.AGENT_PACKAGE, logger));
} catch (HydraLabRuntimeException e) {
return Result.error(e.getCode(), e);
} catch (Exception e) {
Expand Down Expand Up @@ -270,7 +270,7 @@ public Result uploadTestJson(@CurrentSecurityContext SysUser requestor,
String blobPath = fileRelativePath + "/" + savedJson.getName();
testJsonInfo.setBlobPath(blobPath);

return Result.ok(attachmentService.addTestJsonFile(testJsonInfo, savedJson, EntityFileRelation.EntityType.TEST_JSON, logger));
return Result.ok(attachmentService.addTestJsonFile(testJsonInfo, savedJson, EntityType.TEST_JSON, logger));
} catch (HydraLabRuntimeException e) {
return Result.error(e.getCode(), e);
} catch (Exception e) {
Expand Down Expand Up @@ -387,8 +387,8 @@ public Result addAttachment(@CurrentSecurityContext SysUser requestor,

File savedAttachment = attachmentService.verifyAndSaveFile(attachment, parentDir, false, newFileName, limitFileTypes);
StorageFileInfo storageFileInfo = new StorageFileInfo(savedAttachment, fileRelativePath, fileType, loadType, loadDir);
attachmentService.addAttachment(fileSetId, EntityFileRelation.EntityType.APP_FILE_SET, storageFileInfo, savedAttachment, logger);
testFileSet.setAttachments(attachmentService.getAttachments(fileSetId, EntityFileRelation.EntityType.APP_FILE_SET));
attachmentService.addAttachment(fileSetId, EntityType.APP_FILE_SET, storageFileInfo, savedAttachment, logger);
testFileSet.setAttachments(attachmentService.getAttachments(fileSetId, EntityType.APP_FILE_SET));
testFileSetService.saveFileSetToMem(testFileSet);
return Result.ok(testFileSet);
} catch (HydraLabRuntimeException e) {
Expand Down Expand Up @@ -420,8 +420,8 @@ public Result removeAttachment(@CurrentSecurityContext SysUser requestor,
return Result.error(HttpStatus.UNAUTHORIZED.value(), "Unauthorized, the TestFileSet doesn't belong to user's Teams");
}

attachmentService.removeAttachment(fileSetId, EntityFileRelation.EntityType.APP_FILE_SET, fileId);
testFileSet.setAttachments(attachmentService.getAttachments(fileSetId, EntityFileRelation.EntityType.APP_FILE_SET));
attachmentService.removeAttachment(fileSetId, EntityType.APP_FILE_SET, fileId);
testFileSet.setAttachments(attachmentService.getAttachments(fileSetId, EntityType.APP_FILE_SET));
testFileSetService.saveFileSetToMem(testFileSet);
return Result.ok(testFileSet);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public TestTask getTestTaskDetail(String testId) {
for (TestRun deviceTestResult : byTestTaskId) {
List<AndroidTestUnit> byDeviceTestResultId = androidTestUnitRepository.findByDeviceTestResultId(deviceTestResult.getId());
deviceTestResult.getTestUnitList().addAll(byDeviceTestResultId);
deviceTestResult.setAttachments(attachmentService.getAttachments(deviceTestResult.getId(), EntityFileRelation.EntityType.TEST_RESULT));
deviceTestResult.setAttachments(attachmentService.getAttachments(deviceTestResult.getId(), EntityType.TEST_RESULT));
}
return testTask;
}
Expand Down Expand Up @@ -185,7 +185,7 @@ public TestTask saveTestTaskData(TestTask testTask) {

List<AndroidTestUnit> list = new ArrayList<>();
for (TestRun deviceTestResult : deviceTestResults) {
attachmentService.saveAttachments(deviceTestResult.getId(), EntityFileRelation.EntityType.TEST_RESULT, deviceTestResult.getAttachments());
attachmentService.saveAttachments(deviceTestResult.getId(), EntityType.TEST_RESULT, deviceTestResult.getAttachments());

List<AndroidTestUnit> testUnitList = deviceTestResult.getTestUnitList();
list.addAll(testUnitList);
Expand Down Expand Up @@ -216,7 +216,7 @@ public TestRun getTestRunWithVideoInfo(String dttId) {
JSONArray deviceTestResInfo = keyValueRepository.getDeviceTestResInfo(dttId);
testRun.setVideoTimeTagArr(deviceTestResInfo);
testRun.setVideoBlobUrl();
testRun.setAttachments(attachmentService.getAttachments(dttId, EntityFileRelation.EntityType.TEST_RESULT));
testRun.setAttachments(attachmentService.getAttachments(dttId, EntityType.TEST_RESULT));
return testRun;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package com.microsoft.hydralab.center.service;

import com.microsoft.hydralab.common.entity.common.CriteriaType;
import com.microsoft.hydralab.common.entity.common.EntityFileRelation;
import com.microsoft.hydralab.common.entity.common.EntityType;
import com.microsoft.hydralab.common.entity.common.TestFileSet;
import com.microsoft.hydralab.common.repository.TestFileSetRepository;
import com.microsoft.hydralab.common.util.AttachmentService;
Expand Down Expand Up @@ -45,7 +45,7 @@ public synchronized TestFileSet getFileSetInfo(String fileSetId) {
}
testFileSet = testFileSetRepository.findById(fileSetId).orElse(null);
if (testFileSet != null) {
testFileSet.setAttachments(attachmentService.getAttachments(fileSetId, EntityFileRelation.EntityType.APP_FILE_SET));
testFileSet.setAttachments(attachmentService.getAttachments(fileSetId, EntityType.APP_FILE_SET));
saveFileSetToMem(testFileSet);
}
return testFileSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,4 @@ public EntityFileRelation(String entityId, String entityType, String fileId) {
this.entityType = entityType;
this.fileId = fileId;
}

public enum EntityType {
APP_FILE_SET("FILE_SET", DeviceNetworkBlobConstants.PKG_BLOB_NAME),
TEST_RESULT("TEST_RES", DeviceNetworkBlobConstants.TEST_RESULT_BLOB_NAME),
AGENT_PACKAGE("AGENT_PKG", DeviceNetworkBlobConstants.PKG_BLOB_NAME),
TEST_JSON("TEST_JSON", DeviceNetworkBlobConstants.TEST_JSON);

public String typeName;
public String blobConstant;

EntityType(String typeName, String blobConstant) {
this.typeName = typeName;
this.blobConstant = blobConstant;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package com.microsoft.hydralab.common.entity.common;

import com.microsoft.hydralab.common.util.blob.DeviceNetworkBlobConstants;

public enum EntityType {
SCREENSHOT("SCREENSHOT", DeviceNetworkBlobConstants.SCREENSHOT_CONTAINER_NAME),
APP_FILE_SET("FILE_SET", DeviceNetworkBlobConstants.APP_FILE_CONTAINER_NAME),
TEST_RESULT("TEST_RES", DeviceNetworkBlobConstants.TEST_RESULT_CONTAINER_NAME),
AGENT_PACKAGE("AGENT_PKG", DeviceNetworkBlobConstants.AGENT_PACKAGE_CONTAINER_NAME),
TEST_JSON("TEST_JSON", DeviceNetworkBlobConstants.TEST_JSON_CONTAINER_NAME);

public String typeName;
public String blobConstant;

EntityType(String typeName, String blobConstant) {
this.typeName = typeName;
this.blobConstant = blobConstant;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public File getScreenShot(DeviceInfo deviceInfo, Logger logger) throws Exception
deviceInfo.setScreenshotUpdateTimeMilli(System.currentTimeMillis());
sendKeyEvent(deviceInfo, KEYCODE_WAKEUP, logger);
screenCapture(deviceInfo, screenshotImageFile.getAbsolutePath(), null);
String blobUrl = blobStorageClient.uploadBlobFromFile(screenshotImageFile, DeviceNetworkBlobConstants.IMAGES_BLOB_NAME, "device/screenshots/" + screenshotImageFile.getName(), null);
String blobUrl = blobStorageClient.uploadBlobFromFile(screenshotImageFile, DeviceNetworkBlobConstants.SCREENSHOT_CONTAINER_NAME, "device/screenshots/" + screenshotImageFile.getName(), null);
if (StringUtils.isBlank(blobUrl)) {
classLogger.warn("blobUrl is empty for device {}", deviceInfo.getName());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public File getScreenShot(DeviceInfo deviceInfo, Logger logger) throws Exception
}
IOSUtils.takeScreenshot(deviceInfo.getSerialNum(), screenshotImageFile.getAbsolutePath(), classLogger);
deviceInfo.setScreenshotUpdateTimeMilli(System.currentTimeMillis());
String blobUrl = blobStorageClient.uploadBlobFromFile(screenshotImageFile, DeviceNetworkBlobConstants.IMAGES_BLOB_NAME, "device/screenshots/" + screenshotImageFile.getName(), null);
String blobUrl = blobStorageClient.uploadBlobFromFile(screenshotImageFile, DeviceNetworkBlobConstants.SCREENSHOT_CONTAINER_NAME, "device/screenshots/" + screenshotImageFile.getName(), null);
if (StringUtils.isBlank(blobUrl)) {
classLogger.warn("blobUrl is empty for device {}", deviceInfo.getName());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public File getScreenShot(DeviceInfo deviceInfo, Logger logger) throws Exception
} catch (IOException e) {
classLogger.error("Screen capture failed for device: {}", deviceInfo, e);
}
String blobUrl = blobStorageClient.uploadBlobFromFile(pcScreenShotImageFile, DeviceNetworkBlobConstants.IMAGES_BLOB_NAME, "device/screenshots/" + pcScreenShotImageFile.getName(), null);
String blobUrl = blobStorageClient.uploadBlobFromFile(pcScreenShotImageFile, DeviceNetworkBlobConstants.SCREENSHOT_CONTAINER_NAME, "device/screenshots/" + pcScreenShotImageFile.getName(), null);
if (StringUtils.isBlank(blobUrl)) {
classLogger.warn("blobUrl is empty for device {}", deviceInfo.getName());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// Licensed under the MIT License.
package com.microsoft.hydralab.common.util;


import com.microsoft.hydralab.common.entity.common.StorageFileInfo;
import com.microsoft.hydralab.common.entity.common.CriteriaType;
import com.microsoft.hydralab.common.entity.common.EntityFileRelation;
import com.microsoft.hydralab.common.entity.common.EntityType;
import com.microsoft.hydralab.common.entity.common.StorageFileInfo;
import com.microsoft.hydralab.common.entity.common.TestJsonInfo;
import com.microsoft.hydralab.common.repository.StorageFileInfoRepository;
import com.microsoft.hydralab.common.repository.EntityFileRelationRepository;
import com.microsoft.hydralab.common.repository.StorageFileInfoRepository;
import com.microsoft.hydralab.common.repository.TestJsonInfoRepository;
import com.microsoft.hydralab.common.util.blob.BlobStorageClient;
import org.apache.commons.codec.binary.Base64InputStream;
Expand All @@ -26,7 +26,11 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class AttachmentService {
Expand All @@ -40,7 +44,7 @@ public class AttachmentService {
BlobStorageClient blobStorageClient;


public StorageFileInfo addAttachment(String entityId, EntityFileRelation.EntityType entityType, StorageFileInfo storageFileInfo, File file, Logger logger) {
public StorageFileInfo addAttachment(String entityId, EntityType entityType, StorageFileInfo storageFileInfo, File file, Logger logger) {
boolean flag = false;
List<StorageFileInfo> tempFileInfos = storageFileInfoRepository.queryStorageFileInfoByMd5(storageFileInfo.getMd5());
for (StorageFileInfo tempFileInfo : tempFileInfos) {
Expand All @@ -58,12 +62,12 @@ public StorageFileInfo addAttachment(String entityId, EntityFileRelation.EntityT
return storageFileInfo;
}

public void removeAttachment(String entityId, EntityFileRelation.EntityType entityType, String fileId) {
public void removeAttachment(String entityId, EntityType entityType, String fileId) {
EntityFileRelation entityFileRelation = new EntityFileRelation(entityId, entityType.typeName, fileId);
entityFileRelationRepository.delete(entityFileRelation);
}

public StorageFileInfo addFileInfo(StorageFileInfo storageFileInfo, File file, EntityFileRelation.EntityType entityType, Logger logger) {
public StorageFileInfo addFileInfo(StorageFileInfo storageFileInfo, File file, EntityType entityType, Logger logger) {
storageFileInfo.setFileId(UUID.randomUUID().toString());
storageFileInfo.setFileParser(PkgUtil.analysisFile(file, entityType));
storageFileInfo.setCreateTime(new Date());
Expand All @@ -75,7 +79,7 @@ public StorageFileInfo addFileInfo(StorageFileInfo storageFileInfo, File file, E
return storageFileInfo;
}

public TestJsonInfo addTestJsonFile(TestJsonInfo testJsonInfo, File file, EntityFileRelation.EntityType entityType, Logger logger) {
public TestJsonInfo addTestJsonFile(TestJsonInfo testJsonInfo, File file, EntityType entityType, Logger logger) {
testJsonInfo.setBlobUrl(saveFileToBlob(file, entityType.blobConstant, testJsonInfo.getBlobPath(), logger));
testJsonInfo.setBlobContainer(entityType.blobConstant);
List<TestJsonInfo> oldJsonInfoList = testJsonInfoRepository.findByIsLatestAndPackageNameAndCaseName(true, testJsonInfo.getPackageName(), testJsonInfo.getCaseName());
Expand Down Expand Up @@ -103,8 +107,7 @@ public List<TestJsonInfo> getLatestTestJsonList(List<CriteriaType> queryParams)
spec = new CriteriaTypeUtil<TestJsonInfo>().transferToSpecification(queryParams, true);

testJsonInfoList = testJsonInfoRepository.findAll(spec);
}
else {
} else {
testJsonInfoList = testJsonInfoRepository.findByIsLatest(true);
}

Expand Down Expand Up @@ -137,7 +140,7 @@ public boolean compareFileInfo(StorageFileInfo newFileInfo, StorageFileInfo oldF
return false;
}

public StorageFileInfo updateFileInfo(StorageFileInfo oldFileInfo, File file, EntityFileRelation.EntityType entityType, Logger logger) {
public StorageFileInfo updateFileInfo(StorageFileInfo oldFileInfo, File file, EntityType entityType, Logger logger) {
int days = (int) ((new Date().getTime() - oldFileInfo.getUpdateTime().getTime()) / 1000 / 60 / 60 / 24);
if (days >= blobStorageClient.fileLimitDay) {
oldFileInfo.setBlobUrl(saveFileToBlob(file, entityType.blobConstant, oldFileInfo.getBlobPath(), logger));
Expand All @@ -150,7 +153,7 @@ public StorageFileInfo updateFileInfo(StorageFileInfo oldFileInfo, File file, En
}


public List<StorageFileInfo> getAttachments(String entityId, EntityFileRelation.EntityType entityType) {
public List<StorageFileInfo> getAttachments(String entityId, EntityType entityType) {
List<StorageFileInfo> result = new ArrayList<>();

List<EntityFileRelation> fileRelations = entityFileRelationRepository.queryAllByEntityIdAndAndEntityType(entityId, entityType.typeName);
Expand All @@ -163,15 +166,15 @@ public List<StorageFileInfo> getAttachments(String entityId, EntityFileRelation.
return result;
}

public void saveRelation(String entityId, EntityFileRelation.EntityType entityType, StorageFileInfo storageFileInfo) {
public void saveRelation(String entityId, EntityType entityType, StorageFileInfo storageFileInfo) {
EntityFileRelation entityFileRelation = new EntityFileRelation();
entityFileRelation.setEntityId(entityId);
entityFileRelation.setEntityType(entityType.typeName);
entityFileRelation.setFileId(storageFileInfo.getFileId());
entityFileRelationRepository.save(entityFileRelation);
}

public void saveRelations(String entityId, EntityFileRelation.EntityType entityType, List<StorageFileInfo> attachments) {
public void saveRelations(String entityId, EntityType entityType, List<StorageFileInfo> attachments) {
if (attachments == null) {
return;
}
Expand All @@ -186,7 +189,7 @@ public void saveRelations(String entityId, EntityFileRelation.EntityType entityT
entityFileRelationRepository.saveAll(relations);
}

public void saveAttachments(String entityId, EntityFileRelation.EntityType entityType, List<StorageFileInfo> attachments) {
public void saveAttachments(String entityId, EntityType entityType, List<StorageFileInfo> attachments) {
if (attachments == null || attachments.size() == 0) {
return;
}
Expand Down
Loading

0 comments on commit 17d8e39

Please sign in to comment.