Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit 3021e06

Browse files
author
Thomas Broyer
committed
Add basic unit test for documents
…and fix a bug in duplicate detection. Change-Id: I57dc2ad2e16a9aa9d0163113901a30d3c9b17e19
1 parent abbf1bc commit 3021e06

5 files changed

Lines changed: 213 additions & 13 deletions

File tree

sormas-backend/src/main/java/de/symeda/sormas/backend/document/DocumentFacadeEjb.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public DocumentDto getDocumentByUuid(String uuid) {
5959

6060
@Override
6161
public DocumentDto saveDocument(DocumentDto dto, byte[] content) throws IOException {
62-
Document existingDocument = dto.getUuid() == null ? documentService.getByUuid(dto.getUuid()) : null;
62+
Document existingDocument = dto.getUuid() == null ? null : documentService.getByUuid(dto.getUuid());
6363
if (existingDocument != null) {
6464
// TODO: add exception message
6565
throw new EntityExistsException();

sormas-backend/src/test/java/de/symeda/sormas/backend/AbstractBeanTest.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
/*******************************************************************************
1+
/*
22
* SORMAS® - Surveillance Outbreak Response Management & Analysis System
3-
* Copyright © 2016-2018 Helmholtz-Zentrum für Infektionsforschung GmbH (HZI)
4-
*
3+
* Copyright © 2016-2020 Helmholtz-Zentrum für Infektionsforschung GmbH (HZI)
54
* This program is free software: you can redistribute it and/or modify
65
* it under the terms of the GNU General Public License as published by
76
* the Free Software Foundation, either version 3 of the License, or
87
* (at your option) any later version.
9-
*
108
* This program is distributed in the hope that it will be useful,
119
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1210
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1311
* GNU General Public License for more details.
14-
*
1512
* You should have received a copy of the GNU General Public License
1613
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17-
*******************************************************************************/
14+
*/
1815
package de.symeda.sormas.backend;
1916

2017
import static org.mockito.Mockito.when;
@@ -45,6 +42,7 @@
4542
import de.symeda.sormas.api.disease.DiseaseConfigurationFacade;
4643
import de.symeda.sormas.api.disease.DiseaseFacade;
4744
import de.symeda.sormas.api.docgeneneration.QuarantineOrderFacade;
45+
import de.symeda.sormas.api.document.DocumentFacade;
4846
import de.symeda.sormas.api.epidata.EpiDataFacade;
4947
import de.symeda.sormas.api.event.EventFacade;
5048
import de.symeda.sormas.api.event.EventParticipantFacade;
@@ -97,6 +95,8 @@
9795
import de.symeda.sormas.backend.disease.DiseaseFacadeEjb.DiseaseFacadeEjbLocal;
9896
import de.symeda.sormas.backend.docgeneration.QuarantineOrderFacadeEjb;
9997
import de.symeda.sormas.backend.docgeneration.TemplateEngineService;
98+
import de.symeda.sormas.backend.document.DocumentFacadeEjb;
99+
import de.symeda.sormas.backend.document.DocumentService;
100100
import de.symeda.sormas.backend.epidata.EpiDataFacadeEjb;
101101
import de.symeda.sormas.backend.event.EventFacadeEjb.EventFacadeEjbLocal;
102102
import de.symeda.sormas.backend.event.EventParticipantFacadeEjb.EventParticipantFacadeEjbLocal;
@@ -481,4 +481,12 @@ public QuarantineOrderFacade getQuarantineOrderFacade() {
481481
public BAGExportFacade getBAGExportFacade() {
482482
return getBean(BAGExportFacadeEjb.BAGExportFacadeEjbLocal.class);
483483
}
484+
485+
public DocumentFacade getDocumentFacade() {
486+
return getBean(DocumentFacadeEjb.DocumentFacadeEjbLocal.class);
487+
}
488+
489+
public DocumentService getDocumentService() {
490+
return getBean(DocumentService.class);
491+
}
484492
}

sormas-backend/src/test/java/de/symeda/sormas/backend/TestDataCreator.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
/*******************************************************************************
1+
/*
22
* SORMAS® - Surveillance Outbreak Response Management & Analysis System
3-
* Copyright © 2016-2018 Helmholtz-Zentrum für Infektionsforschung GmbH (HZI)
4-
*
3+
* Copyright © 2016-2020 Helmholtz-Zentrum für Infektionsforschung GmbH (HZI)
54
* This program is free software: you can redistribute it and/or modify
65
* it under the terms of the GNU General Public License as published by
76
* the Free Software Foundation, either version 3 of the License, or
87
* (at your option) any later version.
9-
*
108
* This program is distributed in the hope that it will be useful,
119
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1210
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1311
* GNU General Public License for more details.
14-
*
1512
* You should have received a copy of the GNU General Public License
1613
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17-
*******************************************************************************/
14+
*/
1815
package de.symeda.sormas.backend;
1916

2017
import java.io.IOException;
@@ -47,6 +44,8 @@
4744
import de.symeda.sormas.api.contact.ContactDto;
4845
import de.symeda.sormas.api.contact.ContactReferenceDto;
4946
import de.symeda.sormas.api.disease.DiseaseConfigurationDto;
47+
import de.symeda.sormas.api.document.DocumentDto;
48+
import de.symeda.sormas.api.document.DocumentRelatedEntityType;
5049
import de.symeda.sormas.api.epidata.EpiDataDto;
5150
import de.symeda.sormas.api.event.EventDto;
5251
import de.symeda.sormas.api.event.EventInvestigationStatus;
@@ -1204,6 +1203,26 @@ public void updateDiseaseConfiguration(Disease disease, Boolean active, Boolean
12041203
beanTest.getDiseaseConfigurationFacade().saveDiseaseConfiguration(config);
12051204
}
12061205

1206+
public DocumentDto createDocument(
1207+
UserReferenceDto uploadingUser,
1208+
String name,
1209+
String contentType,
1210+
long size,
1211+
DocumentRelatedEntityType relatedEntityType,
1212+
String relatedEntityUuid,
1213+
byte[] content)
1214+
throws IOException {
1215+
DocumentDto document = DocumentDto.build();
1216+
document.setUploadingUser(uploadingUser);
1217+
document.setName(name);
1218+
document.setContentType(contentType);
1219+
document.setSize(size);
1220+
document.setRelatedEntityType(relatedEntityType);
1221+
document.setRelatedEntityUuid(relatedEntityUuid);
1222+
1223+
return beanTest.getDocumentFacade().saveDocument(document, content);
1224+
}
1225+
12071226
/**
12081227
* Creates a list with {@code count} values of type {@code T}.
12091228
* The list index is given to the {@code valueSupplier} for each value to create.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* SORMAS® - Surveillance Outbreak Response Management & Analysis System
3+
* Copyright © 2016-2020 Helmholtz-Zentrum für Infektionsforschung GmbH (HZI)
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
14+
*/
15+
package de.symeda.sormas.backend.document;
16+
17+
import static org.hamcrest.Matchers.empty;
18+
import static org.hamcrest.Matchers.equalTo;
19+
import static org.hamcrest.Matchers.hasSize;
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertNull;
23+
import static org.junit.Assert.assertThat;
24+
import static org.junit.Assert.assertTrue;
25+
import static org.junit.Assume.assumeNotNull;
26+
import static org.junit.Assume.assumeThat;
27+
28+
import java.io.IOException;
29+
import java.nio.charset.StandardCharsets;
30+
31+
import javax.persistence.EntityExistsException;
32+
33+
import org.junit.Test;
34+
35+
import de.symeda.sormas.api.document.DocumentDto;
36+
import de.symeda.sormas.api.document.DocumentRelatedEntityType;
37+
import de.symeda.sormas.api.user.UserDto;
38+
import de.symeda.sormas.api.utils.DataHelper;
39+
import de.symeda.sormas.backend.AbstractBeanTest;
40+
import de.symeda.sormas.backend.TestDataCreator;
41+
42+
public class DocumentFacadeEjbTest extends AbstractBeanTest {
43+
44+
@Test
45+
public void testDocumentCreation() throws IOException {
46+
TestDataCreator.RDCF rdcf = creator.createRDCF("Region", "District", "Community", "Facility");
47+
UserDto user = creator.createUser(rdcf);
48+
String eventUuid = DataHelper.createUuid();
49+
50+
DocumentDto document = creator.createDocument(
51+
user.toReference(),
52+
"Name.pdf",
53+
"application/pdf",
54+
42L,
55+
DocumentRelatedEntityType.EVENT,
56+
eventUuid,
57+
"content".getBytes(StandardCharsets.UTF_8));
58+
59+
assertNotNull(getDocumentFacade().getDocumentByUuid(document.getUuid()));
60+
61+
assertThat(getDocumentFacade().getDocumentsRelatedToEntity(DocumentRelatedEntityType.EVENT, eventUuid), hasSize(1));
62+
}
63+
64+
@Test
65+
public void testExistingDocument() throws IOException {
66+
TestDataCreator.RDCF rdcf = creator.createRDCF("Region", "District", "Community", "Facility");
67+
UserDto user = creator.createUser(rdcf);
68+
String eventUuid = DataHelper.createUuid();
69+
70+
DocumentDto document = creator.createDocument(
71+
user.toReference(),
72+
"Name.pdf",
73+
"application/pdf",
74+
42L,
75+
DocumentRelatedEntityType.EVENT,
76+
eventUuid,
77+
"content".getBytes(StandardCharsets.UTF_8));
78+
79+
assertEquals(document.getUuid(), getDocumentFacade().isExistingDocument(DocumentRelatedEntityType.EVENT, eventUuid, document.getName()));
80+
assertNull(getDocumentFacade().isExistingDocument(DocumentRelatedEntityType.EVENT, eventUuid, "Some other name.docx"));
81+
}
82+
83+
@Test(expected = EntityExistsException.class)
84+
public void testDuplicate() throws IOException {
85+
TestDataCreator.RDCF rdcf = creator.createRDCF("Region", "District", "Community", "Facility");
86+
UserDto user = creator.createUser(rdcf);
87+
String eventUuid = DataHelper.createUuid();
88+
89+
DocumentDto document = creator.createDocument(
90+
user.toReference(),
91+
"Name.pdf",
92+
"application/pdf",
93+
42L,
94+
DocumentRelatedEntityType.EVENT,
95+
eventUuid,
96+
"content".getBytes(StandardCharsets.UTF_8));
97+
98+
getDocumentFacade().saveDocument(document, "duplicate".getBytes(StandardCharsets.UTF_8));
99+
}
100+
101+
@Test
102+
public void testDocumentDeletion() throws IOException {
103+
TestDataCreator.RDCF rdcf = creator.createRDCF("Region", "District", "Community", "Facility");
104+
UserDto user = creator.createUser(rdcf);
105+
String eventUuid = DataHelper.createUuid();
106+
107+
DocumentDto document = creator.createDocument(
108+
user.toReference(),
109+
"Name.pdf",
110+
"application/pdf",
111+
42L,
112+
DocumentRelatedEntityType.EVENT,
113+
eventUuid,
114+
"content".getBytes(StandardCharsets.UTF_8));
115+
116+
assumeNotNull(getDocumentFacade().getDocumentByUuid(document.getUuid()));
117+
assumeThat(getDocumentFacade().getDocumentsRelatedToEntity(DocumentRelatedEntityType.EVENT, eventUuid), hasSize(1));
118+
assumeThat(
119+
getDocumentFacade().isExistingDocument(DocumentRelatedEntityType.EVENT, eventUuid, document.getName()),
120+
equalTo(document.getUuid()));
121+
122+
getDocumentFacade().deleteDocument(document.getUuid());
123+
124+
Document deleted = getDocumentService().getByUuid(document.getUuid());
125+
assertNotNull(deleted);
126+
assertTrue(deleted.isDeleted());
127+
128+
assertThat(getDocumentFacade().getDocumentsRelatedToEntity(DocumentRelatedEntityType.EVENT, eventUuid), empty());
129+
assertNull(getDocumentFacade().isExistingDocument(DocumentRelatedEntityType.EVENT, eventUuid, document.getName()));
130+
}
131+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* SORMAS® - Surveillance Outbreak Response Management & Analysis System
3+
* Copyright © 2016-2020 Helmholtz-Zentrum für Infektionsforschung GmbH (HZI)
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
14+
*/
15+
package de.symeda.sormas.backend.document;
16+
17+
import java.io.IOException;
18+
import java.nio.charset.StandardCharsets;
19+
20+
import javax.enterprise.inject.Specializes;
21+
22+
@Specializes
23+
public class MockDocumentStorageService extends DocumentStorageService {
24+
25+
@Override
26+
public byte[] read(Document document) throws IOException {
27+
return document.getUuid().getBytes(StandardCharsets.UTF_8);
28+
}
29+
30+
@Override
31+
public void save(Document document, byte[] content) throws IOException {
32+
}
33+
34+
@Override
35+
public void delete(Document document) {
36+
}
37+
38+
@Override
39+
public void cleanupUnsavedDocument(DocumentSaved event) {
40+
41+
}
42+
}

0 commit comments

Comments
 (0)