|
| 1 | +package com.groupdocs.ui.viewer.demosapitests.external; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.groupdocs.ui.viewer.demosapitests.Constants; |
| 5 | +import com.groupdocs.ui.viewer.demosapitests.cases.LoadDocumentDescription; |
| 6 | +import com.groupdocs.ui.viewer.demosapitests.cases.LoadDocumentDescriptionCase; |
| 7 | +import com.groupdocs.ui.viewer.demosapitests.common.Utils; |
| 8 | +import io.restassured.http.ContentType; |
| 9 | +import org.apache.commons.io.FileUtils; |
| 10 | +import org.apache.commons.io.IOUtils; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | +import org.testng.SkipException; |
| 14 | +import org.testng.annotations.DataProvider; |
| 15 | +import org.testng.annotations.Test; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.InputStream; |
| 19 | +import java.net.URL; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.nio.file.Paths; |
| 24 | +import java.util.*; |
| 25 | +import java.util.regex.Matcher; |
| 26 | +import java.util.regex.Pattern; |
| 27 | + |
| 28 | +import static io.restassured.RestAssured.given; |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | + |
| 31 | +public class LoadExternalDocumentDescriptionApiTests { |
| 32 | + public static final int ERROR_MESSAGE_DATA_LENGTH = 128; |
| 33 | + public static final int ERROR_MESSAGE_DATA_LOOK_BACK = 16; |
| 34 | + private static final Logger LOGGER = LoggerFactory.getLogger(LoadExternalDocumentDescriptionApiTests.class); |
| 35 | + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
| 36 | + private final Pattern EXTERNAL_LINK_PATTERN = Pattern.compile("(?:src|href)=[\"']([^\"']+)[\"']"); |
| 37 | + private final List<String> SKIP_RESOURCES_NAMES = Arrays.asList("filelist.xml", "editdata.mso", "oledata.mso"); |
| 38 | + |
| 39 | + @DataProvider(name = "testLoadDocumentDescription_ExternalResources_DataProvider") |
| 40 | + public Object[][] testLoadDocumentDescription_ExternalResources_DataProvider() throws IOException { |
| 41 | + final java.net.URL documentDescriptionResource = this.getClass().getResource(""); |
| 42 | + assertThat(documentDescriptionResource).isNotNull(); |
| 43 | + |
| 44 | + List<Object[]> caseList = new ArrayList<>(); |
| 45 | + |
| 46 | + final Path documentDescriptionDirectory = Paths.get(documentDescriptionResource.getPath().substring(1)); |
| 47 | + |
| 48 | + for (Path filePath : Files.newDirectoryStream(documentDescriptionDirectory, "*.json")) { |
| 49 | +// if (filePath.toString().contains("xps-")) // To run one test |
| 50 | + caseList.add(new Object[]{filePath.toString()}); |
| 51 | + } |
| 52 | + |
| 53 | + return caseList.toArray(new Object[0][]); |
| 54 | + } |
| 55 | + |
| 56 | + @Test(dataProvider = "testLoadDocumentDescription_ExternalResources_DataProvider") |
| 57 | + public void testLoadDocumentDescription(String caseFile) throws IOException { |
| 58 | + if (caseFile.contains("eml") || caseFile.contains("msg")) { |
| 59 | + throw new SkipException("ASPOSEJAVA-166"); |
| 60 | + } |
| 61 | + |
| 62 | + LoadDocumentDescription expectedObject = null; |
| 63 | + LoadDocumentDescription actualObject = null; |
| 64 | + final Path caseFilePath = Paths.get(caseFile); |
| 65 | + try { |
| 66 | + final LoadDocumentDescriptionCase caseObject = OBJECT_MAPPER.readValue(caseFilePath.toFile(), LoadDocumentDescriptionCase.class); |
| 67 | + final String sourceGuid = caseObject.getSourceGuid(); |
| 68 | + final String sourcePassword = caseObject.getSourcePassword(); |
| 69 | + expectedObject = caseObject.getExpectedObject(); |
| 70 | + |
| 71 | + LOGGER.info("Case name: '{}', Document guid: '{}'", caseFilePath.getFileName().toString(), sourceGuid); |
| 72 | + |
| 73 | + actualObject = given() |
| 74 | + .when() |
| 75 | + .body(String.format("{ 'guid': '%s', 'password': '%s' }".replace('\'', '"'), sourceGuid, sourcePassword).replace("\"null\"", "null")) |
| 76 | + .contentType(ContentType.JSON) |
| 77 | + .post(Constants.URL_TEMPLATE, "loadDocumentDescription") |
| 78 | + .then() |
| 79 | +// .log().all() |
| 80 | + .assertThat() |
| 81 | + .statusCode(200) |
| 82 | + .and().extract().body().as(LoadDocumentDescription.class); |
| 83 | + |
| 84 | + assertThat(actualObject).isNotNull(); |
| 85 | + |
| 86 | + assertThat(actualObject.getGuid()).isEqualTo(expectedObject.getGuid()); |
| 87 | + assertThat(actualObject.getPrintAllowed()).isEqualTo(expectedObject.getPrintAllowed()); |
| 88 | + assertThat(actualObject.isShowGridLines()).isEqualTo(expectedObject.isShowGridLines()); |
| 89 | + |
| 90 | + final List<LoadDocumentDescription.PageDescriptionEntity> expectedPages = expectedObject.getPages(); |
| 91 | + final List<LoadDocumentDescription.PageDescriptionEntity> actualPages = actualObject.getPages(); |
| 92 | + |
| 93 | + assertThat(expectedPages).isNotNull(); |
| 94 | + assertThat(actualPages).isNotNull(); |
| 95 | + |
| 96 | + assertThat(actualPages) |
| 97 | + .as("Pages count") |
| 98 | + .hasSameSizeAs(expectedPages); |
| 99 | + for (int n = 0; n < actualPages.size(); n++) { |
| 100 | + final LoadDocumentDescription.PageDescriptionEntity expectedPageDescriptionEntity = expectedPages.get(n); |
| 101 | + final LoadDocumentDescription.PageDescriptionEntity actualPageDescriptionEntity = actualPages.get(n); |
| 102 | + |
| 103 | + assertThat(actualPageDescriptionEntity.getAngle()).isEqualTo(expectedPageDescriptionEntity.getAngle()); |
| 104 | + assertThat(actualPageDescriptionEntity.getHeight()).isEqualTo(expectedPageDescriptionEntity.getHeight()); |
| 105 | + assertThat(actualPageDescriptionEntity.getWidth()).isEqualTo(expectedPageDescriptionEntity.getWidth()); |
| 106 | + assertThat(actualPageDescriptionEntity.getNumber()).isEqualTo(expectedPageDescriptionEntity.getNumber()); |
| 107 | + |
| 108 | + |
| 109 | + assertThat(expectedPageDescriptionEntity.getData() == null && actualPageDescriptionEntity.getData() != null) |
| 110 | + .as("Actual data must be null but was not") |
| 111 | + .isFalse(); |
| 112 | + assertThat(expectedPageDescriptionEntity.getData() != null && actualPageDescriptionEntity.getData() == null) |
| 113 | + .as("Actual data must NOT be null but was") |
| 114 | + .isFalse(); |
| 115 | + |
| 116 | + if (expectedPageDescriptionEntity.getData() != null) { |
| 117 | + final String expectedData = Utils.normalizeDataBeforeComparing(expectedPageDescriptionEntity.getData()); |
| 118 | + final String actualData = Utils.normalizeDataBeforeComparing(actualPageDescriptionEntity.getData()); |
| 119 | + |
| 120 | + final int pageNumber = n + 1; |
| 121 | + assertThat(actualData) |
| 122 | + .withFailMessage(() -> { |
| 123 | + int startIndex = 0; |
| 124 | + while (startIndex < expectedData.length() && startIndex < actualData.length() |
| 125 | + && expectedData.charAt(startIndex) == actualData.charAt(startIndex)) { |
| 126 | + startIndex++; |
| 127 | + } |
| 128 | + return String.format("expected data of page %d to be \n'%s', \nbut was \n'%s'", |
| 129 | + pageNumber, |
| 130 | + expectedData.substring(Math.max(0, startIndex - ERROR_MESSAGE_DATA_LOOK_BACK), Math.min(expectedData.length(), ERROR_MESSAGE_DATA_LENGTH + startIndex - ERROR_MESSAGE_DATA_LOOK_BACK)), |
| 131 | + actualData.substring(Math.max(0, startIndex - ERROR_MESSAGE_DATA_LOOK_BACK), Math.min(actualData.length(), ERROR_MESSAGE_DATA_LENGTH + startIndex - ERROR_MESSAGE_DATA_LOOK_BACK)) |
| 132 | + ); |
| 133 | + } |
| 134 | + ) |
| 135 | + .isEqualTo(expectedData); |
| 136 | + |
| 137 | + final Map<Integer, Map<String, String>> externalData = caseObject.getExternalData(); |
| 138 | + final Map<String, String> externalPageResources = externalData.get(pageNumber); |
| 139 | + |
| 140 | + final Matcher matcher = EXTERNAL_LINK_PATTERN.matcher(actualData); |
| 141 | + Set<String> externalResourcesCount = new HashSet<>(); // To calculate only unique resources |
| 142 | + // To prevent saving pages when the problem is in resources |
| 143 | + actualObject = null; |
| 144 | + expectedObject = null; |
| 145 | + |
| 146 | + while (matcher.find()) { |
| 147 | + final String externalRelativeLink = matcher.group(1); |
| 148 | + if (externalRelativeLink.startsWith("http") // External links |
| 149 | + || externalRelativeLink.startsWith("mailto:") |
| 150 | + || externalRelativeLink.startsWith("data:") |
| 151 | + || SKIP_RESOURCES_NAMES.contains(Paths.get(externalRelativeLink).getFileName().toString())) { |
| 152 | + continue; |
| 153 | + } |
| 154 | + assertThat(externalRelativeLink) |
| 155 | + .isNotBlank() |
| 156 | + .as("External relative link") |
| 157 | + .startsWith("/"); |
| 158 | + final String externalLink = Constants.URL_BASE + externalRelativeLink; |
| 159 | + final URL url = new URL(externalLink); |
| 160 | + try (final InputStream inputStream = url.openStream()) { |
| 161 | + assertThat(externalPageResources) |
| 162 | + .withFailMessage("Resource '%s' is not in the list of expected resources for case %s (page %d)", |
| 163 | + externalRelativeLink, |
| 164 | + caseFilePath.getFileName().toString(), |
| 165 | + pageNumber) |
| 166 | + .containsKey(externalRelativeLink); |
| 167 | + |
| 168 | + final String actualLinkContent = IOUtils.toString(inputStream, StandardCharsets.UTF_8); |
| 169 | + final String expectedLinkContent = externalPageResources.get(externalRelativeLink); |
| 170 | + |
| 171 | + assertThat(actualLinkContent) |
| 172 | + .isNotNull() |
| 173 | + .isNotBlank(); |
| 174 | + |
| 175 | + |
| 176 | + if (externalRelativeLink.endsWith(".png")) { |
| 177 | + externalPageResources.put(externalRelativeLink, String.valueOf(actualLinkContent.length())); |
| 178 | + final int expectedLinkContentLength = Integer.parseInt(expectedLinkContent); |
| 179 | + assertThat(actualLinkContent) |
| 180 | + .withFailMessage("Resource content length (binary file) is expected to be %d, but was %d", expectedLinkContentLength, actualLinkContent.length()) |
| 181 | + .hasSize(expectedLinkContentLength); |
| 182 | + } else { |
| 183 | + externalPageResources.put(externalRelativeLink, actualLinkContent); |
| 184 | + assertThat(Utils.normalizeDataBeforeComparing(actualLinkContent)) |
| 185 | + .withFailMessage(() -> { |
| 186 | + int startIndex = 0; |
| 187 | + while (startIndex < expectedLinkContent.length() && startIndex < actualLinkContent.length() |
| 188 | + && expectedLinkContent.charAt(startIndex) == actualLinkContent.charAt(startIndex)) { |
| 189 | + startIndex++; |
| 190 | + } |
| 191 | + return String.format("expected data of resource '%s' (page %d) to be \n'%s', \nbut was \n'%s'", |
| 192 | + externalRelativeLink, |
| 193 | + pageNumber, |
| 194 | + expectedLinkContent.substring(Math.max(0, startIndex - ERROR_MESSAGE_DATA_LOOK_BACK), Math.min(expectedLinkContent.length(), ERROR_MESSAGE_DATA_LENGTH + startIndex - ERROR_MESSAGE_DATA_LOOK_BACK)), |
| 195 | + actualLinkContent.substring(Math.max(0, startIndex - ERROR_MESSAGE_DATA_LOOK_BACK), Math.min(actualLinkContent.length(), ERROR_MESSAGE_DATA_LENGTH + startIndex - ERROR_MESSAGE_DATA_LOOK_BACK)) |
| 196 | + ); |
| 197 | + } |
| 198 | + ) |
| 199 | + .isEqualTo(Utils.normalizeDataBeforeComparing(expectedLinkContent)); |
| 200 | + } |
| 201 | + } |
| 202 | + externalResourcesCount.add(externalRelativeLink); |
| 203 | + } |
| 204 | + assertThat(externalResourcesCount.size()) |
| 205 | + .as("Expected external resources count") |
| 206 | + .isEqualTo(externalPageResources.size()); |
| 207 | + } |
| 208 | + } |
| 209 | + } catch (AssertionError e) { |
| 210 | + if (expectedObject != null && actualObject != null) { |
| 211 | + final String fileName = caseFilePath.getFileName().toString(); |
| 212 | + final List<LoadDocumentDescription.PageDescriptionEntity> expectedPages = expectedObject.getPages(); |
| 213 | + final List<LoadDocumentDescription.PageDescriptionEntity> actualPages = actualObject.getPages(); |
| 214 | + for (int n = 0; n < Math.max(expectedPages.size(), actualPages.size()); n++) { |
| 215 | + if (expectedPages.size() > n) { |
| 216 | + final LoadDocumentDescription.PageDescriptionEntity expectedPageDescriptionEntity = expectedPages.get(n); |
| 217 | + final Path expectedTempFile = Files.createTempFile(Paths.get("target"), "LoadDocumentDescription-" + fileName.substring(0, fileName.lastIndexOf('.')) + "-expected-page-" + expectedPageDescriptionEntity.getNumber() + "-", ".html"); |
| 218 | + FileUtils.write(expectedTempFile.toFile(), expectedPageDescriptionEntity.getData(), StandardCharsets.UTF_8); |
| 219 | + LOGGER.info("Expected data for page {} was written:\t'{}'", n + 1, expectedTempFile); |
| 220 | + } |
| 221 | + if (actualPages.size() > n) { |
| 222 | + final LoadDocumentDescription.PageDescriptionEntity actualPageDescriptionEntity = actualPages.get(n); |
| 223 | + final Path actualTempFile = Files.createTempFile(Paths.get("target"), "LoadDocumentDescription-" + fileName.substring(0, fileName.lastIndexOf('.')) + "-actual-page-" + actualPageDescriptionEntity.getNumber() + "-", ".html"); |
| 224 | + FileUtils.write(actualTempFile.toFile(), actualPageDescriptionEntity.getData(), StandardCharsets.UTF_8); |
| 225 | + LOGGER.info("Actual data for page {} was written:\t'{}'", n + 1, actualTempFile); |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + throw e; |
| 230 | + } finally { |
| 231 | + System.out.println(); |
| 232 | + } |
| 233 | + } |
| 234 | +} |
0 commit comments