Skip to content

Commit

Permalink
Merge pull request #5383 from smatvienko-tb/sql_tests_on_postgres_tes…
Browse files Browse the repository at this point in the history
…tcontainer

[3.3.3] dao sql tests: run on postgresql container with in-memory disk using testcontainers jdbc (jdbc:tc:postgresql:12.8)
  • Loading branch information
ikulikov authored Dec 17, 2021
2 parents cf2d8c6 + 56cc5a5 commit adc3ef5
Show file tree
Hide file tree
Showing 35 changed files with 296 additions and 484 deletions.
9 changes: 7 additions & 2 deletions application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>jdbc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = CaffeineCacheDefaultConfigurationTestSuite.class, loader = SpringBootContextLoader.class)
@ContextConfiguration(classes = CaffeineCacheDefaultConfigurationTest.class, loader = SpringBootContextLoader.class)
@ComponentScan({"org.thingsboard.server.cache"})
@EnableConfigurationProperties
@Slf4j
public class CaffeineCacheDefaultConfigurationTestSuite {
public class CaffeineCacheDefaultConfigurationTest {

@Autowired
CaffeineCacheConfiguration caffeineCacheConfiguration;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright © 2016-2021 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.controller;

import lombok.extern.slf4j.Slf4j;
import org.junit.After;
import org.junit.Before;
import org.thingsboard.server.queue.memory.InMemoryStorage;

@Slf4j
public abstract class AbstractInMemoryStorageTest {

@Before
public void setUpInMemoryStorage() {
log.info("set up InMemoryStorage");
cleanupInMemStorage();
}

@After
public void tearDownInMemoryStorage() {
log.info("tear down InMemoryStorage");
cleanupInMemStorage();
}

public static void cleanupInMemStorage() {
InMemoryStorage.getInstance().cleanup();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import io.jsonwebtoken.Header;
import io.jsonwebtoken.Jwt;
import io.jsonwebtoken.Jwts;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -59,18 +58,20 @@
import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration;
import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileTransportConfiguration;
import org.thingsboard.server.common.data.device.profile.DeviceProfileData;
import org.thingsboard.server.common.data.edge.Edge;
import org.thingsboard.server.common.data.device.profile.DeviceProfileTransportConfiguration;
import org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration;
import org.thingsboard.server.common.data.device.profile.MqttTopics;
import org.thingsboard.server.common.data.device.profile.ProtoTransportPayloadConfiguration;
import org.thingsboard.server.common.data.device.profile.TransportPayloadTypeConfiguration;
import org.thingsboard.server.common.data.edge.Edge;
import org.thingsboard.server.common.data.id.HasId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.common.data.page.TimePageLink;
import org.thingsboard.server.common.data.security.Authority;
import org.thingsboard.server.config.ThingsboardSecurityConfiguration;
import org.thingsboard.server.dao.tenant.TenantProfileService;
import org.thingsboard.server.service.mail.TestMailService;
import org.thingsboard.server.service.security.auth.jwt.RefreshTokenRequest;
import org.thingsboard.server.service.security.auth.rest.LoginRequest;
Expand All @@ -81,6 +82,7 @@
import java.util.Comparator;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
Expand All @@ -93,7 +95,7 @@
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

@Slf4j
public abstract class AbstractWebTest {
public abstract class AbstractWebTest extends AbstractInMemoryStorageTest {

protected ObjectMapper mapper = new ObjectMapper();

Expand Down Expand Up @@ -132,6 +134,9 @@ public abstract class AbstractWebTest {
@Autowired
private WebApplicationContext webApplicationContext;

@Autowired
private TenantProfileService tenantProfileService;

@Rule
public TestRule watcher = new TestWatcher() {
protected void starting(Description description) {
Expand Down Expand Up @@ -161,8 +166,9 @@ void setConverters(HttpMessageConverter<?>[] converters) {
}

@Before
public void setup() throws Exception {
log.info("Executing setup");
public void setupWebTest() throws Exception {
log.info("Executing web test setup");

if (this.mockMvc == null) {
this.mockMvc = webAppContextSetup(webApplicationContext)
.apply(springSecurity()).build();
Expand Down Expand Up @@ -197,16 +203,38 @@ public void setup() throws Exception {

logout();

log.info("Executed setup");
log.info("Executed web test setup");
}

@After
public void teardown() throws Exception {
log.info("Executing teardown");
public void teardownWebTest() throws Exception {
log.info("Executing web test teardown");

loginSysAdmin();
doDelete("/api/tenant/" + tenantId.getId().toString())
.andExpect(status().isOk());
log.info("Executed teardown");

verifyNoTenantsLeft();

tenantProfileService.deleteTenantProfiles(TenantId.SYS_TENANT_ID);

log.info("Executed web test teardown");
}

void verifyNoTenantsLeft() throws Exception {
List<Tenant> loadedTenants = new ArrayList<>();
PageLink pageLink = new PageLink(10);
PageData<Tenant> pageData;
do {
pageData = doGetTypedWithPageLink("/api/tenants?", new TypeReference<PageData<Tenant>>() {
}, pageLink);
loadedTenants.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageLink.nextPageLink();
}
} while (pageData.hasNext());

assertThat(loadedTenants).as("All tenants expected to be deleted, but some tenants left in the database").isEmpty();
}

protected void loginSysAdmin() throws Exception {
Expand Down Expand Up @@ -570,6 +598,7 @@ protected static <T> ResultMatcher statusReason(Matcher<T> matcher) {
protected Edge constructEdge(String name, String type) {
return constructEdge(tenantId, name, type);
}

protected Edge constructEdge(TenantId tenantId, String name, String type) {
Edge edge = new Edge();
edge.setTenantId(tenantId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ public abstract class BaseTenantProfileControllerTest extends AbstractController
@Autowired
private TenantProfileService tenantProfileService;

@After
@Override
public void teardown() throws Exception {
super.teardown();
tenantProfileService.deleteTenantProfiles(TenantId.SYS_TENANT_ID);
}

@Test
public void testSaveTenantProfile() throws Exception {
loginSysAdmin();
Expand Down
Loading

0 comments on commit adc3ef5

Please sign in to comment.