/*
 * Copyright 2020-2024 the original author or authors.
 *
 * 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
 *
 *      https://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 org.springframework.cloud.fn.consumer.log;

import java.util.function.Consumer;
import java.util.function.Supplier;

import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.log.LogAccessor;
import org.springframework.integration.handler.LoggingHandler;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.Message;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.util.MimeType;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
 * @author Artem Bilan
 */
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@SpringBootTest({ "log.consumer.name=foo", "log.consumer.level=warn", "log.consumer.expression=payload.toUpperCase()" })
class LogConsumerApplicationTests {

	@Autowired
	private Consumer<Message<?>> logConsumer;

	@Autowired
	@Qualifier("logConsumerFlow.logging-channel-adapter#0")
	private LoggingHandler loggingHandler;

	@Test
	void testJsonContentType() {
		Message<String> message = MessageBuilder.withPayload("{\"foo\":\"bar\"}")
			.setHeader("contentType", new MimeType("json"))
			.build();
		testMessage(message, "{\"foo\":\"bar\"}");
	}

	@SuppressWarnings("unchecked")
	private void testMessage(Message<?> message, String expectedPayload) {
		assertThat(this.loggingHandler.getLevel()).isEqualTo(LoggingHandler.Level.WARN);
		LogAccessor logger = TestUtils.getPropertyValue(this.loggingHandler, "messageLogger", LogAccessor.class);
		assertThat(TestUtils.getPropertyValue(logger.getLog(), "logger.name")).isEqualTo("foo");
		logger = spy(logger);
		new DirectFieldAccessor(this.loggingHandler).setPropertyValue("messageLogger", logger);
		this.logConsumer.accept(message);
		ArgumentCaptor<Supplier<CharSequence>> captor = ArgumentCaptor.forClass(Supplier.class);
		verify(logger).warn(captor.capture());
		assertThat(captor.getValue().get()).isEqualTo(expectedPayload.toUpperCase());
		this.loggingHandler.setLogExpressionString("#this");
		this.logConsumer.accept(message);
		verify(logger, times(2)).warn(captor.capture());

		// Message<?> captorMessage = (Message<?>) captor.getAllValues().get(2);
		// assertThat(captorMessage.getPayload()).isEqualTo(expectedPayload);
		//
		// MessageHeaders messageHeaders = captorMessage.getHeaders();
		// assertThat(messageHeaders).hasSize(4);
		//
		// assertThat(messageHeaders)
		// .containsEntry(MessageHeaders.CONTENT_TYPE,
		// message.getHeaders().get(MessageHeaders.CONTENT_TYPE));
	}

	@SpringBootApplication
	static class LogConsumerTestApplication {

	}

}
