Browse Source

tests: Redis test suite implemented on testcontainers with valid parameters initialization by System.setProperty

pull/8149/head
Sergey Matvienko 4 years ago
parent
commit
47ed9e0541
  1. 2
      dao/src/main/java/org/thingsboard/server/dao/attributes/CachedAttributesService.java
  2. 51
      dao/src/test/java/org/thingsboard/server/dao/AbstractRedisContainer.java
  3. 24
      dao/src/test/java/org/thingsboard/server/dao/RedisSqlTestSuite.java
  4. 3
      dao/src/test/resources/application-test.properties

2
dao/src/main/java/org/thingsboard/server/dao/attributes/CachedAttributesService.java

@ -66,7 +66,7 @@ public class CachedAttributesService implements AttributesService {
private final TbTransactionalCache<AttributeCacheKey, AttributeKvEntry> cache;
private ListeningExecutorService cacheExecutor;
@Value("${cache.type}")
@Value("${cache.type:caffeine}")
private String cacheType;
public CachedAttributesService(AttributesDao attributesDao,

51
dao/src/test/java/org/thingsboard/server/dao/AbstractRedisContainer.java

@ -0,0 +1,51 @@
/**
* Copyright © 2016-2023 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.dao;
import lombok.extern.slf4j.Slf4j;
import org.junit.ClassRule;
import org.junit.rules.ExternalResource;
import org.testcontainers.containers.GenericContainer;
import java.util.List;
@Slf4j
public class AbstractRedisContainer {
@ClassRule(order = 0)
public static GenericContainer redis = new GenericContainer("redis:7.0")
.withExposedPorts(6379);
@ClassRule(order = 1)
public static ExternalResource resource = new ExternalResource() {
@Override
protected void before() throws Throwable {
redis.start();
System.setProperty("cache.type", "redis");
System.setProperty("redis.connection.type", "standalone");
System.setProperty("redis.standalone.host", redis.getHost());
System.setProperty("redis.standalone.port", String.valueOf(redis.getMappedPort(6379)));
}
@Override
protected void after() {
redis.stop();
List.of("cache.type", "redis.connection.type", "redis.standalone.host", "redis.standalone.port")
.forEach(System.getProperties()::remove);
}
};
}

24
dao/src/test/java/org/thingsboard/server/dao/RedisSqlTestSuite.java

@ -15,37 +15,15 @@
*/
package org.thingsboard.server.dao;
import org.junit.ClassRule;
import org.junit.extensions.cpsuite.ClasspathSuite;
import org.junit.extensions.cpsuite.ClasspathSuite.ClassnameFilters;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.testcontainers.containers.GenericContainer;
@ContextConfiguration(initializers = RedisSqlTestSuite.class)
@RunWith(ClasspathSuite.class)
@ClassnameFilters(
//All the same tests using redis instead of caffeine.
"org.thingsboard.server.dao.service.*ServiceSqlTest"
)
public class RedisSqlTestSuite implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@ClassRule
public static GenericContainer redis = new GenericContainer("redis:4.0").withExposedPorts(6379);
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
applicationContext, "cache.type=redis");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
applicationContext, "redis.connection.type=standalone");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
applicationContext, "redis.standalone.host=localhost");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
applicationContext, "redis.standalone.port=" + redis.getMappedPort(6379));
}
public class RedisSqlTestSuite extends AbstractRedisContainer {
}

3
dao/src/test/resources/application-test.properties

@ -7,10 +7,9 @@ updates.enabled=false
audit-log.enabled=true
audit-log.sink.type=none
cache.type=caffeine
#cache.type=caffeine # will be injected redis by RedisContainer or will be default (caffeine)
cache.maximumPoolSize=16
cache.attributes.enabled=true
#cache.type=redis
cache.specs.relations.timeToLiveInMinutes=1440
cache.specs.relations.maxSize=100000

Loading…
Cancel
Save