|
| 1 | +/* |
| 2 | + * matrix-java-sdk - Matrix Client SDK for Java |
| 3 | + * Copyright (C) 2017 Arne Augenstein |
| 4 | + * |
| 5 | + * https://max.kamax.io/ |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Affero General Public License as |
| 9 | + * published by the Free Software Foundation, either version 3 of the |
| 10 | + * License, or (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package io.kamax.matrix.client; |
| 22 | + |
| 23 | +import org.hamcrest.core.IsEqual; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.IOException; |
| 28 | +import java.net.URI; |
| 29 | +import java.net.URISyntaxException; |
| 30 | +import java.nio.file.Files; |
| 31 | +import java.nio.file.Paths; |
| 32 | +import java.util.Optional; |
| 33 | + |
| 34 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 35 | +import static org.junit.jupiter.api.Assertions.*; |
| 36 | + |
| 37 | +/* |
| 38 | + * TODO As the spec is outdated, I'm not sure if the error 403 can really happen in these test cases. This class has |
| 39 | + * to be checked for correctness, when matrix's spec is updated. |
| 40 | + */ |
| 41 | +public abstract class AMatrixHttpContentTest extends MatrixHttpTest { |
| 42 | + protected String bodyFilename = "textfile.txt"; |
| 43 | + protected URI address = new URI("mxc://localhost/testpath/" + bodyFilename); |
| 44 | + |
| 45 | + public AMatrixHttpContentTest() throws URISyntaxException { |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void isValid() throws URISyntaxException { |
| 50 | + assertTrue(createContentObject().isValid()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void isValidMissingContentType() throws URISyntaxException { |
| 55 | + assertTrue(createContentObject().isValid()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void isValidContentNotFound() throws URISyntaxException { |
| 60 | + assertFalse(createContentObject().isValid()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void isValidErrorAccessDenied() throws URISyntaxException { |
| 65 | + assertFalse(createContentObject().isValid()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void getType() throws URISyntaxException, IOException { |
| 70 | + assertEquals(Optional.of("text/plain"), createContentObject().getType()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void getTypeMissingContentType() throws URISyntaxException { |
| 75 | + assertEquals(Optional.empty(), createContentObject().getType()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void getTypeErrorContentNotFound() throws URISyntaxException, IOException { |
| 80 | + MatrixHttpContent contentObject = createContentObject(); |
| 81 | + assertFalse(contentObject.isValid()); |
| 82 | + assertThrows(IllegalStateException.class, contentObject::getType); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void getTypeErrorAccessDenied() throws URISyntaxException, IOException { |
| 87 | + MatrixHttpContent contentObject = createContentObject(); |
| 88 | + assertFalse(contentObject.isValid()); |
| 89 | + assertThrows(IllegalStateException.class, createContentObject()::getType); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void getData() throws URISyntaxException, IOException { |
| 94 | + byte[] expectedResult = Files.readAllBytes(Paths.get(ClassLoader |
| 95 | + .getSystemResource("wiremock" + File.separator + "__files" + File.separator + bodyFilename).toURI())); |
| 96 | + assertThat(createContentObject().getData(), IsEqual.equalTo(expectedResult)); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void getDataMissingContentType() throws URISyntaxException, IOException { |
| 101 | + byte[] expectedResult = Files.readAllBytes(Paths.get(ClassLoader |
| 102 | + .getSystemResource("wiremock" + File.separator + "__files" + File.separator + bodyFilename).toURI())); |
| 103 | + assertThat(createContentObject().getData(), IsEqual.equalTo(expectedResult)); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void getDataErrorContentNotFound() throws URISyntaxException, IOException { |
| 108 | + MatrixHttpContent contentObject = createContentObject(); |
| 109 | + assertFalse(contentObject.isValid()); |
| 110 | + assertThrows(IllegalStateException.class, contentObject::getData); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void getDataErrorAccessDenied() throws URISyntaxException, IOException { |
| 115 | + MatrixHttpContent contentObject = createContentObject(); |
| 116 | + assertFalse(contentObject.isValid()); |
| 117 | + assertThrows(IllegalStateException.class, contentObject::getData); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void getFilename() throws URISyntaxException, IOException { |
| 122 | + assertEquals(Optional.of(bodyFilename), createContentObject().getFilename()); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void getFilenameMissingContentType() throws URISyntaxException { |
| 127 | + assertEquals(Optional.empty(), createContentObject().getFilename()); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void getFilenameErrorContentNotFound() throws URISyntaxException, IOException { |
| 132 | + MatrixHttpContent contentObject = createContentObject(); |
| 133 | + assertFalse(contentObject.isValid()); |
| 134 | + assertThrows(IllegalStateException.class, contentObject::getFilename); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void getFilenameErrorAccessDenied() throws URISyntaxException, IOException { |
| 139 | + MatrixHttpContent contentObject = createContentObject(); |
| 140 | + assertFalse(contentObject.isValid()); |
| 141 | + assertThrows(IllegalStateException.class, contentObject::getFilename); |
| 142 | + } |
| 143 | + |
| 144 | + private MatrixHttpContent createContentObject() throws URISyntaxException { |
| 145 | + MatrixClientContext context = getOrCreateClientContext(); |
| 146 | + return new MatrixHttpContent(context, address); |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments