package org.codesharp.traffic;

import static org.easymock.classextension.EasyMock.*;

import org.junit.Test;

public class NodeTrafficTest {
	@Test
	public void traffic_test() {
		Object req = "req";
		Object rep = "rep";
		Object msg = "msg";
		Object ack = "ack";
		Object req_body = "req_body";
		Object rep_body = "rep_body";
		Object outId = 1;
		Object dst = "C3";
		Object n_id_1 = 1;
		Object n_id_2 = 2;
		Object n_id_3 = 3;
		
		MessageHelper helper = createStrictMock(MessageHelper.class);
		Router r1 = newRouter(helper, n_id_1, "R2", null);
		Connection c2 = newConnection(helper, n_id_2, "C2", r1);
		Connection c3 = newConnection(helper, n_id_3, dst, r1);
		r1.register(c2);
		r1.register(c3);
		
		// c2-r1->c3
		expect(helper.getCommand(req)).andReturn(Commands.REQ);
		expect(helper.newMessage(req, c2)).andReturn(msg);
		expect(helper.getCommand(msg)).andReturn(Commands.MSG);
		expect(helper.getDestination(msg)).andReturn(dst);
		expect(helper.getCommand(msg)).andReturn(Commands.MSG);
		expect(helper.getOutId(msg)).andReturn(outId);
		expect(helper.getBody(msg)).andReturn(req_body);
		// c3->r1
		expect(helper.getCommand(rep)).andReturn(Commands.REP);
		expect(helper.getOutId(rep)).andReturn(outId);
		expect(helper.newAck(msg, rep)).andReturn(ack);
		expect(helper.getCommand(ack)).andReturn(Commands.ACK);
		expect(helper.getNext(ack)).andReturn(n_id_1);
		// r1->c2
		expect(helper.getCommand(ack)).andReturn(Commands.ACK);
		expect(helper.getNext(ack)).andReturn(n_id_2);
		expect(helper.getCommand(ack)).andReturn(Commands.ACK);
		expect(helper.getBody(ack)).andReturn(rep_body);
		replay(helper);
		
		System.out.println(String.format("%s#%s -> %s", c2.flag(), c2.id(), req));
		c2.onMessage(req);
		System.out.println(String.format("%s#%s -> %s", c3.flag(), c3.id(), rep));
		c3.onMessage(rep);
	}
	
	private Router newRouter(MessageHelper helper, final Object id, final Object flag, final Node next) {
		return new Router(helper) {
			@Override
			public Object id() {
				return id;
			}
			
			@Override
			public Object flag() {
				return flag;
			}
			
			@Override
			public void send(Object msg) {
				this.onMessage(msg);
			}
			
			@Override
			public Node next() {
				return next;
			}
		};
	}
	
	private Connection newConnection(MessageHelper helper, final Object id, final Object flag, final Node next) {
		return new Connection(helper) {
			@Override
			public Object id() {
				return id;
			}
			
			@Override
			public Object flag() {
				return flag;
			}
			
			@Override
			protected void flush(Object msg) {
				System.out.println(String.format("%s -> %s#%s", msg, this.flag(), this.id()));
			}
			
			@Override
			public Node next() {
				return next;
			}
		};
	}
}

@@ -155,16 +155,24 @@
 ic void 
+c1_r_c2_
 traffic_
@@ -369,17 +369,17 @@
 dst = %22C
-3
+2
 %22;%0A%09%09Obj
@@ -386,18 +386,16 @@
 ect 
-n
+r
 _id
-_1
  = 
-1
+0
 ;%0A%09%09
@@ -405,18 +405,17 @@
 ect 
-n
+c1
 _id
-_2
  = 
-2
+1
 ;%0A%09%09
@@ -425,18 +425,17 @@
 ect 
-n
+c2
 _id
-_3
  = 
-3
+2
 ;%0A%09%09
@@ -509,17 +509,16 @@
 Router r
-1
  = newRo
@@ -534,19 +534,16 @@
 er, 
-n
+r
 _id
-_1
 , %22R
-2
 %22, n
@@ -562,17 +562,17 @@
 ection c
-2
+1
  = newCo
@@ -592,24 +592,22 @@
 er, 
-n
+c1
 _id
-_2
 , %22C
-2
+1
 %22, r
-1
 );%0A%09
@@ -619,17 +619,17 @@
 ection c
-3
+2
  = newCo
@@ -649,22 +649,21 @@
 er, 
-n
+c2
 _id
-_3
 , dst, r
 1);%0A
@@ -662,16 +662,14 @@
 t, r
-1
 );%0A%09%09r
-1
 .reg
@@ -679,16 +679,15 @@
 er(c
-2
+1
 );%0A%09%09r
-1
 .reg
@@ -693,17 +693,17 @@
 gister(c
-3
+2
 );%0A%09%09%0A%09%09
@@ -806,17 +806,17 @@
 e(req, c
-2
+1
 )).andRe
@@ -1366,14 +1366,12 @@
 urn(
-n
+r
 _id
-_1
 );%0A%09
@@ -1483,14 +1483,13 @@
 urn(
-n
+c1
 _id
-_2
 );%0A%09
@@ -1668,17 +1668,17 @@
 %3E %25s%22, c
-2
+1
 .flag(),
@@ -1679,17 +1679,17 @@
 lag(), c
-2
+1
 .id(), r
@@ -1697,17 +1697,17 @@
 q));%0A%09%09c
-2
+1
 .onMessa
@@ -1766,17 +1766,17 @@
 %3E %25s%22, c
-3
+2
 .flag(),
@@ -1777,17 +1777,17 @@
 lag(), c
-3
+2
 .id(), r
@@ -1795,17 +1795,17 @@
 p));%0A%09%09c
-3
+2
 .onMessa

rename test c1_r_c2_traffic_test