Skip to content

Commit d088f2d

Browse files
author
eugenp
committed
work on search
1 parent 5d7ee60 commit d088f2d

15 files changed

Lines changed: 366 additions & 166 deletions

‎eclipse/INTEGRATION-SINGLE.launch‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
22
<launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
33
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
4-
<listEntry value="/rest/src/test/java/org/rest/sec/util/SearchUtilUnitTest.java"/>
4+
<listEntry value="/rest/src/test/java/org/rest/sec/web/role/RoleSearchRESTIntegrationTest.java"/>
55
</listAttribute>
66
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
77
<listEntry value="1"/>
@@ -12,10 +12,10 @@
1212
</mapAttribute>
1313
<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
1414
<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
15-
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value="test"/>
15+
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
1616
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/>
1717
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/>
18-
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.rest.sec.util.SearchUtilUnitTest"/>
18+
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.rest.sec.web.role.RoleSearchRESTIntegrationTest"/>
1919
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="rest"/>
2020
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/>
2121
</launchConfiguration>

‎src/main/java/org/rest/sec/util/SearchUtil.java‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,43 @@ private SearchUtil(){
1515

1616
//
1717

18-
public static List< ImmutablePair< String, String >> parseQueryString( final String queryString ){
18+
public static List< ImmutablePair< String, ? >> parseQueryString( final String queryString ){
1919
Preconditions.checkNotNull( queryString );
20-
Preconditions.checkState( queryString.matches( "(id:[0-9]+),?(name:[0-9a-z]+)?" ) );
20+
Preconditions.checkState( queryString.matches( "(id:[0-9]+)?,?(name:[0-9a-zA-Z]+)?" ) );
2121

22-
final List< ImmutablePair< String, String >> tuplesList = Lists.newArrayList();
22+
final List< ImmutablePair< String, ? >> tuplesList = Lists.newArrayList();
2323
final String[] tuples = queryString.split( "," );
2424
for( final String tuple : tuples ){
2525
final String[] keyAndValue = tuple.split( ":" );
2626
Preconditions.checkState( keyAndValue.length == 2 );
27-
tuplesList.add( new ImmutablePair< String, String >( keyAndValue[0], keyAndValue[1] ) );
27+
tuplesList.add( constructTuple( keyAndValue[0], keyAndValue[1] ) );
2828
}
2929

3030
return tuplesList;
3131
}
3232

33+
private static ImmutablePair< String, ? > constructTuple( final String key, final String value ){
34+
if( key.equals( "id" ) ){
35+
return new ImmutablePair< String, Long >( key, Long.parseLong( value ) );
36+
}
37+
return new ImmutablePair< String, String >( key, value );
38+
}
39+
40+
//
41+
42+
public static String constructQueryString( final Long id, final String name ){
43+
final StringBuffer queryString = new StringBuffer();
44+
if( id != null ){
45+
queryString.append( "id:" + id );
46+
}
47+
if( name != null ){
48+
if( queryString.length() != 0 ){
49+
queryString.append( ',' );
50+
}
51+
queryString.append( "name:" + name );
52+
}
53+
54+
return queryString.toString();
55+
}
56+
3357
}

‎src/main/java/org/rest/sec/web/controller/RoleController.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public RoleController(){
4444
@RequestMapping( params = { "q" },method = RequestMethod.GET )
4545
@ResponseBody
4646
public List< Role > search( @RequestParam( "q" ) final String queryString ){
47-
final List< ImmutablePair< String, String >> parsedQuery = SearchUtil.parseQueryString( queryString );
47+
final List< ImmutablePair< String, ? >> parsedQuery = SearchUtil.parseQueryString( queryString );
4848

4949
final List< Role > results = getService().search( parsedQuery.get( 0 ) );
5050

‎src/test/java/org/rest/client/template/AbstractRESTTemplate.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public AbstractRESTTemplate( final Class< T > clazzToSet ){
3030
// search
3131

3232
@Override
33-
public Response searchAsResponse( final Long id ){
33+
public Response searchAsResponse( final Long id, final String name ){
3434
throw new UnsupportedOperationException();
3535
}
3636
@Override
37-
public List< T > search( final Long id ){
37+
public List< T > search( final Long id, final String name ){
3838
throw new UnsupportedOperationException();
3939
}
4040

‎src/test/java/org/rest/client/template/IRESTTemplate.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ public interface IRESTTemplate< T extends IEntity > extends IRestDao< T >, IEnti
1919

2020
// search
2121

22-
List< T > search( final Long id );
22+
List< T > search( final Long id, final String name );
2323

2424
}

‎src/test/java/org/rest/client/template/ITemplateAsResponse.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public interface ITemplateAsResponse< T extends IEntity >{
1717

1818
// search
1919

20-
Response searchAsResponse( Long id );
20+
Response searchAsResponse( final Long id, final String name );
2121

2222
// create
2323

‎src/test/java/org/rest/client/template/impl/RoleRESTTemplateImpl.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.rest.sec.client.ExamplePaths;
1010
import org.rest.sec.model.Privilege;
1111
import org.rest.sec.model.Role;
12+
import org.rest.sec.util.SearchUtil;
1213
import org.rest.testing.security.AuthenticationUtil;
1314
import org.springframework.beans.factory.annotation.Autowired;
1415
import org.springframework.stereotype.Component;
@@ -35,18 +36,18 @@ public final Role findByName( final String name ){
3536
}
3637

3738
@Override
38-
public final Response searchAsResponse( final Long id ){
39-
final String queryURI = getURI() + "?q=id:" + id;
39+
public final Response searchAsResponse( final Long id, final String name ){
40+
final String queryURI = getURI() + "?q=" + SearchUtil.constructQueryString( id, name );
4041
return givenAuthenticated().header( HttpHeaders.ACCEPT, marshaller.getMime() ).get( queryURI );
4142
}
4243

4344
@SuppressWarnings( { "unchecked", "rawtypes" } )
4445
@Override
45-
public final List< Role > search( final Long id ){
46+
public final List< Role > search( final Long id, final String name ){
4647
final String queryURI = getURI() + "?q=id:" + id;
4748
final Response searchResponse = givenAuthenticated().header( HttpHeaders.ACCEPT, marshaller.getMime() ).get( queryURI );
4849
Preconditions.checkState( searchResponse.getStatusCode() == 200 );
49-
50+
5051
return getMarshaller().<List> decode( searchResponse.getBody().asString(), List.class );
5152
}
5253

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.rest.sec.util;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public final class ConstructQueryStringUnitTest{
8+
9+
//
10+
11+
@Test
12+
public final void whenQueryStringIsConstructedFromNull_thenNoException(){
13+
SearchUtil.constructQueryString( null, null );
14+
}
15+
16+
@Test
17+
public final void whenQueryStringIsConstructedFromId_thenNoException(){
18+
SearchUtil.constructQueryString( 2l, null );
19+
}
20+
@Test
21+
public final void whenQueryStringIsConstructedFromName_thenNoException(){
22+
SearchUtil.constructQueryString( null, "someName" );
23+
}
24+
25+
@Test
26+
public final void whenQueryStringIsConstructedFromId_thenQueryStringIsCorrect(){
27+
final String queryString = SearchUtil.constructQueryString( 2l, null );
28+
assertEquals( "id:2", queryString );
29+
}
30+
@Test
31+
public final void whenQueryStringIsConstructedFromName_thenQueryStringIsCorrect(){
32+
final String queryString = SearchUtil.constructQueryString( null, "someName" );
33+
assertEquals( "name:someName", queryString );
34+
}
35+
36+
@Test
37+
public final void whenQueryStringIsConstructedFromIdAndName_thenQueryStringIsCorrect(){
38+
final String queryString = SearchUtil.constructQueryString( 32l, "someName" );
39+
assertEquals( "id:32,name:someName", queryString );
40+
}
41+
42+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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

Comments
 (0)