1
|
package cz.zcu.kiv.backendapi.type;
|
2
|
|
3
|
import org.junit.jupiter.api.BeforeEach;
|
4
|
import org.junit.jupiter.api.Test;
|
5
|
import org.junit.jupiter.api.extension.ExtendWith;
|
6
|
import org.mockito.ArgumentCaptor;
|
7
|
import org.mockito.Mock;
|
8
|
import org.mockito.junit.jupiter.MockitoExtension;
|
9
|
|
10
|
import java.util.Set;
|
11
|
import java.util.stream.Collectors;
|
12
|
import java.util.stream.Stream;
|
13
|
|
14
|
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
15
|
import static org.mockito.BDDMockito.given;
|
16
|
import static org.mockito.Mockito.verify;
|
17
|
|
18
|
@ExtendWith(MockitoExtension.class)
|
19
|
class TypeServiceImplTest {
|
20
|
|
21
|
@Mock
|
22
|
private TypeRepository typeRepository;
|
23
|
|
24
|
private TypeServiceImpl underTest;
|
25
|
|
26
|
@BeforeEach
|
27
|
void setUp() {
|
28
|
underTest = new TypeServiceImpl(typeRepository);
|
29
|
}
|
30
|
|
31
|
@Test
|
32
|
void saveType() {
|
33
|
// when
|
34
|
Type type = new Type("type");
|
35
|
underTest.saveType(type);
|
36
|
|
37
|
// then
|
38
|
ArgumentCaptor<Type> argumentCaptor = ArgumentCaptor.forClass(Type.class);
|
39
|
|
40
|
verify(typeRepository).save(argumentCaptor.capture());
|
41
|
|
42
|
Type capturedType = argumentCaptor.getValue();
|
43
|
|
44
|
assertThat(capturedType).isEqualTo(type);
|
45
|
}
|
46
|
|
47
|
@Test
|
48
|
void getTypeByName() {
|
49
|
// when
|
50
|
String typeName = "type";
|
51
|
|
52
|
// given
|
53
|
underTest.getTypeByName(typeName);
|
54
|
|
55
|
// then
|
56
|
verify(typeRepository).findById(typeName);
|
57
|
}
|
58
|
|
59
|
@Test
|
60
|
void getTypesAsString() {
|
61
|
// when
|
62
|
String stringType1 = "type1";
|
63
|
String stringType2 = "type2";
|
64
|
String stringType3 = "type3";
|
65
|
String stringType4 = "type4";
|
66
|
String stringType5 = "type5";
|
67
|
|
68
|
Type type1 = new Type(stringType1);
|
69
|
Type type2 = new Type(stringType2);
|
70
|
Type type3 = new Type(stringType3);
|
71
|
Type type4 = new Type(stringType4);
|
72
|
Type type5 = new Type(stringType5);
|
73
|
|
74
|
// given
|
75
|
given(typeRepository.findAll()).willReturn(Stream.of(type1, type2, type3, type4, type5).collect(Collectors.toList()));
|
76
|
Set<String> res = underTest.getAllTypesAsString();
|
77
|
|
78
|
// then
|
79
|
verify(typeRepository).findAll();
|
80
|
|
81
|
assertThat(res.size()).isEqualTo(5);
|
82
|
|
83
|
assertThat(res.contains(stringType1)).isTrue();
|
84
|
assertThat(res.contains(stringType2)).isTrue();
|
85
|
assertThat(res.contains(stringType3)).isTrue();
|
86
|
assertThat(res.contains(stringType4)).isTrue();
|
87
|
assertThat(res.contains(stringType5)).isTrue();
|
88
|
}
|
89
|
}
|