Skip to content
Snippets Groups Projects
Unverified Commit 8976f8a4 authored by Martin Weise's avatar Martin Weise
Browse files

Added Unit test

parent 5d2340e0
No related branches found
No related tags found
1 merge request!106Dev
...@@ -144,6 +144,7 @@ public abstract class BaseUnitTest { ...@@ -144,6 +144,7 @@ public abstract class BaseUnitTest {
.driverClass(IMAGE_1_DRIVER) .driverClass(IMAGE_1_DRIVER)
.compiled(IMAGE_1_BUILT) .compiled(IMAGE_1_BUILT)
.size(IMAGE_1_SIZE) .size(IMAGE_1_SIZE)
.environment(IMAGE_1_ENV)
.defaultPort(IMAGE_1_PORT) .defaultPort(IMAGE_1_PORT)
.build(); .build();
......
package at.tuwien.service;
import at.tuwien.BaseUnitTest;
import at.tuwien.api.container.ContainerCreateRequestDto;
import at.tuwien.config.ReadyConfig;
import at.tuwien.entities.container.Container;
import at.tuwien.exception.ContainerAlreadyExistsException;
import at.tuwien.exception.DockerClientException;
import at.tuwien.exception.ImageNotFoundException;
import at.tuwien.exception.UserNotFoundException;
import at.tuwien.repository.jpa.ContainerRepository;
import at.tuwien.repository.jpa.ImageRepository;
import at.tuwien.repository.jpa.UserRepository;
import lombok.extern.log4j.Log4j2;
import org.apache.http.auth.BasicUserPrincipal;
import org.junit.jupiter.api.BeforeEach;
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.boot.test.mock.mockito.MockBean;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.security.Principal;
import static org.junit.jupiter.api.Assertions.assertThrows;
@Log4j2
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@SpringBootTest
@ExtendWith(SpringExtension.class)
public class ContainerServiceUnitTest extends BaseUnitTest {
@MockBean
private ReadyConfig readyConfig;
@Autowired
private ContainerRepository containerRepository;
@Autowired
private ImageRepository imageRepository;
@Autowired
private UserRepository userRepository;
@Autowired
private ContainerService containerService;
@BeforeEach
public void beforeEach() {
/* mock data */
userRepository.save(USER_1);
imageRepository.save(IMAGE_1);
containerRepository.save(CONTAINER_1);
}
@Test
public void create_nameExists_fails() {
final ContainerCreateRequestDto request = ContainerCreateRequestDto.builder()
.name(CONTAINER_1_NAME)
.repository(IMAGE_1_REPOSITORY)
.tag(IMAGE_1_TAG)
.build();
final Principal principal = new BasicUserPrincipal(USER_1_USERNAME);
/* test */
assertThrows(ContainerAlreadyExistsException.class, () -> {
containerService.create(request, principal);
});
}
}
...@@ -110,7 +110,7 @@ public class ContainerServiceImpl implements ContainerService { ...@@ -110,7 +110,7 @@ public class ContainerServiceImpl implements ContainerService {
.exec(); .exec();
} catch (ConflictException e) { } catch (ConflictException e) {
log.error("Conflicting names {}, reason: {}", createDto.getName(), e.getMessage()); log.error("Conflicting names {}, reason: {}", createDto.getName(), e.getMessage());
throw new ContainerAlreadyExistsException("Unexpected behavior", e); throw new ContainerAlreadyExistsException("Conflicting names", e);
} catch (NotFoundException e) { } catch (NotFoundException e) {
log.error("The image {}:{} not available on the container service", createDto.getRepository(), log.error("The image {}:{} not available on the container service", createDto.getRepository(),
createDto.getTag()); createDto.getTag());
......
...@@ -61,6 +61,7 @@ public class ContainerImage { ...@@ -61,6 +61,7 @@ public class ContainerImage {
@Column(nullable = false) @Column(nullable = false)
private Integer defaultPort; private Integer defaultPort;
@ToString.Exclude
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "iid", insertable = false, updatable = false) @JoinColumn(name = "iid", insertable = false, updatable = false)
private List<ContainerImageEnvironmentItem> environment; private List<ContainerImageEnvironmentItem> environment;
......
...@@ -46,6 +46,7 @@ public class ContainerImageEnvironmentItem { ...@@ -46,6 +46,7 @@ public class ContainerImageEnvironmentItem {
@Enumerated(EnumType.STRING) @Enumerated(EnumType.STRING)
private ContainerImageEnvironmentItemType type; private ContainerImageEnvironmentItemType type;
@ToString.Exclude
@org.springframework.data.annotation.Transient @org.springframework.data.annotation.Transient
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumns({ @JoinColumns({
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment