<commit_before>/*
 * Copyright 2012 MarkLogic Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.marklogic.client.test;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.io.Reader;

import javax.xml.XMLConstants;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.w3c.dom.Document;

import com.marklogic.client.JSONDocumentManager;
import com.marklogic.client.XMLDocumentManager;
import com.marklogic.client.io.BytesHandle;
import com.marklogic.client.io.DOMHandle;
import com.marklogic.client.io.FileHandle;
import com.marklogic.client.io.InputStreamHandle;
import com.marklogic.client.io.ReaderHandle;
import com.marklogic.client.io.StringHandle;

public class JSONDocumentTest {
	@BeforeClass
	public static void beforeClass() {
		Common.connect();
	}
	@AfterClass
	public static void afterClass() {
		Common.release();
	}

	@Test
	public void testReadWrite() throws IOException {
		String docId = "/test/testWrite1.json";

		ObjectMapper mapper = new ObjectMapper();

		ObjectNode sourceNode = mapper.createObjectNode();
		sourceNode.put("stringKey", "string value");
		sourceNode.put("numberKey", 7);
		ObjectNode childNode = mapper.createObjectNode();
		childNode.put("childObjectKey", "child object value");
		sourceNode.put("objectKey", childNode);
		ArrayNode childArray = mapper.createArrayNode();
		childArray.add("item value");
		childArray.add(3);
		childNode = mapper.createObjectNode();
		childNode.put("itemObjectKey", "item object value");
		childArray.add(childNode);
		sourceNode.put("arrayKey", childArray);

		String content = mapper.writeValueAsString(sourceNode);
		JSONDocumentManager docMgr = Common.client.newJSONDocumentManager();
		docMgr.write(docId, new StringHandle().with(content));

		String docText = docMgr.read(docId, new StringHandle()).get();
		assertNotNull("Read null string for JSON content",docText);
		JsonNode readNode = mapper.readTree(docText);
		assertTrue("Failed to read JSON document as String", sourceNode.equals(readNode));
		
		BytesHandle bytesHandle = new BytesHandle();
		docMgr.read(docId, bytesHandle);
		readNode = mapper.readTree(bytesHandle.get());
		assertTrue("JSON document mismatch reading bytes", sourceNode.equals(readNode));

		InputStreamHandle inputStreamHandle = new InputStreamHandle();
		docMgr.read(docId, inputStreamHandle);
		readNode = mapper.readTree(inputStreamHandle.get());
		assertTrue("JSON document mismatch reading input stream", sourceNode.equals(readNode));

		Reader reader = docMgr.read(docId, new ReaderHandle()).get();
		readNode = mapper.readTree(reader);
		assertTrue("JSON document mismatch with reader", sourceNode.equals(readNode));

		File file = docMgr.read(docId, new FileHandle()).get();
		readNode = mapper.readTree(file);
		assertTrue("JSON document mismatch with file", sourceNode.equals(readNode));

		String lang = "fr-CA";
		docMgr.setLanguage(lang);<commit_msg>Bug:17060 test case <commit_after>/*
 * Copyright 2012 MarkLogic Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.marklogic.client.test;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.io.Reader;

import javax.xml.XMLConstants;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.w3c.dom.Document;

import com.marklogic.client.JSONDocumentManager;
import com.marklogic.client.XMLDocumentManager;
import com.marklogic.client.io.BytesHandle;
import com.marklogic.client.io.DOMHandle;
import com.marklogic.client.io.FileHandle;
import com.marklogic.client.io.InputStreamHandle;
import com.marklogic.client.io.ReaderHandle;
import com.marklogic.client.io.StringHandle;

public class JSONDocumentTest {
	@BeforeClass
	public static void beforeClass() {
		Common.connect();
	}
	@AfterClass
	public static void afterClass() {
		Common.release();
	}

	@Test
	public void testReadWrite() throws IOException {
		String docId = "/test/testWrite1.json";

		ObjectMapper mapper = new ObjectMapper();

		ObjectNode sourceNode = mapper.createObjectNode();
		sourceNode.put("stringKey", "string value");
		sourceNode.put("numberKey", 7);
		ObjectNode childNode = mapper.createObjectNode();
		childNode.put("childObjectKey", "child object value");
		sourceNode.put("objectKey", childNode);
		ArrayNode childArray = mapper.createArrayNode();
		childArray.add("item value");
		childArray.add(3);
		childNode = mapper.createObjectNode();
		childNode.put("itemObjectKey", "item object value");
		childArray.add(childNode);
		sourceNode.put("arrayKey", childArray);

		String content = mapper.writeValueAsString(sourceNode);
		JSONDocumentManager docMgr = Common.client.newJSONDocumentManager();
		docMgr.write(docId, new StringHandle().with(content));

		String docText = docMgr.read(docId, new StringHandle()).get();
		assertNotNull("Read null string for JSON content",docText);
		JsonNode readNode = mapper.readTree(docText);
		assertTrue("Failed to read JSON document as String", sourceNode.equals(readNode));
		
		BytesHandle bytesHandle = new BytesHandle();
		docMgr.read(docId, bytesHandle);
		readNode = mapper.readTree(bytesHandle.get());
		assertTrue("JSON document mismatch reading bytes", sourceNode.equals(readNode));

		InputStreamHandle inputStreamHandle = new InputStreamHandle();
		docMgr.read(docId, inputStreamHandle);
		readNode = mapper.readTree(inputStreamHandle.get());
		assertTrue("JSON document mismatch reading input stream", sourceNode.equals(readNode));

		Reader reader = docMgr.read(docId, new ReaderHandle()).get();
		readNode = mapper.readTree(reader);
		assertTrue("JSON document mismatch with reader", sourceNode.equals(readNode));

		File file = docMgr.read(docId, new FileHandle()).get();
		readNode = mapper.readTree(file);
		assertTrue("JSON document mismatch with file", sourceNode.equals(readNode));

		docMgr.write(docId, new StringHandle().with(GenericDocumentTest.metadata),
				new StringHandle().with(content));
		docText = docMgr.read(docId, new StringHandle()).get();
		assertNotNull("Read null string for JSON content",docText);
		readNode = mapper.readTree(docText);
		assertTrue("Failed to read JSON document as String", sourceNode.equals(readNode));

		String lang = "fr-CA";
		docMgr.setLanguage(lang);
{'fraction_non_alphanumeric': 0.11251881585549423, 'fraction_numerical': 0.003637732062217762, 'mean_word_length': 5.545977011494253, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6613225', 'n_tokens_mistral': 2431, 'n_tokens_neox': 2264, 'n_words': 595}