|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. |
| 3 | + */ |
| 4 | +package com.marklogic.client.test.document; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | +import com.marklogic.client.DatabaseClient; |
| 8 | +import com.marklogic.client.document.*; |
| 9 | +import com.marklogic.client.io.DocumentMetadataHandle; |
| 10 | +import com.marklogic.client.io.Format; |
| 11 | +import com.marklogic.client.io.InputStreamHandle; |
| 12 | +import com.marklogic.client.io.JacksonHandle; |
| 13 | +import com.marklogic.client.test.AbstractClientTest; |
| 14 | +import com.marklogic.client.test.Common; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 18 | + |
| 19 | +class ReadJsonAsBinaryTest extends AbstractClientTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + void test() { |
| 23 | + try (DatabaseClient client = Common.newClient()) { |
| 24 | + final String uri = "/a.json"; |
| 25 | + writeJsonAsBinary(uri, client); |
| 26 | + |
| 27 | + GenericDocumentManager docManager = client.newDocumentManager(); |
| 28 | + |
| 29 | + // A multipart request returns the correct document format. |
| 30 | + try (DocumentPage page = docManager.read(uri)) { |
| 31 | + DocumentRecord record = page.next(); |
| 32 | + assertEquals(uri, record.getUri()); |
| 33 | + assertEquals("application/json", record.getMimetype()); |
| 34 | + assertEquals(Format.BINARY, record.getFormat()); |
| 35 | + } |
| 36 | + |
| 37 | + // But a request for a single URI does not! |
| 38 | + try (InputStreamHandle handle = docManager.read(uri, new InputStreamHandle())) { |
| 39 | + assertEquals("application/json", handle.getMimetype()); |
| 40 | + assertEquals(Format.JSON, handle.getFormat(), |
| 41 | + "This is not correct! This is identifying a bug in the server where a GET with a 'uri' query " + |
| 42 | + "parameter and a 'category' parameter is not returning the correct document type. This is " + |
| 43 | + "evident in the response header which is 'vnd.marklogic.document-format: json' instead of " + |
| 44 | + "'vnd.marklogic.document-format: binary'. Will update this soon with a Jira ID for the " + |
| 45 | + "server bug."); |
| 46 | + } |
| 47 | + |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private void writeJsonAsBinary(String uri, DatabaseClient client) { |
| 52 | + JSONDocumentManager jsonDocManager = client.newJSONDocumentManager(); |
| 53 | + DocumentMetadataHandle metadata = new DocumentMetadataHandle() |
| 54 | + .withPermission("rest-reader", DocumentMetadataHandle.Capability.READ, DocumentMetadataHandle.Capability.UPDATE); |
| 55 | + |
| 56 | + jsonDocManager.setWriteTransform(new ServerTransform("toBinary")); |
| 57 | + jsonDocManager.write(uri, metadata, new JacksonHandle(new ObjectMapper().createObjectNode().put("a", 1))); |
| 58 | + } |
| 59 | +} |
0 commit comments