// Path: src/main/java/org/wmaop/aop/interceptor/FlowPosition.java
// public class FlowPosition {
// 
// 	public final String packageName;
// 	public final String serviceName;
// 	public final String fqname;
// 	private InterceptPoint interceptPoint;
// 	
// 	public FlowPosition(InterceptPoint point, String fqname) {
// 		interceptPoint = point;
// 		if (fqname == null) {
// 			serviceName = "";
// 			packageName = "";
// 			this.fqname = "";
// 		} else {
// 			this.fqname = fqname;
// 			int pkgsep = fqname.lastIndexOf(':');
// 			if (pkgsep == -1) {
// 				serviceName = fqname;
// 				packageName = "";
// 			} else {
// 				serviceName = fqname.substring(pkgsep + 1);
// 				packageName = fqname.substring(0, pkgsep);
// 			}
// 		}
// 	}
// 	
// 	@Override
// 	public String toString() {
// 		return fqname;
// 	}
// 
// 	public void setInterceptPoint(InterceptPoint point) {
// 		interceptPoint = point;
// 	}
// 
// 	public InterceptPoint getInterceptPoint() {
// 		return interceptPoint;
// 	}
// 
// 	public String getPackageName() {
// 		return packageName;
// 	}
// 
// 	public String getServiceName() {
// 		return serviceName;
// 	}
// 
// 	public String getFqname() {
// 		return fqname;
// 	}
// }
// 
// Path: src/main/java/org/wmaop/aop/interceptor/InterceptPoint.java
// public enum InterceptPoint {
// 		BEFORE,INVOKE,AFTER;
// 
// }


import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Stack;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.wmaop.aop.interceptor.FlowPosition;
import org.wmaop.aop.interceptor.InterceptPoint;
import com.wm.app.b2b.server.InvokeState;
import com.wm.app.b2b.server.Package;
import com.wm.lang.ns.NSName;
import com.wm.lang.ns.NSService;

package org.wmaop.aop.matcher;





@RunWith(PowerMockRunner.class)
@PrepareForTest(InvokeState.class)
public class CallingServicePositionMatcherTest {

	private static final String PUB_FOO_BAR = "pub.foo:bar";

	@Test
	public void shouldMatchCorrectNamespacePackage() {
		NSName nsnTestService = NSName.create("org.wmaop.pub:testService");
		NSName nsnBarService = NSName.create(PUB_FOO_BAR);
		
		Package pkg1 = mock(Package.class);
		when(pkg1.getName()).thenReturn("pkg1");
		Package pkg2 = mock(Package.class);
		when(pkg2.getName()).thenReturn("pkg2");

		PowerMockito.mockStatic(InvokeState.class);
		InvokeState mockInvokeState = mock(InvokeState.class);
		when(InvokeState.getCurrentState()).thenReturn(mockInvokeState);

		NSService svcTest = mock(NSService.class);
		when(svcTest.getNSName()).thenReturn(nsnTestService);
		when(svcTest.getPackage()).thenReturn(pkg1);
		
		NSService svcBar = mock(NSService.class);
		when(svcBar.getNSName()).thenReturn(nsnBarService);
		when(svcBar.getPackage()).thenReturn(pkg2);
		
		Stack<NSService> callStack = new Stack<>();
		callStack.push(svcTest);
		callStack.push(svcBar);
		when(mockInvokeState.getCallStack()).thenReturn(callStack);
		
		FlowPosition fp = new FlowPosition(InterceptPoint.INVOKE, PUB_FOO_BAR);
		
		assertFalse(new CallingServicePositionMatcher("id", PUB_FOO_BAR, null).match(null).isMatch());

// Path: src/main/java/org/wmaop/aop/interceptor/FlowPosition.java
// public class FlowPosition {
// 
// 	public final String packageName;
// 	public final String serviceName;
// 	public final String fqname;
// 	private InterceptPoint interceptPoint;
// 	
// 	public FlowPosition(InterceptPoint point, String fqname) {
// 		interceptPoint = point;
// 		if (fqname == null) {
// 			serviceName = "";
// 			packageName = "";
// 			this.fqname = "";
// 		} else {
// 			this.fqname = fqname;
// 			int pkgsep = fqname.lastIndexOf(':');
// 			if (pkgsep == -1) {
// 				serviceName = fqname;
// 				packageName = "";
// 			} else {
// 				serviceName = fqname.substring(pkgsep + 1);
// 				packageName = fqname.substring(0, pkgsep);
// 			}
// 		}
// 	}
// 	
// 	@Override
// 	public String toString() {
// 		return fqname;
// 	}
// 
// 	public void setInterceptPoint(InterceptPoint point) {
// 		interceptPoint = point;
// 	}
// 
// 	public InterceptPoint getInterceptPoint() {
// 		return interceptPoint;
// 	}
// 
// 	public String getPackageName() {
// 		return packageName;
// 	}
// 
// 	public String getServiceName() {
// 		return serviceName;
// 	}
// 
// 	public String getFqname() {
// 		return fqname;
// 	}
// }
// 
// Path: src/main/java/org/wmaop/aop/interceptor/InterceptPoint.java
// public enum InterceptPoint {
// 		BEFORE,INVOKE,AFTER;
// 
// }


// Path: src/test/java/org/wmaop/aop/matcher/CallingServicePositionMatcherTest.java
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Stack;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.wmaop.aop.interceptor.FlowPosition;
import org.wmaop.aop.interceptor.InterceptPoint;
import com.wm.app.b2b.server.InvokeState;
import com.wm.app.b2b.server.Package;
import com.wm.lang.ns.NSName;
import com.wm.lang.ns.NSService;

package org.wmaop.aop.matcher;





@RunWith(PowerMockRunner.class)
@PrepareForTest(InvokeState.class)
public class CallingServicePositionMatcherTest {

	private static final String PUB_FOO_BAR = "pub.foo:bar";

	@Test
	public void shouldMatchCorrectNamespacePackage() {
		NSName nsnTestService = NSName.create("org.wmaop.pub:testService");
		NSName nsnBarService = NSName.create(PUB_FOO_BAR);
		
		Package pkg1 = mock(Package.class);
		when(pkg1.getName()).thenReturn("pkg1");
		Package pkg2 = mock(Package.class);
		when(pkg2.getName()).thenReturn("pkg2");

		PowerMockito.mockStatic(InvokeState.class);
		InvokeState mockInvokeState = mock(InvokeState.class);
		when(InvokeState.getCurrentState()).thenReturn(mockInvokeState);

		NSService svcTest = mock(NSService.class);
		when(svcTest.getNSName()).thenReturn(nsnTestService);
		when(svcTest.getPackage()).thenReturn(pkg1);
		
		NSService svcBar = mock(NSService.class);
		when(svcBar.getNSName()).thenReturn(nsnBarService);
		when(svcBar.getPackage()).thenReturn(pkg2);
		
		Stack<NSService> callStack = new Stack<>();
		callStack.push(svcTest);
		callStack.push(svcBar);
		when(mockInvokeState.getCallStack()).thenReturn(callStack);
		
		FlowPosition fp = new FlowPosition(InterceptPoint.INVOKE, PUB_FOO_BAR);
		
		assertFalse(new CallingServicePositionMatcher("id", PUB_FOO_BAR, null).match(null).isMatch());

		assertFalse(new CallingServicePositionMatcher("id", PUB_FOO_BAR, null).match( new FlowPosition(InterceptPoint.INVOKE, "nother:service")).isMatch());
