<commit_before>/***************************************************************************
 * Copyright 2018 Kieker Project (http://kieker-monitoring.net)
 *
 * 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 kieker.monitoring.writer.filesystem;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.BufferOverflowException;
import java.nio.charset.Charset;
import java.nio.file.Paths;

import org.junit.Assert;
import org.junit.Test;

import kieker.common.record.IMonitoringRecord;
import kieker.common.record.controlflow.OperationExecutionRecord;
import kieker.common.registry.IRegistryListener;
import kieker.common.registry.writer.WriterRegistry;
import kieker.monitoring.writer.compression.ICompressionFilter;
import kieker.monitoring.writer.compression.NoneCompressionFilter;

/**
 * @author <NAME>
 *
 * @since 1.14
 */
public class BinaryLogStreamHandlerTest implements IRegistryListener<String> {

	/** Initializing parameters for TextlogStreamhandler object instance. */
	private final Charset charset = Charset.defaultCharset();
	private final ICompressionFilter compressionFilter = new NoneCompressionFilter(null);
	private final WriterRegistry reg = new WriterRegistry(this);

	public BinaryLogStreamHandlerTest() {
		// nothing to be done here.
	}

	@Test
	public void testClose() {
		// test not required
	}

	@Test
	public void testSerializeBufferTooTiny() throws IOException {
		final IMonitoringRecord record = new OperationExecutionRecord("testing", "abc", 1, 0, 1, "localhost", 123, 456);

		final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		final BinaryLogStreamHandler handler = new BinaryLogStreamHandler(true, 10,
				this.charset, this.compressionFilter, this.reg);

		try {
			// channel initializing
			handler.initialize(byteArrayOutputStream, Paths.get("test-filename"));
			// serializing test record
			handler.serialize(record, 2);
			Assert.assertEquals("String does not match", "$2;-1;testing;abc;1;0;1;localhost;123;456",
					byteArrayOutputStream.toString().trim());
		} catch (final BufferOverflowException e) {
			// as buffer size is to small it will always catch exception
		} finally {
			byteArrayOutputStream.close();
		}
	}

	@Test
	public void testSerializeBufferSufficient() throws IOException {
		final IMonitoringRecord record = new OperationExecutionRecord("testing", "abc", 1, 0, 1, "localhost", 123, 456);

		final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		final BinaryLogStreamHandler handler = new BinaryLogStreamHandler(true, 10240,
				this.charset, this.compressionFilter, this.reg);

		try {
			// channel initializing
			handler.initialize(byteArrayOutputStream, Paths.get("test-filename"));
			// serializing test record<commit_msg>Added nopmd rules<commit_after>/***************************************************************************
 * Copyright 2018 Kieker Project (http://kieker-monitoring.net)
 *
 * 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 kieker.monitoring.writer.filesystem;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.BufferOverflowException;
import java.nio.charset.Charset;
import java.nio.file.Paths;

import org.junit.Assert;
import org.junit.Test;

import kieker.common.record.IMonitoringRecord;
import kieker.common.record.controlflow.OperationExecutionRecord;
import kieker.common.registry.IRegistryListener;
import kieker.common.registry.writer.WriterRegistry;
import kieker.monitoring.writer.compression.ICompressionFilter;
import kieker.monitoring.writer.compression.NoneCompressionFilter;

/**
 * @author <NAME>
 *
 * @since 1.14
 */
public class BinaryLogStreamHandlerTest implements IRegistryListener<String> {

	/** Initializing parameters for TextlogStreamhandler object instance. */
	private final Charset charset = Charset.defaultCharset();
	private final ICompressionFilter compressionFilter = new NoneCompressionFilter(null);
	private final WriterRegistry reg = new WriterRegistry(this);

	public BinaryLogStreamHandlerTest() {
		// nothing to be done here.
	}

	@Test
	public void testClose() {
		// test not required
	}

	@Test
	public void testSerializeBufferTooTiny() throws IOException {
		final IMonitoringRecord record = new OperationExecutionRecord("testing", "abc", 1, 0, 1, "localhost", 123, 456);

		final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		final BinaryLogStreamHandler handler = new BinaryLogStreamHandler(true, 10,
				this.charset, this.compressionFilter, this.reg);

		try {
			// channel initializing
			handler.initialize(byteArrayOutputStream, Paths.get("test-filename"));
			// serializing test record
			handler.serialize(record, 2);
			Assert.assertEquals("String does not match", "$2;-1;testing;abc;1;0;1;localhost;123;456",
					byteArrayOutputStream.toString().trim());
		} catch (final BufferOverflowException e) { // NOPMD
			// as buffer size is to small it will always catch exception
		} finally {
			byteArrayOutputStream.close();
		}
	}

	@Test
	public void testSerializeBufferSufficient() throws IOException {
		final IMonitoringRecord record = new OperationExecutionRecord("testing", "abc", 1, 0, 1, "localhost", 123, 456);

		final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		final BinaryLogStreamHandler handler = new BinaryLogStreamHandler(true, 10240,
				this.charset, this.compressionFilter, this.reg);

		try {
			// channel initializing
			handler.initialize(byteArrayOutputStream, Paths.get("test-filename"));
			// serializing test record
{'fraction_non_alphanumeric': 0.13406171563561198, 'fraction_numerical': 0.014173925882179242, 'mean_word_length': 5.336763330215154, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '5048331', 'n_tokens_mistral': 1941, 'n_tokens_neox': 1736, 'n_words': 528}