|
|
|
@ -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); |
|
|
|
} |
|
|
|
|