PublisherServiceSpringTest.java
4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package org.legrog.web.publisher;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.legrog.entities.*;
import org.legrog.test.MockitoExtension;
import org.legrog.web.xyz.SharedService;
import org.mockito.Mock;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
/**
* Classe testant PublisherServiceSpring.
*/
@RunWith(JUnitPlatform.class)
@ExtendWith(MockitoExtension.class)
@DisplayName("Service layer for all publisher-related data")
public class PublisherServiceSpringTest {
PublisherServiceSpring publisherServiceSpring;
PublisherVersion publisherVersion;
PublisherVersion publisherVersion1;
Publisher publisher;
PublisherRepository publisherRepository;
PublisherVersionRepository publisherVersionRepository;
@Mock
PublisherVersion publisherVersionMock;
@BeforeEach
public void setUp(@Mock PublisherRepository publisherRepository,
@Mock PublisherVersionRepository publisherVersionRepository,
@Mock PublisherActionRepository publisherActionRepository,
@Mock PublisherSearchRepository publisherSearchRepository,
@Mock SharedService sharedService) throws Exception {
publisherServiceSpring = new PublisherServiceSpring(publisherRepository,
publisherVersionRepository, publisherActionRepository, publisherSearchRepository, sharedService);
publisherVersion = new PublisherVersion();
publisherVersion1 = new PublisherVersion();
this.publisherRepository = publisherRepository;
}
@DisplayName("Adds a new publisher by setting it and its first version up and saving both")
@Test
public void testAddNewPublisher(@Mock PublisherVersionRepository publisherVersionRepository) {
publisherServiceSpring.addNewPublisher(publisherVersion);
publisher = publisherVersion.getPublisher();
assertThat(publisher.getVersions()).containsExactly(publisherVersion);
assertThat(publisherVersion.getPublisher()).isEqualTo(publisher);
verify(publisherRepository).save(publisher);
verify(publisherVersionRepository).save(publisherVersion);
}
@DisplayName("Adds a new version of a publisher, setting it up, attaching it to publisher and saving both")
@Test
public void testAddVersionToPublisher(@Mock PublisherRepository publisherRepository,
@Mock PublisherVersionRepository publisherVersionRepository) {
publisherServiceSpring.addNewPublisher(publisherVersion);
publisher = publisherVersion.getPublisher();
publisherServiceSpring.addVersionToPublisher(publisher, publisherVersion1);
assertThat(publisherVersion1.getPublisher()).isEqualTo(publisher);
assertThat(publisher.getVersions()).contains(publisherVersion, publisherVersion1);
verify(publisherRepository, times(2)).save(publisher);
verify(publisherVersionRepository).save(publisherVersion1);
}
@DisplayName("Validates a version by creating a VALIDATE action that links to publisher and version, sets versions in publisher's validated and saves it")
@Test
public void testValidateVersion(@Mock PublisherActionRepository publisherActionRepository) {
Set<PublisherVersion> publisherVersions;
ActionType actionType = ActionType.VALIDATE;
publisher = new Publisher();
publisherVersion = new PublisherVersion();
publisherVersions = new HashSet<PublisherVersion>();
publisherVersions.add(publisherVersion);
publisherVersions.add(publisherVersionMock);
when(publisherVersionMock.getPublisher()).thenReturn(publisher);
PublisherAction publisherAction;
publisherAction = publisherServiceSpring.validatePublisherVersion(publisherVersionMock);
verify(publisherActionRepository).save(publisherAction);
assertThat(publisherAction.getPublisherVersion()).isEqualTo(publisherVersionMock);
assertThat(publisherAction.getPublisher()).isEqualTo(publisher);
assertThat(publisherAction.getPublisher().getValidatedVersion()).isEqualTo(publisherVersionMock);
assertThat(publisherAction.getActionType()).isEqualTo(actionType);
}
}