package ws.wamp.jawampa.roles;

import static org.junit.Assert.*;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import rx.Observer;
import rx.subjects.PublishSubject;
import ws.wamp.jawampa.PubSubData;
import ws.wamp.jawampa.Reply;
import ws.wamp.jawampa.ids.PublicationId;
import ws.wamp.jawampa.ids.RequestId;
import ws.wamp.jawampa.ids.SubscriptionId;
import ws.wamp.jawampa.io.BaseClient;
import ws.wamp.jawampa.messages.CallMessage;
import ws.wamp.jawampa.messages.EventMessage;
import ws.wamp.jawampa.messages.SubscribeMessage;
import ws.wamp.jawampa.messages.SubscribedMessage;
import ws.wamp.jawampa.messages.UnsubscribeMessage;
import ws.wamp.jawampa.messages.WampMessage;

public class SubscriberTest {
    @Mock private BaseClient baseClient;

    private String topic = "some_topic";
    @Mock private ArrayNode arguments;
    @Mock private ObjectNode kwArguments;

    @Before
    public void setup() {
        initMocks( this );
    }

    @Test
    public void testSubscribeSendsSubscribeMessage() {
        Subscriber subject = new Subscriber( baseClient );
        PublishSubject<PubSubData> resultSubject = PublishSubject.create();

        when( baseClient.getNewRequestId() ).thenReturn( RequestId.of( 42L ) );

        subject.subscribe( topic, resultSubject );

        ArgumentMatcher<WampMessage> messageMatcher = new ArgumentMatcher<WampMessage>() {
            @Override
            public boolean matches( Object argument ) {
                SubscribeMessage message = (SubscribeMessage)argument;
                if ( !message.requestId.equals( RequestId.of( 42L ) ) ) return false;
                if ( message.topic != topic ) return false;
                return true;
            }
        };
        verify( baseClient ).scheduleMessageToRouter( argThat( messageMatcher ) );
    }

    @Test
    public void testEventIsDeliveredAfterSubscription() {
        Subscriber subject = new Subscriber( baseClient );

        PublishSubject<PubSubData> resultSubject = PublishSubject.create();
        @SuppressWarnings( "unchecked" )
        Observer<PubSubData> publicationObserver = mock(Observer.class);
        resultSubject.subscribe( publicationObserver );

        when( baseClient.getNewRequestId() ).thenReturn( RequestId.of( 42L ) );

        subject.subscribe( topic, resultSubject );

        subject.onSubscribed( new SubscribedMessage( RequestId.of( 42L ), SubscriptionId.of( 23L ) ) );
        subject.onEvent( new EventMessage( SubscriptionId.of( 23L ), PublicationId.of( 17L ), null, arguments, kwArguments ) );

        ArgumentMatcher<PubSubData> dataMatcher = new ArgumentMatcher<PubSubData>() {
            @Override
            public boolean matches( Object argument ) {
                PubSubData data = (PubSubData)argument;
                if ( data.arguments() != arguments ) return false;
                if ( data.keywordArguments() != kwArguments ) return false;
                return true;
            }
        };
        verify( publicationObserver ).onNext( argThat( dataMatcher ) );
    }
}

@@ -47,146 +47,32 @@
 org.
-junit.Assert.*;%0Aimport static org.mockito.Matchers.argThat;%0Aimport static org.mockito.Matchers.any;%0Aimport static org.mockito.Mockito.mock
+mockito.Matchers.argThat
 ;%0Aim
@@ -237,33 +237,8 @@
 re;%0A
-import org.junit.Ignore;%0A
 impo
@@ -322,118 +322,8 @@
 k;%0A%0A
-import com.fasterxml.jackson.databind.node.ArrayNode;%0Aimport com.fasterxml.jackson.databind.node.ObjectNode;%0A%0A
 impo
@@ -412,38 +412,8 @@
 ta;%0A
-import ws.wamp.jawampa.Reply;%0A
 impo
@@ -573,53 +573,8 @@
 nt;%0A
-import ws.wamp.jawampa.messages.CallMessage;%0A
 impo
@@ -752,19 +752,12 @@
 ges.
-Unsubscribe
+Wamp
 Mess
@@ -765,50 +765,115 @@
 ge;%0A
+%0A
 import 
-ws.wamp.jawampa.messages.WampMessag
+com.fasterxml.jackson.databind.node.ArrayNode;%0Aimport com.fasterxml.jackson.databind.node.ObjectNod
 e;%0A%0A
@@ -1075,159 +1075,224 @@
     
-@Before%0A    public void setup() %7B%0A        initMocks( this );%0A    %7D%0A%0A    @Test%0A    public void testSubscribeSendsSubscribeMessage() %7B%0A        Subscriber
+private Subscriber subject;%0A    private PublishSubject%3CPubSubData%3E resultSubject;%0A    @Mock private Observer%3CPubSubData%3E publicationObserver;%0A%0A    @Before%0A    public void setup() %7B%0A        initMocks( this );%0A%0A       
  sub
@@ -1328,24 +1328,25 @@
 ient );%0A
+%0A
         
 PublishS
@@ -1341,35 +1341,8 @@
     
-PublishSubject%3CPubSubData%3E 
 resu
@@ -1375,16 +1375,72 @@
 .create(
+);%0A        resultSubject.subscribe( publicationObserver 
 );%0A%0A    
@@ -1507,32 +1507,103 @@
 Id.of( 42L ) );%0A
+    %7D%0A%0A    @Test%0A    public void testSubscribeSendsSubscribeMessage() %7B
 %0A        subject
@@ -2241,396 +2241,8 @@
 ) %7B%0A
-        Subscriber subject = new Subscriber( baseClient );%0A%0A        PublishSubject%3CPubSubData%3E resultSubject = PublishSubject.create();%0A        @SuppressWarnings( %22unchecked%22 )%0A        Observer%3CPubSubData%3E publicationObserver = mock(Observer.class);%0A        resultSubject.subscribe( publicationObserver );%0A%0A        when( baseClient.getNewRequestId() ).thenReturn( RequestId.of( 42L ) );%0A%0A
     

Use setup() to setup test fixture