BAEL-1524: Chain of Responsibility Design Pattern in Java (#3573)

* BAEL-1422: measure performance of Random and ThreadLocalRandom using JMH

* BAEL-1422: updated benchmarking examples of Random and ThreadLocalRandom to use newWorkStealingPool that leverages ForkJoinPool

* BAEL-1422: refactored benchmarking examples for comparing performance of ThreadLocalRandom and Random
- initialised the collection of Callable before running benchmarking
- removed for loop for submitting task and instead used executor.invokeAll(collection_of_callable)

* BAEL-1282: added TDD type junit tests for geospatial queries elasticsearch

* BAEL-1524: added example for chain of responsibility design pattern

* BAEL-1524: added BDD style jUnit test to test unknown handler in ChainOfResponsibility design pattern

* BAEL-1524: refactored ChainOfResponsibility design pattern example

* BAEL-1524: refactored ChainOfResponsibility design pattern example

* BAEL-1524: updated ChainOfResponsibility design pattern example

* BAEL-1524: updated ChainOfResponsibility design pattern example
This commit is contained in:
Tarang Bhalodia 2018-02-19 22:19:48 +05:30 committed by Grzegorz Piwowarek
parent c0bf1c7a93
commit a243d8494d
8 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,13 @@
package com.baeldung.designpatterns.chainofresponsibility;
public abstract class AuthenticationProcessor {
// next element in chain or responsibility
public AuthenticationProcessor nextProcessor;
public AuthenticationProcessor(AuthenticationProcessor nextProcessor) {
this.nextProcessor = nextProcessor;
}
public abstract boolean isAuthorized(AuthenticationProvider authProvider);
}

View File

@ -0,0 +1,5 @@
package com.baeldung.designpatterns.chainofresponsibility;
public interface AuthenticationProvider {
}

View File

@ -0,0 +1,21 @@
package com.baeldung.designpatterns.chainofresponsibility;
public class OAuthAuthenticationProcessor extends AuthenticationProcessor {
public OAuthAuthenticationProcessor(AuthenticationProcessor nextProcessor) {
super(nextProcessor);
}
@Override
public boolean isAuthorized(AuthenticationProvider authProvider) {
if (authProvider instanceof OAuthTokenProvider) {
return Boolean.TRUE;
} else if (nextProcessor != null) {
return nextProcessor.isAuthorized(authProvider);
} else {
return Boolean.FALSE;
}
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.designpatterns.chainofresponsibility;
public class OAuthTokenProvider implements AuthenticationProvider {
}

View File

@ -0,0 +1,5 @@
package com.baeldung.designpatterns.chainofresponsibility;
public class SamlAuthenticationProvider implements AuthenticationProvider {
}

View File

@ -0,0 +1,20 @@
package com.baeldung.designpatterns.chainofresponsibility;
public class UsernamePasswordAuthenticationProcessor extends AuthenticationProcessor {
public UsernamePasswordAuthenticationProcessor(AuthenticationProcessor nextProcessor) {
super(nextProcessor);
}
@Override
public boolean isAuthorized(AuthenticationProvider authProvider) {
if (authProvider instanceof UsernamePasswordProvider) {
return Boolean.TRUE;
} else if (nextProcessor != null) {
return nextProcessor.isAuthorized(authProvider);
} else {
return Boolean.FALSE;
}
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.designpatterns.chainofresponsibility;
public class UsernamePasswordProvider implements AuthenticationProvider {
}

View File

@ -0,0 +1,37 @@
package com.baeldung.designpatterns.chainofresponsibility;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class ChainOfResponsibilityTest {
private static AuthenticationProcessor getChainOfAuthProcessor() {
AuthenticationProcessor oAuthProcessor = new OAuthAuthenticationProcessor(null);
AuthenticationProcessor unamePasswordProcessor = new UsernamePasswordAuthenticationProcessor(oAuthProcessor);
return unamePasswordProcessor;
}
@Test
public void givenOAuthProvider_whenCheckingAuthorized_thenSuccess() {
AuthenticationProcessor authProcessorChain = getChainOfAuthProcessor();
boolean isAuthorized = authProcessorChain.isAuthorized(new OAuthTokenProvider());
assertTrue(isAuthorized);
}
@Test
public void givenUsernamePasswordProvider_whenCheckingAuthorized_thenSuccess() {
AuthenticationProcessor authProcessorChain = getChainOfAuthProcessor();
boolean isAuthorized = authProcessorChain.isAuthorized(new UsernamePasswordProvider());
assertTrue(isAuthorized);
}
@Test
public void givenSamlAuthProvider_whenCheckingAuthorized_thenFailure() {
AuthenticationProcessor authProcessorChain = getChainOfAuthProcessor();
boolean isAuthorized = authProcessorChain.isAuthorized(new SamlAuthenticationProvider());
assertTrue(!isAuthorized);
}
}