Skip to content
Snippets Groups Projects
Commit 997d0bf8 authored by Martin Weise's avatar Martin Weise
Browse files

added base tests

parent 8d79065f
No related branches found
No related tags found
No related merge requests found
Showing
with 118 additions and 54 deletions
...@@ -10,7 +10,6 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2; ...@@ -10,7 +10,6 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication @SpringBootApplication
@EnableJpaAuditing @EnableJpaAuditing
@ComponentScan(basePackages = {"at.tuwien"})
@EnableJpaRepositories(basePackages = {"at.tuwien.repositories"}) @EnableJpaRepositories(basePackages = {"at.tuwien.repositories"})
@EntityScan(basePackages = {"at.tuwien.entities"}) @EntityScan(basePackages = {"at.tuwien.entities"})
@EnableSwagger2 @EnableSwagger2
......
package at.tuwien.integration;
import org.springframework.test.context.TestPropertySource;
@TestPropertySource(locations = "classpath:application.properties")
public abstract class BaseIntegrationTest {
}
package at.tuwien.integration;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc
public class EndpointTest extends BaseIntegrationTest {
@Test
public void contextLoads() {
//
}
}
package at.tuwien.mapper;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.model.ContainerNetwork;
import com.github.dockerjava.api.model.NetworkSettings;
import lombok.SneakyThrows;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.lang.reflect.Field;
import java.util.Map;
public abstract class BaseMappingTest {
@Configuration
@ComponentScan(basePackages = {"at.tuwien"})
public static class BaseMappingContext {
}
final String CONTAINER_ID = "deadbeef";
final String CONTAINER_NETWORK_IP = "154.234.88.15";
@SneakyThrows
final InspectContainerResponse mockInspectResponse() {
final InspectContainerResponse responseC = new InspectContainerResponse();
final Object response = responseC.getClass().getConstructor().newInstance();
final Field idField = responseC.getClass().getDeclaredField("id");
idField.setAccessible(true);
idField.set(response, CONTAINER_ID);
final Field networkSettingsField = responseC.getClass().getDeclaredField("networkSettings");
networkSettingsField.setAccessible(true);
// define the network and address
final ContainerNetwork networkC = new ContainerNetwork();
final Object network = networkC.getClass().getConstructor().newInstance();
final Field ipField = networkC.getClass().getDeclaredField("ipAddress");
ipField.setAccessible(true);
ipField.set(network, CONTAINER_NETWORK_IP);
final Map<String, ContainerNetwork> map = Map.of("bridge", (ContainerNetwork) network);
// add to network settings
final NetworkSettings settingsC = new NetworkSettings();
final Object settings = settingsC.getClass().getConstructor().newInstance();
final Field networksField = settingsC.getClass().getDeclaredField("networks");
networksField.setAccessible(true);
networksField.set(settings, map);
networkSettingsField.set(response, settings);
return (InspectContainerResponse) response;
}
}
package at.tuwien.mapper;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.InspectContainerResponse;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.when;
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class DatabaseContainerMappingTest extends BaseMappingTest {
@Test
public void inspectContainerResponseToDatabaseContainerMappingTest_succeeds() {
final InspectContainerResponse response = mockInspectResponse();
assertNotNull(response, "response must not be null");
assertEquals(CONTAINER_ID, response.getId());
assertNotNull(response.getNetworkSettings(), "networkSettings must not be null");
assertNotNull(response.getNetworkSettings().getNetworks(), "networkSettings.networks must not be null");
assertNotNull(response.getNetworkSettings().getNetworks().get("bridge"), "networkSettings.networks['bridge'] must not be null");
assertNotNull(response.getNetworkSettings().getNetworks().get("bridge").getIpAddress(), "networkSettings.networks['bridge'].ipAddress must not be null");
assertEquals(CONTAINER_NETWORK_IP, response.getNetworkSettings().getNetworks().get("bridge").getIpAddress());
}
}
#Disable discovery
spring.cloud.discovery.enabled = false
#Disable cloud config and config discovery
spring.cloud.config.discovery.enabled = false
spring.cloud.config.enabled = false
\ No newline at end of file
package at.tuwien.integration;
public class BaseIntegrationTest {
}
package at.tuwien.mapper;
import at.tuwien.entities.DatabaseContainer;
import com.github.dockerjava.api.command.InspectContainerResponse;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(SpringExtension.class)
public class BaseMappingTest {
private final DatabaseContainerMapper databaseContainerMapper;
@Autowired
public BaseMappingTest(DatabaseContainerMapper databaseContainerMapper) {
this.databaseContainerMapper = databaseContainerMapper;
}
@Configuration
@ComponentScan(basePackages = {"at.tuwien.mapper"})
public static class BaseMappingContext {
}
private static final String CONTAINER_ID = "deadbeef";
@Test
public void inspectContainerResponseToDatabaseContainer_succeed() throws NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
final InspectContainerResponse data = new InspectContainerResponse();
final Object instance = data.getClass().getConstructor().newInstance();
final Field idField = data.getClass().getDeclaredField("id");
idField.setAccessible(true);
idField.set(instance, CONTAINER_ID);
final InspectContainerResponse mockResponse = (InspectContainerResponse) instance;
final DatabaseContainer container = databaseContainerMapper.inspectContainerResponseToDatabaseContainer(mockResponse);
assertEquals(CONTAINER_ID, container.getContainerId());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment