|
| 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 ParseQueryStringUnitTest{ |
| 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 givenQueryContainIdConstraintFieldButNoValue_whenQueryIsParsed_thenException(){ |
| 29 | + SearchUtil.parseQueryString( "id:" ); |
| 30 | + } |
| 31 | + @Test( expected = IllegalStateException.class ) |
| 32 | + public final void givenQueryContainIdConstraintNonNumericalValueForId_whenQueryIsParsed_thenException(){ |
| 33 | + SearchUtil.parseQueryString( "id:aa" ); |
| 34 | + } |
| 35 | + @Test( expected = IllegalStateException.class ) |
| 36 | + public final void givenQueryContainSingleIdConstraintWithValidFieldValueAndAdditionalInvalidCharacter_whenQueryIsParsed_thenException(){ |
| 37 | + SearchUtil.parseQueryString( "id:aa," ); |
| 38 | + } |
| 39 | + @Test( expected = IllegalStateException.class ) |
| 40 | + public final void givenQueryContainSingleIdConstraintWithCorrectFieldValueButInvalidSeparator_whenQueryIsParsed_thenException(){ |
| 41 | + SearchUtil.parseQueryString( "id|aa," ); |
| 42 | + } |
| 43 | + @Test( expected = IllegalStateException.class ) |
| 44 | + public final void givenQueryContainIdConstraintWithUnknownKey_whenQueryIsParsed_thenException(){ |
| 45 | + SearchUtil.parseQueryString( "somekey|aa" ); |
| 46 | + } |
| 47 | + |
| 48 | + // -- correct |
| 49 | + |
| 50 | + @Test |
| 51 | + public final void givenQueryContainsValidIdConstraint_whenQueryIsParsed_thenNoExceptions(){ |
| 52 | + SearchUtil.parseQueryString( "id:2" ); |
| 53 | + } |
| 54 | + @Test |
| 55 | + public final void givenValidQueryWithIdConstraint_whenQueryIsParsed_thenResultIsNotNull(){ |
| 56 | + final List< ImmutablePair< String, ? >> queryTyples = SearchUtil.parseQueryString( "id:2" ); |
| 57 | + |
| 58 | + assertFalse( queryTyples.isEmpty() ); |
| 59 | + } |
| 60 | + @Test |
| 61 | + public final void givenValidQueryWithIdConstraint_whenQueryIsParsed_thenResultKeyIsCorrect(){ |
| 62 | + final List< ImmutablePair< String, ? >> queryTyples = SearchUtil.parseQueryString( "id:2" ); |
| 63 | + final ImmutablePair< String, ? > pair = queryTyples.get( 0 ); |
| 64 | + assertEquals( "id", pair.getLeft() ); |
| 65 | + } |
| 66 | + @Test |
| 67 | + public final void givenValidQueryWithIdConstraint_whenQueryIsParsed_thenResultValueIsCorrect(){ |
| 68 | + final List< ImmutablePair< String, ? >> queryTyples = SearchUtil.parseQueryString( "id:2" ); |
| 69 | + final ImmutablePair< String, ? > pair = queryTyples.get( 0 ); |
| 70 | + assertEquals( 2l, pair.getRight() ); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public final void givenValidQueryWithNameConstraint_whenQueryIsParsed_thenNoExceptions(){ |
| 75 | + SearchUtil.parseQueryString( "name:some" ); |
| 76 | + } |
| 77 | + @Test |
| 78 | + public final void givenValidQueryWithNameConstraint_whenQueryIsParsed_thenResultIsNotNull(){ |
| 79 | + final List< ImmutablePair< String, ? >> queryTyples = SearchUtil.parseQueryString( "name:some" ); |
| 80 | + |
| 81 | + assertFalse( queryTyples.isEmpty() ); |
| 82 | + } |
| 83 | + @Test |
| 84 | + public final void givenValidQueryWithNameConstraint_whenQueryIsParsed_thenResultKeyIsCorrect(){ |
| 85 | + final List< ImmutablePair< String, ? >> queryTyples = SearchUtil.parseQueryString( "name:some" ); |
| 86 | + final ImmutablePair< String, ? > pair = queryTyples.get( 0 ); |
| 87 | + assertEquals( "name", pair.getLeft() ); |
| 88 | + } |
| 89 | + @Test |
| 90 | + public final void givenValidQueryWithNameConstraint_whenQueryIsParsed_thenResultValueIsCorrect(){ |
| 91 | + final List< ImmutablePair< String, ? >> queryTyples = SearchUtil.parseQueryString( "name:some" ); |
| 92 | + final ImmutablePair< String, ? > pair = queryTyples.get( 0 ); |
| 93 | + assertEquals( "some", pair.getRight() ); |
| 94 | + } |
| 95 | + @Test |
| 96 | + public final void givenValidQueryWithNameConstraint_whenQueryIsParsedForNameValueWithUppercase_thenResultValueIsCorrect(){ |
| 97 | + final List< ImmutablePair< String, ? >> queryTyples = SearchUtil.parseQueryString( "name:Some" ); |
| 98 | + final ImmutablePair< String, ? > pair = queryTyples.get( 0 ); |
| 99 | + assertEquals( "Some", pair.getRight() ); |
| 100 | + } |
| 101 | + |
| 102 | + // multiple key-value tuples |
| 103 | + |
| 104 | + @Test( expected = IllegalStateException.class ) |
| 105 | + public final void givenQueryContainOneValidConstraintAndOneIncorrectOne_whenQueryIsParsed_thenException(){ |
| 106 | + SearchUtil.parseQueryString( "id:2,name" ); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public final void givenQueryContainANameConstraintWithNumbersInTheName_whenQueryIsParsed_thenNoExceptions(){ |
| 111 | + SearchUtil.parseQueryString( "id:2,name:eugen2" ); |
| 112 | + } |
| 113 | + @Test |
| 114 | + public final void givenQueryContainTwoValidConstraints_whenQueryIsParsed_thenNoExceptions(){ |
| 115 | + SearchUtil.parseQueryString( "id:2,name:eugen" ); |
| 116 | + } |
| 117 | + @Test |
| 118 | + public final void givenQueryContainTwoValidConstraints_whenQueryIsParsed_thenConstraintsAreCorrect(){ |
| 119 | + final List< ImmutablePair< String, ? >> parsedQueryString = SearchUtil.parseQueryString( "id:2,name:eugen" ); |
| 120 | + assertThat( parsedQueryString, hasItem( new ImmutablePair< String, Long >( "id", 2l ) ) ); |
| 121 | + assertThat( parsedQueryString, hasItem( new ImmutablePair< String, String >( "name", "eugen" ) ) ); |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments