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.context.event.EventListener;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; 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.EntityId;
import org.thingsboard.server.common.data.id.OtaPackageId; import org.thingsboard.server.common.data.id.OtaPackageId;
@ -47,7 +48,7 @@ import java.util.stream.Collectors;
@Slf4j @Slf4j
@RequiredArgsConstructor @RequiredArgsConstructor
@Component @Service
public class BaseFileCacheService implements FileCacheService { public class BaseFileCacheService implements FileCacheService {
@Value("${files.temporary_files_directory:}") @Value("${files.temporary_files_directory:}")
@ -64,6 +65,9 @@ public class BaseFileCacheService implements FileCacheService {
if (StringUtils.isEmpty(tmpDir)) { if (StringUtils.isEmpty(tmpDir)) {
tmpDir = defaultTmpDir; tmpDir = defaultTmpDir;
} }
createTempDirectoryIfNotExist();
cleanDirectory();
log.info("Directory {} with temporary ota files cleaned", tmpDir);
} }
@Override @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() { private void createTempDirectoryIfNotExist() {
File directory = Paths.get(tmpDir, "ota").toFile(); File directory = Paths.get(tmpDir, "ota").toFile();
if (!directory.exists()) { 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 lombok.SneakyThrows;
import org.junit.jupiter.api.Test; 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.cache.ota.files.BaseFileCacheService;
import org.thingsboard.server.common.data.id.OtaPackageId; import org.thingsboard.server.common.data.id.OtaPackageId;
@ -24,6 +32,8 @@ import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.Objects; import java.util.Objects;
import java.util.UUID; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; 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 { 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 final static String FILE_FILLING = "Hello, testing environment";
private static final int ONE_MEGA_BYTE = 1_000_000; private static final int ONE_MEGA_BYTE = 1_000_000;
private final static OtaPackageId OTA_PACKAGE_ID = new OtaPackageId(UUID.randomUUID()); private final static OtaPackageId OTA_PACKAGE_ID = new OtaPackageId(UUID.randomUUID());
private final static InputStream DATA = new ByteArrayInputStream(FILE_FILLING.getBytes()); private final static InputStream DATA = new ByteArrayInputStream(FILE_FILLING.getBytes());
BaseFileCacheService baseFileCacheService = new BaseFileCacheService();
@Autowired
private BaseFileCacheService baseFileCacheService;
@Test @Test
void testDataSavingWithNullInputStream() { void testDataSavingWithNullInputStream() {
@ -54,7 +69,7 @@ class BaseFileCacheServiceTest {
@Test @Test
@SneakyThrows @SneakyThrows
void testMultiSavingDataToFile() { void testMultiSavingDataToFile() {
File directory = new File(PATH); File directory = PATH.toFile();
int beginning = Objects.requireNonNull(directory.list()).length; int beginning = Objects.requireNonNull(directory.list()).length;
Thread thread1 = new Thread(() -> baseFileCacheService.findOrLoad(OTA_PACKAGE_ID, () -> DATA)); Thread thread1 = new Thread(() -> baseFileCacheService.findOrLoad(OTA_PACKAGE_ID, () -> DATA));
Thread thread2 = 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(); thread2.start();
thread1.join(); thread1.join();
thread2.join(); thread2.join();
File directory1 = new File(PATH); File directory1 = PATH.toFile();
int ending = Objects.requireNonNull(directory1.list()).length; int ending = Objects.requireNonNull(directory1.list()).length;
assertEquals(1, ending - beginning); assertEquals(1, ending - beginning);
} }

Loading…
Cancel
Save