Skip to content

Commit a243d84

Browse files
tarangbhalodiapivovarit
authored andcommitted
BAEL-1524: Chain of Responsibility Design Pattern in Java (eugenp#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
1 parent c0bf1c7 commit a243d84

8 files changed

Lines changed: 111 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.designpatterns.chainofresponsibility;
2+
3+
public abstract class AuthenticationProcessor {
4+
5+
// next element in chain or responsibility
6+
public AuthenticationProcessor nextProcessor;
7+
8+
public AuthenticationProcessor(AuthenticationProcessor nextProcessor) {
9+
this.nextProcessor = nextProcessor;
10+
}
11+
12+
public abstract boolean isAuthorized(AuthenticationProvider authProvider);
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.designpatterns.chainofresponsibility;
2+
3+
public interface AuthenticationProvider {
4+
5+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.designpatterns.chainofresponsibility;
2+
3+
public class OAuthAuthenticationProcessor extends AuthenticationProcessor {
4+
5+
public OAuthAuthenticationProcessor(AuthenticationProcessor nextProcessor) {
6+
super(nextProcessor);
7+
}
8+
9+
@Override
10+
public boolean isAuthorized(AuthenticationProvider authProvider) {
11+
12+
if (authProvider instanceof OAuthTokenProvider) {
13+
return Boolean.TRUE;
14+
} else if (nextProcessor != null) {
15+
return nextProcessor.isAuthorized(authProvider);
16+
} else {
17+
return Boolean.FALSE;
18+
}
19+
}
20+
21+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.designpatterns.chainofresponsibility;
2+
3+
public class OAuthTokenProvider implements AuthenticationProvider {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.designpatterns.chainofresponsibility;
2+
3+
public class SamlAuthenticationProvider implements AuthenticationProvider {
4+
5+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.designpatterns.chainofresponsibility;
2+
3+
public class UsernamePasswordAuthenticationProcessor extends AuthenticationProcessor {
4+
5+
public UsernamePasswordAuthenticationProcessor(AuthenticationProcessor nextProcessor) {
6+
super(nextProcessor);
7+
}
8+
9+
@Override
10+
public boolean isAuthorized(AuthenticationProvider authProvider) {
11+
if (authProvider instanceof UsernamePasswordProvider) {
12+
return Boolean.TRUE;
13+
} else if (nextProcessor != null) {
14+
return nextProcessor.isAuthorized(authProvider);
15+
} else {
16+
return Boolean.FALSE;
17+
}
18+
}
19+
20+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.designpatterns.chainofresponsibility;
2+
3+
public class UsernamePasswordProvider implements AuthenticationProvider {
4+
5+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.designpatterns.chainofresponsibility;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertTrue;
6+
7+
public class ChainOfResponsibilityTest {
8+
9+
private static AuthenticationProcessor getChainOfAuthProcessor() {
10+
11+
AuthenticationProcessor oAuthProcessor = new OAuthAuthenticationProcessor(null);
12+
AuthenticationProcessor unamePasswordProcessor = new UsernamePasswordAuthenticationProcessor(oAuthProcessor);
13+
return unamePasswordProcessor;
14+
}
15+
16+
@Test
17+
public void givenOAuthProvider_whenCheckingAuthorized_thenSuccess() {
18+
AuthenticationProcessor authProcessorChain = getChainOfAuthProcessor();
19+
boolean isAuthorized = authProcessorChain.isAuthorized(new OAuthTokenProvider());
20+
assertTrue(isAuthorized);
21+
}
22+
23+
@Test
24+
public void givenUsernamePasswordProvider_whenCheckingAuthorized_thenSuccess() {
25+
AuthenticationProcessor authProcessorChain = getChainOfAuthProcessor();
26+
boolean isAuthorized = authProcessorChain.isAuthorized(new UsernamePasswordProvider());
27+
assertTrue(isAuthorized);
28+
}
29+
30+
@Test
31+
public void givenSamlAuthProvider_whenCheckingAuthorized_thenFailure() {
32+
AuthenticationProcessor authProcessorChain = getChainOfAuthProcessor();
33+
boolean isAuthorized = authProcessorChain.isAuthorized(new SamlAuthenticationProvider());
34+
assertTrue(!isAuthorized);
35+
}
36+
37+
}

0 commit comments

Comments
 (0)