|
| 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 | +} |
0 commit comments