package org.openur.remoting.resource.security;

import static org.junit.Assert.*;

import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;

import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.Test;
import org.openur.module.service.security.IAuthorizationServices;
import org.openur.remoting.resource.AbstractResourceTest;

public class AuthorizationResourceTest
	extends AbstractResourceTest
{
	@Override
	protected Application configure()
	{
		AbstractBinder binder = new AbstractBinder()
		{
			@Override
			protected void configure()
			{
				bindFactory(MockAuthorizationServicesFactory.class).to(IAuthorizationServices.class);
			}
		};
		
		ResourceConfig config = new ResourceConfig(AuthorizationResource.class)
			.register(binder);

		return config;
	}
	
	@Test
	public void testHasPermissionInOrgUnit()
	{
		String result = performRestCall("authorization/ou" + "?personId=" + MockAuthorizationServicesFactory.PERSON_ID + "&ouId="
			+ MockAuthorizationServicesFactory.OU_ID + "&text=" + MockAuthorizationServicesFactory.PERMISSION_TEXT
			+ "&appName=" + MockAuthorizationServicesFactory.APP_NAME, MediaType.TEXT_PLAIN, String.class);
		
		Boolean b = Boolean.valueOf(result);
		
		assertTrue(b);
	}
	
	@Test
	public void testHasNoPermissionInOrgUnit()
	{
		String result = performRestCall("authorization/ou" + "?personId=" + MockAuthorizationServicesFactory.PERSON_ID + "&ouId="
			+ MockAuthorizationServicesFactory.OU_ID + "&text=" + MockAuthorizationServicesFactory.OTHER_PERMISSION_TEXT
			+ "&appName=" + MockAuthorizationServicesFactory.APP_NAME, MediaType.TEXT_PLAIN, String.class);
		
		Boolean b = Boolean.valueOf(result);
		
		assertFalse(b);
	}

	@Test
	public void testHasPermissionInSystem()
	{
		String result = performRestCall("authorization/system" + "?personId=" + MockAuthorizationServicesFactory.PERSON_ID 
			+ "&text=" + MockAuthorizationServicesFactory.PERMISSION_TEXT
			+ "&appName=" + MockAuthorizationServicesFactory.APP_NAME, MediaType.TEXT_PLAIN, String.class);
		
		Boolean b = Boolean.valueOf(result);
		
		assertTrue(b);
	}

	@Test
	public void testHasNotPermissionInSystem()
	{
		String result = performRestCall("authorization/system" + "?personId=" + MockAuthorizationServicesFactory.PERSON_ID 
			+ "&text=" + MockAuthorizationServicesFactory.OTHER_PERMISSION_TEXT
			+ "&appName=" + MockAuthorizationServicesFactory.APP_NAME, MediaType.TEXT_PLAIN, String.class);
		
		Boolean b = Boolean.valueOf(result);
		
		assertFalse(b);
	}
}

@@ -898,37 +898,33 @@
 Unit()%0A%09%7B%0A%09%09
-String result
+Boolean b
  = performRe
@@ -1214,62 +1214,21 @@
 IN, 
-String.class);%0A%09%09%0A%09%09Boolean b = Boolean.valueOf(result
+Boolean.class
 );%0A%09
@@ -1303,37 +1303,33 @@
 Unit()%0A%09%7B%0A%09%09
-String result
+Boolean b
  = performRe
@@ -1625,62 +1625,21 @@
 IN, 
-String.class);%0A%09%09%0A%09%09Boolean b = Boolean.valueOf(result
+Boolean.class
 );%0A%09
@@ -1711,37 +1711,33 @@
 stem()%0A%09%7B%0A%09%09
-String result
+Boolean b
  = performRe
@@ -1980,62 +1980,21 @@
 IN, 
-String.class);%0A%09%09%0A%09%09Boolean b = Boolean.valueOf(result
+Boolean.class
 );%0A%09
@@ -2076,21 +2076,17 @@
 %7B%0A%09%09
-String result
+Boolean b
  = p
@@ -2343,62 +2343,21 @@
 IN, 
-String.class);%0A%09%09%0A%09%09Boolean b = Boolean.valueOf(result
+Boolean.class
 );%0A%09

use generic functionally when determing return-type.