/*
 * Copyright (c) 2010-2018. Axon Framework
 *
 * 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 org.axonframework.messaging;

import org.axonframework.messaging.unitofwork.CurrentUnitOfWork;
import org.axonframework.messaging.unitofwork.UnitOfWork;
import org.axonframework.serialization.SerializedObject;
import org.axonframework.serialization.Serializer;
import org.axonframework.serialization.json.JacksonSerializer;
import org.junit.jupiter.api.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * @author Rene de Waele
 */
class GenericMessageTest {

    private Map<String, ?> correlationData = MetaData.from(Collections.singletonMap("foo", "bar"));

    @BeforeEach
    void setUp() {
        UnitOfWork<?> unitOfWork = mock(UnitOfWork.class);
        when(unitOfWork.getCorrelationData()).thenAnswer(invocation -> correlationData);
        CurrentUnitOfWork.set(unitOfWork);
    }

    @AfterEach
    void tearDown() {
        while (CurrentUnitOfWork.isStarted()) {
            CurrentUnitOfWork.clear(CurrentUnitOfWork.get());
        }
    }

    @Test
    void testCorrelationDataAddedToNewMessage() {
        assertEquals(correlationData, new HashMap<>(new GenericMessage<>(new Object()).getMetaData()));

        MetaData newMetaData = MetaData.from(Collections.singletonMap("whatever", new Object()));
        assertEquals(newMetaData.mergedWith(correlationData),
                     new GenericMessage<>(new Object(), newMetaData).getMetaData());
    }

    @Test
    void testMessageSerialization() {
        GenericMessage<String> message = new GenericMessage<>("payload", Collections.singletonMap("key", "value"));
        Serializer jacksonSerializer = JacksonSerializer.builder().build();

        SerializedObject<String> serializedPayload = message.serializePayload(jacksonSerializer, String.class);
        SerializedObject<String> serializedMetaData = message.serializeMetaData(jacksonSerializer, String.class);

        assertEquals("\"payload\"", serializedPayload.getData());
        assertEquals("{\"key\":\"value\",\"foo\":\"bar\"}", serializedMetaData.getData());
    }
}

@@ -1092,28 +1092,17 @@
 ertions.
-assertEquals
+*
 ;%0Aimport
@@ -1133,57 +1133,81 @@
 ito.
-mock;%0Aimport static org.mockito.Mockito.when;%0A%0A/*
+*;%0A%0A/**%0A * Test correct operations of the %7B@link GenericMessage%7D class.%0A 
 *%0A *
@@ -2806,11 +2806,555 @@
 );%0A    %7D
+%0A%0A    @Test%0A    void testAsMessageReturnsProvidedMessageAsIs() %7B%0A        GenericMessage%3CString%3E testMessage = new GenericMessage%3C%3E(%22payload%22);%0A%0A        Message%3C?%3E result = GenericMessage.asMessage(testMessage);%0A%0A        assertEquals(testMessage, result);%0A    %7D%0A%0A    @Test%0A    void testAsMessageWrapsProvidedObjectsInMessage() %7B%0A        String testPayload = %22payload%22;%0A%0A        Message%3C?%3E result = GenericMessage.asMessage(testPayload);%0A%0A        assertNotEquals(testPayload, result);%0A        assertEquals(testPayload, result.getPayload());%0A    %7D
 %0A%7D%0A

Add test cases