|
| 1 | +package org.rest.sec.util; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.hasItem; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertFalse; |
| 6 | +import static org.junit.Assert.assertThat; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import org.apache.commons.lang3.tuple.ImmutablePair; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +public final class SearchUtilUnitTest{ |
| 14 | + |
| 15 | + // single key-value tuple |
| 16 | + |
| 17 | + // -- incorrect |
| 18 | + |
| 19 | + @Test( expected = NullPointerException.class ) |
| 20 | + public final void whenNullQueryIsParsed_thenException(){ |
| 21 | + SearchUtil.parseQueryString( null ); |
| 22 | + } |
| 23 | + @Test( expected = IllegalStateException.class ) |
| 24 | + public final void givenQueryDoesNotContainConstraintField_whenQueryIsParsed_thenException(){ |
| 25 | + SearchUtil.parseQueryString( "justtext" ); |
| 26 | + } |
| 27 | + @Test( expected = IllegalStateException.class ) |
| 28 | + public final void givenQueryContainConstraintFieldButNoValue_whenQueryIsParsed_thenException(){ |
| 29 | + SearchUtil.parseQueryString( "id:" ); |
| 30 | + } |
| 31 | + @Test( expected = IllegalStateException.class ) |
| 32 | + public final void givenQueryContainConstraintNonNumericalValueForId_whenQueryIsParsed_thenException(){ |
| 33 | + SearchUtil.parseQueryString( "id:aa" ); |
| 34 | + } |
| 35 | + @Test( expected = IllegalStateException.class ) |
| 36 | + public final void givenQueryContainSingleConstraintWithValidFieldValueAndAdditionalInvalidCharacter_whenQueryIsParsed_thenException(){ |
| 37 | + SearchUtil.parseQueryString( "id:aa," ); |
| 38 | + } |
| 39 | + @Test( expected = IllegalStateException.class ) |
| 40 | + public final void givenQueryContainSingleConstraintWithCorrectFieldValueButInvalidSeparator_whenQueryIsParsed_thenException(){ |
| 41 | + SearchUtil.parseQueryString( "id|aa," ); |
| 42 | + } |
| 43 | + @Test( expected = IllegalStateException.class ) |
| 44 | + public final void givenQueryContainConstraintWithUnknownKey_whenQueryIsParsed_thenException(){ |
| 45 | + SearchUtil.parseQueryString( "somekey|aa" ); |
| 46 | + } |
| 47 | + |
| 48 | + // -- correct |
| 49 | + |
| 50 | + @Test |
| 51 | + public final void givenQueryContainConstraintValidFieldValueForId_whenQueryIsParsed_thenNoExceptions(){ |
| 52 | + SearchUtil.parseQueryString( "id:2" ); |
| 53 | + } |
| 54 | + @Test |
| 55 | + public final void givenQueryStringValid_whenQueryIsParsed_thenResultIsNotNull(){ |
| 56 | + final List< ImmutablePair< String, String >> queryTyples = SearchUtil.parseQueryString( "id:2" ); |
| 57 | + |
| 58 | + assertFalse( queryTyples.isEmpty() ); |
| 59 | + } |
| 60 | + @Test |
| 61 | + public final void givenQueryStringValid_whenQueryIsParsed_thenResultKeyIsCorrect(){ |
| 62 | + final List< ImmutablePair< String, String >> queryTyples = SearchUtil.parseQueryString( "id:2" ); |
| 63 | + final ImmutablePair< String, String > pair = queryTyples.get( 0 ); |
| 64 | + assertEquals( "id", pair.getLeft() ); |
| 65 | + } |
| 66 | + @Test |
| 67 | + public final void givenQueryStringValid_whenQueryIsParsed_thenResultValueIsCorrect(){ |
| 68 | + final List< ImmutablePair< String, String >> queryTyples = SearchUtil.parseQueryString( "id:2" ); |
| 69 | + final ImmutablePair< String, String > pair = queryTyples.get( 0 ); |
| 70 | + assertEquals( "2", pair.getRight() ); |
| 71 | + } |
| 72 | + |
| 73 | + // multiple key-value tuples |
| 74 | + |
| 75 | + @Test( expected = IllegalStateException.class ) |
| 76 | + public final void givenQueryContainOneValidConstraintAndOneIncorrectOne_whenQueryIsParsed_thenException(){ |
| 77 | + SearchUtil.parseQueryString( "id:2,name" ); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public final void givenQueryContainANameConstraintWithNumbersInTheName_whenQueryIsParsed_thenNoExceptions(){ |
| 82 | + SearchUtil.parseQueryString( "id:2,name:eugen2" ); |
| 83 | + } |
| 84 | + @Test |
| 85 | + public final void givenQueryContainTwoValidConstraints_whenQueryIsParsed_thenNoExceptions(){ |
| 86 | + SearchUtil.parseQueryString( "id:2,name:eugen" ); |
| 87 | + } |
| 88 | + @Test |
| 89 | + public final void givenQueryContainTwoValidConstraints_whenQueryIsParsed_thenConstraintsAreCorrect(){ |
| 90 | + final List< ImmutablePair< String, String >> parsedQueryString = SearchUtil.parseQueryString( "id:2,name:eugen" ); |
| 91 | + assertThat( parsedQueryString, hasItem( new ImmutablePair< String, String >( "id", "2" ) ) ); |
| 92 | + assertThat( parsedQueryString, hasItem( new ImmutablePair< String, String >( "name", "eugen" ) ) ); |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments