package com.orientechnologies.orient.client.remote.message;

import com.orientechnologies.orient.core.serialization.serializer.record.binary.ORecordSerializerNetworkV37;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * Created by tglman on 09/05/17.
 */
public class ORemotePushMessagesTest {

  @Test
  public void testDistributedConfig() throws IOException {
    MockChannel channel = new MockChannel();
    List<String> hosts = new ArrayList<>();
    hosts.add("one");
    hosts.add("two");
    OPushDistributedConfigurationRequest request = new OPushDistributedConfigurationRequest(hosts);
    request.write(channel);
    channel.close();

    OPushDistributedConfigurationRequest readRequest = new OPushDistributedConfigurationRequest();
    readRequest.read(channel);
    assertEquals(readRequest.getHosts().size(), 2);
    assertEquals(readRequest.getHosts().get(0), "one");
    assertEquals(readRequest.getHosts().get(1), "two");
  }

  @Test
  public void testSubscribeRequest() throws IOException {
    MockChannel channel = new MockChannel();

    OSubscribeRequest request = new OSubscribeRequest(new OSubscribeLiveQueryRequest("10", new HashMap<>()));
    request.write(channel, null);
    channel.close();

    OSubscribeRequest requestRead = new OSubscribeRequest();
    requestRead.read(channel, 1, ORecordSerializerNetworkV37.INSTANCE);

    assertEquals(request.getPushMessage(), requestRead.getPushMessage());
    assertTrue(requestRead.getPushRequest() instanceof OSubscribeLiveQueryRequest);

  }

  @Test
  public void testSubscribeResponse() throws IOException {
    MockChannel channel = new MockChannel();

    OSubscribeResponse response = new OSubscribeResponse(new OSubscribeLiveQueryResponse(10));
    response.write(channel, 1, ORecordSerializerNetworkV37.INSTANCE);
    channel.close();

    OSubscribeResponse responseRead = new OSubscribeResponse(new OSubscribeLiveQueryResponse());
    responseRead.read(channel, null);

    assertTrue(responseRead.getResponse() instanceof OSubscribeLiveQueryResponse);
    assertEquals(((OSubscribeLiveQueryResponse) responseRead.getResponse()).getMonitorId(), 10);

  }

  @Test
  public void testUnsubscribeRequest() throws IOException {
    MockChannel channel = new MockChannel();
    OUnsubscribeRequest request = new OUnsubscribeRequest(new OUnsubscribeLiveQueryRequest(10));
    request.write(channel, null);
    channel.close();
    OUnsubscribeRequest readRequest = new OUnsubscribeRequest();
    readRequest.read(channel, 0, null);
    assertEquals(((OUnsubscribeLiveQueryRequest) readRequest.getUnsubscribeRequest()).getMonitorId(), 10);
  }

}

@@ -54,16 +54,198 @@
 ssage;%0A%0A
+import com.orientechnologies.orient.core.db.*;%0Aimport com.orientechnologies.orient.core.metadata.schema.OSchemaProxy;%0Aimport com.orientechnologies.orient.core.record.impl.ODocument;%0A
 import c
@@ -345,16 +345,16 @@
 orkV37;%0A
-
 import o
@@ -520,16 +520,62 @@
 Equals;%0A
+import static org.junit.Assert.assertNotNull;%0A
 import s
@@ -1276,24 +1276,24 @@
 0), %22one%22);%0A
-
     assertEq
@@ -1337,24 +1337,1496 @@
 two%22);%0A  %7D%0A%0A
+  @Test%0A  public void testSchema() throws IOException %7B%0A%0A    OrientDB orientDB = new OrientDB(%22embedded:%22, OrientDBConfig.defaultConfig());%0A    orientDB.create(%22test%22, ODatabaseType.MEMORY);%0A    ODatabaseSession session = orientDB.open(%22test%22, %22admin%22, %22admin%22);%0A    ODocument schema = ((ODatabaseDocumentInternal) session).getSharedContext().getSchema().toStream();%0A    session.close();%0A    orientDB.close();%0A    MockChannel channel = new MockChannel();%0A%0A    OPushSchemaRequest request = new OPushSchemaRequest(schema);%0A    request.write(channel);%0A    channel.close();%0A%0A    OPushSchemaRequest readRequest = new OPushSchemaRequest();%0A    readRequest.read(channel);%0A    assertNotNull(readRequest.getSchema());%0A%0A  %7D%0A%0A  @Test%0A  public void testIndexManager() throws IOException %7B%0A%0A    OrientDB orientDB = new OrientDB(%22embedded:%22, OrientDBConfig.defaultConfig());%0A    orientDB.create(%22test%22, ODatabaseType.MEMORY);%0A    ODatabaseSession session = orientDB.open(%22test%22, %22admin%22, %22admin%22);%0A    ODocument schema = ((ODatabaseDocumentInternal) session).getSharedContext().getIndexManager().toStream();%0A    session.close();%0A    orientDB.close();%0A    MockChannel channel = new MockChannel();%0A%0A    OPushIndexManagerRequest request = new OPushIndexManagerRequest(schema);%0A    request.write(channel);%0A    channel.close();%0A%0A    OPushIndexManagerRequest readRequest = new OPushIndexManagerRequest();%0A    readRequest.read(channel);%0A    assertNotNull(readRequest.getIndexManager());%0A%0A  %7D%0A%0A
   @Test%0A  pu

add basic tests for new push messages serialization