package org.atlasapi.equivalence;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.atlasapi.content.BrandRef;
import org.atlasapi.content.ItemRef;
import org.atlasapi.entity.Id;
import org.atlasapi.media.entity.Publisher;
import org.atlasapi.messaging.JacksonMessageSerializer;

import com.metabroadcast.common.queue.MessagingException;
import com.metabroadcast.common.time.Timestamp;

import com.google.common.collect.ImmutableSet;
import org.joda.time.DateTime;
import org.apache.commons.io.IOUtils;
import org.junit.Test;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

public class EquivalenceGraphUpdateMessageTest {

    private JacksonMessageSerializer<EquivalenceGraphUpdateMessage> serializer
            = JacksonMessageSerializer.forType(EquivalenceGraphUpdateMessage.class);

    @Test
    public void testDeSerializationWithoutCreatedDeleted() throws Exception {
        testSerializingMessageWith(
                EquivalenceGraphUpdate.builder(
                        EquivalenceGraph.valueOf(new BrandRef(Id.valueOf(1), Publisher.BBC))
                )
                        .build()
        );
    }

    @Test
    public void testDeSerializationWithoutDeleted() throws Exception {
        testSerializingMessageWith(EquivalenceGraphUpdate
                .builder(EquivalenceGraph.valueOf(new BrandRef(Id.valueOf(1), Publisher.BBC)))
                .withCreated(
                        ImmutableSet.of(EquivalenceGraph.valueOf(new BrandRef(
                                Id.valueOf(2),
                                Publisher.BBC
                        )))
                )
                .build()
        );
    }

    @Test
    public void testDeSerializationWithoutCreated() throws Exception {
        testSerializingMessageWith(
                EquivalenceGraphUpdate
                        .builder(
                                EquivalenceGraph.valueOf(new BrandRef(Id.valueOf(1), Publisher.BBC))
                        )
                        .withDeleted(ImmutableSet.of(Id.valueOf(1)))
                        .build()
        );
    }

    @Test
    public void testDeSerialization() throws Exception {
        testSerializingMessageWith(
                EquivalenceGraphUpdate
                    .builder(EquivalenceGraph.valueOf(new BrandRef(Id.valueOf(1), Publisher.BBC)))
                    .withCreated(
                            ImmutableSet.of(EquivalenceGraph.valueOf(new BrandRef(
                                    Id.valueOf(2),
                                    Publisher.BBC
                            )))
                    )
                    .withDeleted(
                            ImmutableSet.of(Id.valueOf(1))
                    )
                    .build()
        );
    }

    @Test
    public void testDeserialisationWithEquivalenceAssertion() throws Exception {
        EquivalenceGraph updatedGraph = EquivalenceGraph.valueOf(
                new BrandRef(Id.valueOf(1), Publisher.BBC)
        );

        EquivalenceAssertion equivalenceAssertion = EquivalenceAssertion.create(
                new ItemRef(Id.valueOf(100L), Publisher.BBC, "", DateTime.now()),
                ImmutableSet.of(
                        new ItemRef(Id.valueOf(101L), Publisher.BBC, "", DateTime.now())
                ),
                ImmutableSet.of(Publisher.BBC)
        );

        testSerializingMessageWith(
                EquivalenceGraphUpdate
                        .builder(updatedGraph)
                        .withAssertion(equivalenceAssertion)
                        .build()
        );
    }

    @Test
    public void testDeSerializationOfOldGraphUpdateModel() throws Exception {
        EquivalenceGraphUpdate equivalenceGraphUpdate = new EquivalenceGraphUpdate(
                EquivalenceGraph.valueOf(new BrandRef(Id.valueOf(1), Publisher.BBC)),
                ImmutableSet.of(EquivalenceGraph.valueOf(new BrandRef(
                        Id.valueOf(2),
                        Publisher.BBC
                ))),
                ImmutableSet.of(Id.valueOf(1))
        );

        EquivalenceGraphUpdateMessage latestModel =
                new EquivalenceGraphUpdateMessage("message", Timestamp.of(0), equivalenceGraphUpdate);

        InputStream model = getClass().getResourceAsStream(
                "/old_equiv_graph_model_byte_array.txt");
        EquivalenceGraphUpdateMessage oldModel = serializer.deserialize(IOUtils.toByteArray(model));
        EquivalenceGraphUpdateMessage newModel = serializer.deserialize(serializer.serialize(latestModel));
        assertEquals(oldModel, newModel);
    }

    private void testSerializingMessageWith(EquivalenceGraphUpdate update)
            throws MessagingException {
        EquivalenceGraphUpdateMessage egum =
                new EquivalenceGraphUpdateMessage("message", Timestamp.of(0), update);

        byte[] serialized = serializer.serialize(egum);

        EquivalenceGraphUpdateMessage deserialized = serializer.deserialize(serialized);

        assertThat(deserialized, is(egum));
        assertThat(deserialized.getGraphUpdate(), is(egum.getGraphUpdate()));
    }
}

@@ -43,25 +43,16 @@
 java.io.
-ByteArray
 InputStr
@@ -59,109 +59,8 @@
 eam;
-%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.UnsupportedEncodingException;
 %0A%0Aim
@@ -422,39 +422,8 @@
 et;%0A
-import org.joda.time.DateTime;%0A
 impo
@@ -452,24 +452,55 @@
 io.IOUtils;%0A
+import org.joda.time.DateTime;%0A
 import org.j
@@ -555,58 +555,8 @@
 is;%0A
-import static org.junit.Assert.assertArrayEquals;%0A
 impo
@@ -3794,20 +3794,16 @@
 Update =
- new
  Equival
@@ -3817,16 +3817,24 @@
 phUpdate
+.builder
 (%0A      
@@ -3911,18 +3911,61 @@
 er.BBC))
-,
 %0A
+        )%0A                .withCreated(%0A    
         
@@ -3964,32 +3964,36 @@
                 
+    
 ImmutableSet.of(
@@ -4047,32 +4047,40 @@
                 
+        
 Id.valueOf(2),%0A 
@@ -4094,32 +4094,40 @@
                 
+   
+     
 Publisher.BBC%0A  
@@ -4144,12 +4144,37 @@
     
-))),
+        )))%0A                )
 %0A   
@@ -4178,32 +4178,45 @@
                 
+.withDeleted(
 ImmutableSet.of(
@@ -4221,32 +4221,33 @@
 f(Id.valueOf(1))
+)
 %0A        );%0A%0A   
@@ -4231,32 +4231,47 @@
 Of(1)))%0A        
+        .build(
 );%0A%0A        Equi
@@ -4309,32 +4309,16 @@
 tModel =
-%0A               
  new Equ
@@ -4336,32 +4336,49 @@
 phUpdateMessage(
+%0A                
 %22message%22, Times
@@ -4407,24 +4407,33 @@
 eGraphUpdate
+%0A        
 );%0A%0A        
@@ -4614,16 +4614,33 @@
 rialize(
+%0A                
 IOUtils.
@@ -4653,24 +4653,33 @@
 Array(model)
+%0A        
 );%0A        E
@@ -4733,32 +4733,49 @@
 zer.deserialize(
+%0A                
 serializer.seria
@@ -4791,16 +4791,25 @@
 stModel)
+%0A        
 );%0A     

Fix merge conflicts