/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.waveprotocol.box.server.rpc;

import com.google.protobuf.Message;
import com.google.protobuf.UnknownFieldSet;

import junit.framework.TestCase;

import org.waveprotocol.box.common.comms.WaveClientRpc;
import org.waveprotocol.wave.model.wave.ParticipantId;

/**
 * @author arb@google.com
 */
public class WebSocketChannelTest extends TestCase {
  private TestWebSocketChannel channel;
  private TestCallback callback;
  private static final int SEQUENCE_NUMBER = 5;

  class TestWebSocketChannel extends WebSocketChannel {
    String message;

    public TestWebSocketChannel(ProtoCallback callback) {
      super(callback);
      this.message = null;
    }

    @Override
    public void sendMessageString(final String data) {
      this.message = data;
    }
  }

  class TestCallback implements ProtoCallback {
      Message savedMessage = null;
      long sequenceNumber;

      @Override
      public void message(int sequenceNo, final Message message) {
        this.sequenceNumber = sequenceNo;
        this.savedMessage = message;
      }
    }

  @Override
  public void setUp() {
    callback = new TestCallback();
    channel = new TestWebSocketChannel(callback);
  }

  public void testRoundTrippingJson() throws Exception {
    WaveClientRpc.ProtocolOpenRequest.Builder sourceBuilder = buildProtocolOpenRequest();
    checkRoundtripping(sourceBuilder);
  }

  public void testRoundTrippingJsonRepeatedField() throws Exception {
    WaveClientRpc.ProtocolOpenRequest.Builder sourceBuilder = buildProtocolOpenRequest();
    sourceBuilder.addWaveletIdPrefix("aaa");
    sourceBuilder.addWaveletIdPrefix("bbb");
    sourceBuilder.addWaveletIdPrefix("ccc");
    checkRoundtripping(sourceBuilder);
  }

  private void checkRoundtripping(final WaveClientRpc.ProtocolOpenRequest.Builder sourceBuilder) {
    WaveClientRpc.ProtocolOpenRequest sourceRequest = sourceBuilder.build();
    channel.sendMessage(SEQUENCE_NUMBER, sourceRequest);
    String sentRequest = channel.message;
    assertNotNull(sentRequest);
    System.out.println(sentRequest);
    channel.handleMessageString(sentRequest);
    assertNotNull(callback.savedMessage);
    assertEquals(SEQUENCE_NUMBER, callback.sequenceNumber);
    assertEquals(sourceRequest, callback.savedMessage);
  }

  private WaveClientRpc.ProtocolOpenRequest.Builder buildProtocolOpenRequest() {
    WaveClientRpc.ProtocolOpenRequest.Builder sourceBuilder =
        WaveClientRpc.ProtocolOpenRequest.newBuilder();
    sourceBuilder.setParticipantId("test@example.com");
    sourceBuilder.setWaveId("example.com!w+test");
    return sourceBuilder;
  }
}

@@ -855,36 +855,57 @@
 ort 
-com.google.protobuf.Message;
+org.waveprotocol.box.common.comms.WaveClientRpc;%0A
 %0Aimp
@@ -932,23 +932,15 @@
 buf.
-UnknownFieldSet
+Message
 ;%0A%0Ai
@@ -976,120 +976,8 @@
 e;%0A%0A
-import org.waveprotocol.box.common.comms.WaveClientRpc;%0Aimport org.waveprotocol.wave.model.wave.ParticipantId;%0A%0A
 /**%0A
@@ -1532,18 +1532,16 @@
 k %7B%0A    
-  
 Message 
@@ -1561,18 +1561,16 @@
 = null;%0A
-  
     long
@@ -1591,18 +1591,16 @@
 r;%0A%0A    
-  
 @Overrid
@@ -1605,18 +1605,16 @@
 ide%0A    
- 
- 
 public v
@@ -1662,26 +1662,24 @@
  message) %7B%0A
-  
       this.s
@@ -1708,26 +1708,24 @@
 ceNo;%0A      
-  
 this.savedMe
@@ -1745,22 +1745,108 @@
 ge;%0A    
+%7D%0A%0A    @Override%0A    public void cancel() %7B%0A      // TODO Auto-generated method stub%0A%0A  
   %7D%0A
-  
   %7D%0A%0A  @
@@ -3197,16 +3197,8 @@
 er =
-%0A       
  Wav
@@ -3227,16 +3227,25 @@
 nRequest
+%0A        
 .newBuil

Fix unit test