1 package de.dlr.shepard.context.export;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.mockito.ArgumentMatchers.any;
5 import static org.mockito.ArgumentMatchers.eq;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.mockConstruction;
8 import static org.mockito.Mockito.verify;
9 import static org.mockito.Mockito.when;
10
11 import de.dlr.shepard.auth.security.AuthenticationContext;
12 import de.dlr.shepard.auth.users.entities.User;
13 import de.dlr.shepard.auth.users.services.UserService;
14 import de.dlr.shepard.common.util.DateHelper;
15 import de.dlr.shepard.context.collection.entities.Collection;
16 import de.dlr.shepard.context.collection.entities.DataObject;
17 import de.dlr.shepard.context.collection.services.CollectionService;
18 import de.dlr.shepard.context.collection.services.DataObjectService;
19 import de.dlr.shepard.context.references.basicreference.entities.BasicReference;
20 import de.dlr.shepard.context.references.basicreference.io.BasicReferenceIO;
21 import de.dlr.shepard.context.references.basicreference.services.BasicReferenceService;
22 import de.dlr.shepard.context.references.file.entities.FileReference;
23 import de.dlr.shepard.context.references.file.io.FileReferenceIO;
24 import de.dlr.shepard.context.references.file.services.FileReferenceService;
25 import de.dlr.shepard.context.references.structureddata.entities.StructuredDataReference;
26 import de.dlr.shepard.context.references.structureddata.io.StructuredDataReferenceIO;
27 import de.dlr.shepard.context.references.structureddata.services.StructuredDataReferenceService;
28 import de.dlr.shepard.context.references.timeseriesreference.io.TimeseriesReferenceIO;
29 import de.dlr.shepard.context.references.timeseriesreference.model.TimeseriesReference;
30 import de.dlr.shepard.context.references.timeseriesreference.services.TimeseriesReferenceService;
31 import de.dlr.shepard.context.references.uri.entities.URIReference;
32 import de.dlr.shepard.context.references.uri.services.URIReferenceService;
33 import io.quarkus.test.InjectMock;
34 import io.quarkus.test.component.QuarkusComponentTest;
35 import jakarta.inject.Inject;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.Test;
40
41 @QuarkusComponentTest
42 public class ExportServiceTest {
43
44
45 DateHelper dateHelper = new DateHelper();
46
47 @InjectMock
48 UserService userService;
49
50 @InjectMock
51 CollectionService collectionService;
52
53 @InjectMock
54 DataObjectService dataObjectService;
55
56 @InjectMock
57 BasicReferenceService basicReferenceService;
58
59 @InjectMock
60 TimeseriesReferenceService timeseriesReferenceService;
61
62 @InjectMock
63 FileReferenceService fileReferenceService;
64
65 @InjectMock
66 StructuredDataReferenceService structuredDataReferenceService;
67
68 @InjectMock
69 URIReferenceService uriReferenceService;
70
71 @InjectMock
72 AuthenticationContext authenticationContext;
73
74 @Inject
75 ExportService service;
76
77 private final User user = new User("bob");
78 private final DataObject dataObject = new DataObject() {
79 {
80 setShepardId(25L);
81 setCreatedAt(dateHelper.getDate());
82 setCreatedBy(user);
83 }
84 };
85 private final Collection collection = new Collection() {
86 {
87 setShepardId(15L);
88 setCreatedAt(dateHelper.getDate());
89 setCreatedBy(user);
90 }
91 };
92
93 @BeforeEach
94 void initMocks() {
95 collection.addDataObject(dataObject);
96 dataObject.setCollection(collection);
97
98 when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
99 when(collectionService.getCollectionWithDataObjectsAndIncomingReferences(collection.getShepardId())).thenReturn(
100 collection
101 );
102 when(dataObjectService.getDataObject(dataObject.getShepardId())).thenReturn(dataObject);
103 }
104
105 @Test
106 public void exportTest_basicReference() throws IOException {
107 var reference = hydrateReferenceMock(mock(BasicReference.class), "BasicReference");
108 dataObject.addReference(reference);
109 when(
110 basicReferenceService.getReference(collection.getShepardId(), dataObject.getShepardId(), reference.getShepardId())
111 ).thenReturn(reference);
112
113 var mockStream = mock(InputStream.class);
114 try (
115 var exportBuilderMockController = mockConstruction(ExportBuilder.class, (mock, context) -> {
116 when(mock.build()).thenReturn(mockStream);
117 });
118 ) {
119 var actual = service.exportCollectionByShepardId(collection.getShepardId());
120
121 var exportBuilderMock = exportBuilderMockController.constructed().get(0);
122 verify(exportBuilderMock).addReference(any(BasicReferenceIO.class), eq(user));
123 verify(exportBuilderMock).addDataObject(dataObject);
124
125 assertEquals(1, exportBuilderMockController.constructed().size());
126 assertEquals(mockStream, actual);
127 }
128 }
129
130 @Test
131 public void exportTest_uriReference() throws IOException {
132 var reference = hydrateReferenceMock(mock(URIReference.class), "URIReference");
133 dataObject.addReference(reference);
134 when(
135 uriReferenceService.getReference(
136 collection.getShepardId(),
137 dataObject.getShepardId(),
138 reference.getShepardId(),
139 null
140 )
141 ).thenReturn(reference);
142
143 var mockStream = mock(InputStream.class);
144 try (
145 var exportBuilderMockController = mockConstruction(ExportBuilder.class, (mock, context) -> {
146 when(mock.build()).thenReturn(mockStream);
147 });
148 ) {
149 var actual = service.exportCollectionByShepardId(collection.getShepardId());
150
151 var exportBuilderMock = exportBuilderMockController.constructed().get(0);
152 verify(exportBuilderMock).addReference(any(BasicReferenceIO.class), eq(user));
153 verify(exportBuilderMock).addDataObject(dataObject);
154
155 assertEquals(1, exportBuilderMockController.constructed().size());
156 assertEquals(mockStream, actual);
157 }
158 }
159
160 @Test
161 public void exportTest_timeseriesReference() throws IOException {
162 var reference = hydrateReferenceMock(mock(TimeseriesReference.class), "TimeseriesReference");
163 dataObject.addReference(reference);
164 when(
165 timeseriesReferenceService.getReference(
166 collection.getShepardId(),
167 dataObject.getShepardId(),
168 reference.getShepardId(),
169 null
170 )
171 ).thenReturn(reference);
172
173 var mockStream = mock(InputStream.class);
174 try (
175 var exportBuilderMockController = mockConstruction(ExportBuilder.class, (mock, context) -> {
176 when(mock.build()).thenReturn(mockStream);
177 });
178 ) {
179 var actual = service.exportCollectionByShepardId(collection.getShepardId());
180
181 var exportBuilderMock = exportBuilderMockController.constructed().get(0);
182 verify(exportBuilderMock).addReference(any(TimeseriesReferenceIO.class), eq(user));
183 verify(exportBuilderMock).addDataObject(dataObject);
184
185 assertEquals(1, exportBuilderMockController.constructed().size());
186 assertEquals(mockStream, actual);
187 }
188 }
189
190 @Test
191 public void exportTest_fileReference() throws IOException {
192 var reference = hydrateReferenceMock(mock(FileReference.class), "FileReference");
193 dataObject.addReference(reference);
194
195 when(
196 fileReferenceService.getReference(
197 collection.getShepardId(),
198 dataObject.getShepardId(),
199 reference.getShepardId(),
200 null
201 )
202 ).thenReturn(reference);
203
204 var mockStream = mock(InputStream.class);
205 try (
206 var exportBuilderMockController = mockConstruction(ExportBuilder.class, (mock, context) -> {
207 when(mock.build()).thenReturn(mockStream);
208 });
209 ) {
210 InputStream actual = service.exportCollectionByShepardId(collection.getShepardId());
211
212 var exportBuilderMock = exportBuilderMockController.constructed().get(0);
213 verify(exportBuilderMock).addReference(any(FileReferenceIO.class), eq(user));
214 verify(exportBuilderMock).addDataObject(dataObject);
215
216 assertEquals(1, exportBuilderMockController.constructed().size());
217 assertEquals(mockStream, actual);
218 }
219 }
220
221 @Test
222 public void exportTest_structuredDataReference() throws IOException {
223 var reference = hydrateReferenceMock(mock(StructuredDataReference.class), "StructuredDataReference");
224 dataObject.addReference(reference);
225 when(
226 structuredDataReferenceService.getReference(
227 collection.getShepardId(),
228 dataObject.getShepardId(),
229 reference.getShepardId(),
230 null
231 )
232 ).thenReturn(reference);
233
234 var mockStream = mock(InputStream.class);
235 try (
236 var exportBuilderMockController = mockConstruction(ExportBuilder.class, (mock, context) -> {
237 when(mock.build()).thenReturn(mockStream);
238 });
239 ) {
240 InputStream actual = service.exportCollectionByShepardId(collection.getShepardId());
241
242 var exportBuilderMock = exportBuilderMockController.constructed().get(0);
243 verify(exportBuilderMock).addReference(any(StructuredDataReferenceIO.class), eq(user));
244 verify(exportBuilderMock).addDataObject(dataObject);
245
246 assertEquals(1, exportBuilderMockController.constructed().size());
247 assertEquals(mockStream, actual);
248 }
249 }
250
251 private <T extends BasicReference> T hydrateReferenceMock(T reference, String type) {
252 when(reference.getShepardId()).thenReturn(35L);
253 when(reference.getCreatedBy()).thenReturn(user);
254 when(reference.getCreatedAt()).thenReturn(dateHelper.getDate());
255 when(reference.getDataObject()).thenReturn(dataObject);
256 when(reference.getType()).thenReturn(type);
257 return reference;
258 }
259 }