/*
 * Copyright 2005-2025 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.ws.support;

import javax.xml.transform.Result;
import javax.xml.transform.Source;

import org.junit.jupiter.api.Test;

import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.mime.MimeContainer;
import org.springframework.oxm.mime.MimeMarshaller;
import org.springframework.oxm.mime.MimeUnmarshaller;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.mime.MimeMessage;
import org.springframework.xml.transform.StringResult;
import org.springframework.xml.transform.StringSource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.isA;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;

class MarshallingUtilsTests {

	@Test
	void testUnmarshal() throws Exception {

		Unmarshaller unmarshallerMock = createMock(Unmarshaller.class);
		WebServiceMessage messageMock = createMock(WebServiceMessage.class);

		Source source = new StringSource("");
		Object unmarshalled = new Object();
		expect(messageMock.getPayloadSource()).andReturn(source);
		expect(unmarshallerMock.unmarshal(source)).andReturn(unmarshalled);

		replay(unmarshallerMock, messageMock);

		Object result = MarshallingUtils.unmarshal(unmarshallerMock, messageMock);

		assertThat(result).isEqualTo(unmarshalled);

		verify(unmarshallerMock, messageMock);
	}

	@Test
	void testUnmarshalMime() throws Exception {

		MimeUnmarshaller unmarshallerMock = createMock(MimeUnmarshaller.class);
		MimeMessage messageMock = createMock(MimeMessage.class);

		Source source = new StringSource("");
		Object unmarshalled = new Object();
		expect(messageMock.getPayloadSource()).andReturn(source);
		expect(unmarshallerMock.unmarshal(eq(source), isA(MimeContainer.class))).andReturn(unmarshalled);

		replay(unmarshallerMock, messageMock);

		Object result = MarshallingUtils.unmarshal(unmarshallerMock, messageMock);

		assertThat(result).isEqualTo(unmarshalled);

		verify(unmarshallerMock, messageMock);
	}

	@Test
	void testUnmarshalNoPayload() throws Exception {

		Unmarshaller unmarshallerMock = createMock(Unmarshaller.class);
		MimeMessage messageMock = createMock(MimeMessage.class);

		expect(messageMock.getPayloadSource()).andReturn(null);

		replay(unmarshallerMock, messageMock);

		Object result = MarshallingUtils.unmarshal(unmarshallerMock, messageMock);

		assertThat(result).isNull();

		verify(unmarshallerMock, messageMock);
	}

	@Test
	void testMarshal() throws Exception {

		Marshaller marshallerMock = createMock(Marshaller.class);
		WebServiceMessage messageMock = createMock(WebServiceMessage.class);

		Result result = new StringResult();
		Object marshalled = new Object();
		expect(messageMock.getPayloadResult()).andReturn(result);
		marshallerMock.marshal(marshalled, result);

		replay(marshallerMock, messageMock);

		MarshallingUtils.marshal(marshallerMock, marshalled, messageMock);

		verify(marshallerMock, messageMock);
	}

	@Test
	void testMarshalMime() throws Exception {

		MimeMarshaller marshallerMock = createMock(MimeMarshaller.class);
		MimeMessage messageMock = createMock(MimeMessage.class);

		Result result = new StringResult();
		Object marshalled = new Object();
		expect(messageMock.getPayloadResult()).andReturn(result);
		marshallerMock.marshal(eq(marshalled), eq(result), isA(MimeContainer.class));

		replay(marshallerMock, messageMock);

		MarshallingUtils.marshal(marshallerMock, marshalled, messageMock);

		verify(marshallerMock, messageMock);
	}

}
