Browse Source

Test fix

pull/7222/head
Andrii Shvaika 4 years ago
parent
commit
aa70785a77
  1. 13
      common/cache/src/main/java/org/thingsboard/server/cache/ota/files/BaseFileCacheService.java
  2. 25
      common/cache/src/test/java/org/thingsboard/server/cache/ota/service/BaseFileCacheServiceTest.java

13
common/cache/src/main/java/org/thingsboard/server/cache/ota/files/BaseFileCacheService.java

@ -24,6 +24,7 @@ import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.OtaPackageId;
@ -47,7 +48,7 @@ import java.util.stream.Collectors;
@Slf4j
@RequiredArgsConstructor
@Component
@Service
public class BaseFileCacheService implements FileCacheService {
@Value("${files.temporary_files_directory:}")
@ -64,6 +65,9 @@ public class BaseFileCacheService implements FileCacheService {
if (StringUtils.isEmpty(tmpDir)) {
tmpDir = defaultTmpDir;
}
createTempDirectoryIfNotExist();
cleanDirectory();
log.info("Directory {} with temporary ota files cleaned", tmpDir);
}
@Override
@ -87,13 +91,6 @@ public class BaseFileCacheService implements FileCacheService {
}
}
@EventListener(ApplicationReadyEvent.class)
public void cleanDirectoryWithTemporaryFiles() {
createTempDirectoryIfNotExist();
cleanDirectory();
log.info("Directory {} with temporary ota files cleaned", tmpDir);
}
private void createTempDirectoryIfNotExist() {
File directory = Paths.get(tmpDir, "ota").toFile();
if (!directory.exists()) {

25
common/cache/src/test/java/org/thingsboard/server/cache/ota/service/BaseFileCacheServiceTest.java

@ -17,6 +17,14 @@ package org.thingsboard.server.cache.ota.service;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit4.SpringRunner;
import org.thingsboard.server.cache.ota.files.BaseFileCacheService;
import org.thingsboard.server.common.data.id.OtaPackageId;
@ -24,6 +32,8 @@ import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.util.Objects;
import java.util.UUID;
@ -31,15 +41,20 @@ import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@EnableConfigurationProperties
@ComponentScan({"org.thingsboard.server.cache.ota.files"})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class BaseFileCacheServiceTest {
private final static String PATH = "/home/anastasiia/IdeaProjects/thingsboard/common/cache";
private final static Path PATH = Paths.get(System.getProperty("java.io.tmpdir"), "ota");
private final static String FILE_FILLING = "Hello, testing environment";
private static final int ONE_MEGA_BYTE = 1_000_000;
private final static OtaPackageId OTA_PACKAGE_ID = new OtaPackageId(UUID.randomUUID());
private final static InputStream DATA = new ByteArrayInputStream(FILE_FILLING.getBytes());
BaseFileCacheService baseFileCacheService = new BaseFileCacheService();
@Autowired
private BaseFileCacheService baseFileCacheService;
@Test
void testDataSavingWithNullInputStream() {
@ -54,7 +69,7 @@ class BaseFileCacheServiceTest {
@Test
@SneakyThrows
void testMultiSavingDataToFile() {
File directory = new File(PATH);
File directory = PATH.toFile();
int beginning = Objects.requireNonNull(directory.list()).length;
Thread thread1 = new Thread(() -> baseFileCacheService.findOrLoad(OTA_PACKAGE_ID, () -> DATA));
Thread thread2 = new Thread(() -> baseFileCacheService.findOrLoad(OTA_PACKAGE_ID, () -> DATA));
@ -62,7 +77,7 @@ class BaseFileCacheServiceTest {
thread2.start();
thread1.join();
thread2.join();
File directory1 = new File(PATH);
File directory1 = PATH.toFile();
int ending = Objects.requireNonNull(directory1.list()).length;
assertEquals(1, ending - beginning);
}

Loading…
Cancel
Save