1 package io.github.magwas.inez.element;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertThrows;
5 import static org.mockito.Mockito.times;
6 import static org.mockito.Mockito.verify;
7
8 import java.text.MessageFormat;
9 import java.util.List;
10
11 import org.junit.jupiter.api.BeforeEach;
12 import org.junit.jupiter.api.DisplayName;
13 import org.junit.jupiter.api.Test;
14 import org.mockito.ArgumentCaptor;
15 import org.mockito.InjectMocks;
16
17 import io.github.magwas.inez.Bridi;
18 import io.github.magwas.testing.TestBase;
19
20 class CreateBridiElementServiceTest extends TestBase implements BridiElementTestData {
21
22 @InjectMocks
23 CreateBridiElementService createBridiElement;
24
25 private BridiElement element;
26 private List<Bridi> saved;
27
28 @BeforeEach
29 @Override
30 public void setUp() throws Throwable {
31 super.setUp();
32 element = createBridiElement.apply(MY_MODEL_ID, HUMAN_ID, ALICE_REPR);
33 @SuppressWarnings("unchecked")
34 ArgumentCaptor<List<Bridi>> argument = ArgumentCaptor.forClass(List.class);
35 verify(createBridiElement.saveBridi).apply(argument.capture());
36 saved = argument.getValue();
37 }
38
39 @Test
40 @DisplayName("saves 3 bridis")
41 void test0() {
42 assertEquals(3, saved.size());
43 }
44
45 @Test
46 @DisplayName("the first one is the element")
47 void test1() {
48 String elementId = element.id;
49 assertEquals(elementId, saved.get(0).id());
50 }
51
52 @Test
53 @DisplayName("with the given representation")
54 void test2() {
55 assertEquals(ALICE_REPR, saved.get(0).representation());
56 }
57
58 @Test
59 @DisplayName("and the given rerferences")
60 void test3() {
61 assertEquals(List.of(), saved.get(0).references());
62 }
63
64 @Test
65 @DisplayName("the second one is '{@elementId} is a {@typeId}' ")
66 void test4() {
67 assertEquals(List.of(IS_A_ID, element.id, HUMAN_ID), saved.get(1).references());
68 }
69
70 @Test
71 @DisplayName("the representation of the second one uses actual representations")
72 void test4_1() {
73 assertEquals(
74 MessageFormat.format("{0} is a {1}", ALICE_REPR, HUMAN_REPR),
75 saved.get(1).representation());
76 }
77
78 @Test
79 @DisplayName("the third one is '{@containerId} contains {@elementId}' ")
80 void test5() {
81 assertEquals(List.of(CONTAINS_ID, MY_MODEL_ID, element.id), saved.get(2).references());
82 }
83
84 @Test
85 @DisplayName("the representation of the third one uses actual representations")
86 void test5_1() {
87 assertEquals(
88 MessageFormat.format("{0} contains {1}", MY_MODEL_REPR, ALICE_REPR),
89 saved.get(2).representation());
90 }
91
92 @Test
93 @DisplayName("the references are saved")
94 void test6() {
95 createBridiElement.apply(MY_MODEL_ID, HUMAN_ID, ALICE_REPR, IS_A_ID, ALICE_ID, GOD_ID);
96 @SuppressWarnings("unchecked")
97 ArgumentCaptor<List<Bridi>> argument = ArgumentCaptor.forClass(List.class);
98 verify(createBridiElement.saveBridi, times(2)).apply(argument.capture());
99 List<Bridi> saved = argument.getValue();
100 assertEquals(3, saved.size());
101 assertEquals(List.of(IS_A_ID, ALICE_ID, GOD_ID), saved.get(0).references());
102 }
103
104 @Test
105 @DisplayName("if the type does not exist, an IllegalArgumentException is thrown")
106 void test7() {
107 assertThrows(
108 IllegalArgumentException.class,
109 () -> createBridiElement.apply(MY_MODEL_ID, NONEXISTENT_ID, ALICE_REPR, IS_A_ID, ALICE_ID, GOD_ID));
110 }
111
112 @Test
113 @DisplayName("if the container does not exist, an IllegalArgumentException is thrown")
114 void test8() {
115 assertThrows(
116 IllegalArgumentException.class,
117 () -> createBridiElement.apply(NONEXISTENT_ID, HUMAN_ID, ALICE_REPR, IS_A_ID, ALICE_ID, GOD_ID));
118 }
119
120 @Test
121 @DisplayName("if the there is a nonexisting reference, an IllegalArgumentException is thrown")
122 void test9() {
123 assertThrows(
124 IllegalArgumentException.class,
125 () -> createBridiElement.apply(MY_MODEL_ID, HUMAN_ID, ALICE_REPR, NONEXISTENT_ID, ALICE_ID, GOD_ID));
126 }
127 }