-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improving http message robustness (#143)
- Loading branch information
1 parent
c179ce0
commit 665ad03
Showing
7 changed files
with
317 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
message/message-http/src/test/java/com/mastercard/test/flow/msg/http/Combinator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.mastercard.test.flow.msg.http; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Spliterator; | ||
import java.util.Spliterators; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
|
||
/** | ||
* Generates combinations of list members | ||
* | ||
* @param <T> The combination member type | ||
* @param <C> The collection type to return | ||
*/ | ||
class Combinator<T, C extends Collection<T>> implements Iterator<C> { | ||
|
||
private final boolean[] included; | ||
private boolean fullSetReturned = false; | ||
private final List<T> elements; | ||
private final Supplier<C> collection; | ||
|
||
/** | ||
* @param collection How to build a collection of the desired return type | ||
* @param members The members to combine | ||
*/ | ||
@SafeVarargs | ||
public Combinator( Supplier<C> collection, T... members ) { | ||
elements = Arrays.asList( members ); | ||
included = new boolean[elements.size()]; | ||
this.collection = collection; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return !fullSetReturned; | ||
} | ||
|
||
@Override | ||
public C next() { | ||
|
||
C result = collection.get(); | ||
for( int i = 0; i < included.length; i++ ) { | ||
if( included[i] ) { | ||
result.add( elements.get( i ) ); | ||
} | ||
} | ||
|
||
fullSetReturned = allSet(); | ||
|
||
for( int i = 0; i < included.length; i++ ) { | ||
if( !included[i] ) { | ||
included[i] = true; | ||
break; | ||
} | ||
included[i] = false; | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* @return A stream of the combinations | ||
*/ | ||
public Stream<C> stream() { | ||
return StreamSupport.stream( | ||
Spliterators.spliteratorUnknownSize( this, Spliterator.ORDERED ), | ||
false ); | ||
} | ||
|
||
private boolean allSet() { | ||
for( int i = 0; i < included.length; i++ ) { | ||
if( !included[i] ) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
message/message-http/src/test/java/com/mastercard/test/flow/msg/http/CombinatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.mastercard.test.flow.msg.http; | ||
|
||
import static java.util.stream.Collectors.joining; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.Arrays; | ||
import java.util.TreeSet; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Exercises {@link Combinator} | ||
*/ | ||
@SuppressWarnings("static-method") | ||
class CombinatorTest { | ||
|
||
/** | ||
* Demonstrates the combinations of zero items | ||
*/ | ||
@Test | ||
void empty() { | ||
test( "[]" ); | ||
} | ||
|
||
/** | ||
* Demonstrates the combinations of zero items | ||
*/ | ||
@Test | ||
void single() { | ||
test( "[]\n" | ||
+ "[a]", | ||
"a" ); | ||
} | ||
|
||
/** | ||
* Demonstrates the combinations of zero items | ||
*/ | ||
@Test | ||
void pair() { | ||
test( "[]\n" | ||
+ "[a]\n" | ||
+ "[b]\n" | ||
+ "[a, b]", | ||
"a", "b" ); | ||
} | ||
|
||
/** | ||
* Demonstrates the combinations of three items | ||
*/ | ||
@Test | ||
void triple() { | ||
test( "" | ||
+ "[]\n" | ||
+ "[a]\n" | ||
+ "[b]\n" | ||
+ "[a, b]\n" | ||
+ "[c]\n" | ||
+ "[a, c]\n" | ||
+ "[b, c]\n" | ||
+ "[a, b, c]", | ||
"a", "b", "c" ); | ||
} | ||
|
||
private static void test( String expected, String... items ) { | ||
assertEquals( | ||
expected, | ||
new Combinator<>( TreeSet::new, items ) | ||
.stream() | ||
.map( String::valueOf ) | ||
.collect( joining( "\n" ) ), | ||
"Combinations of " + Arrays.toString( items ) ); | ||
} | ||
} |
Oops, something went wrong.