Skip to content

Commit 5d7ee60

Browse files
author
eugenp
committed
work on search functionality - query parsing, etc
1 parent eb1a1ab commit 5d7ee60

12 files changed

Lines changed: 265 additions & 29 deletions

File tree

‎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/persistence/service/RoleServiceSearchPersistenceIntegrationTest.java"/>
4+
<listEntry value="/rest/src/test/java/org/rest/sec/util/SearchUtilUnitTest.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=""/>
15+
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value="test"/>
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.persistence.service.RoleServiceSearchPersistenceIntegrationTest"/>
18+
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.rest.sec.util.SearchUtilUnitTest"/>
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>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.rest.sec.util;
2+
3+
import java.util.List;
4+
5+
import org.apache.commons.lang3.tuple.ImmutablePair;
6+
7+
import com.google.common.base.Preconditions;
8+
import com.google.common.collect.Lists;
9+
10+
public final class SearchUtil{
11+
12+
private SearchUtil(){
13+
throw new UnsupportedOperationException();
14+
}
15+
16+
//
17+
18+
public static List< ImmutablePair< String, String >> parseQueryString( final String queryString ){
19+
Preconditions.checkNotNull( queryString );
20+
Preconditions.checkState( queryString.matches( "(id:[0-9]+),?(name:[0-9a-z]+)?" ) );
21+
22+
final List< ImmutablePair< String, String >> tuplesList = Lists.newArrayList();
23+
final String[] tuples = queryString.split( "," );
24+
for( final String tuple : tuples ){
25+
final String[] keyAndValue = tuple.split( ":" );
26+
Preconditions.checkState( keyAndValue.length == 2 );
27+
tuplesList.add( new ImmutablePair< String, String >( keyAndValue[0], keyAndValue[1] ) );
28+
}
29+
30+
return tuplesList;
31+
}
32+
33+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
import javax.servlet.http.HttpServletResponse;
66

7+
import org.apache.commons.lang3.tuple.ImmutablePair;
78
import org.rest.common.exceptions.ConflictException;
89
import org.rest.common.web.RestPreconditions;
910
import org.rest.sec.model.Role;
1011
import org.rest.sec.persistence.service.IRoleService;
12+
import org.rest.sec.util.SearchUtil;
1113
import org.rest.sec.util.SecurityConstants;
1214
import org.rest.web.common.AbstractController;
1315
import org.springframework.beans.factory.annotation.Autowired;
@@ -36,6 +38,19 @@ public RoleController(){
3638

3739
// API
3840

41+
// search
42+
43+
@SuppressWarnings( "unchecked" )
44+
@RequestMapping( params = { "q" },method = RequestMethod.GET )
45+
@ResponseBody
46+
public List< Role > search( @RequestParam( "q" ) final String queryString ){
47+
final List< ImmutablePair< String, String >> parsedQuery = SearchUtil.parseQueryString( queryString );
48+
49+
final List< Role > results = getService().search( parsedQuery.get( 0 ) );
50+
51+
return results;
52+
}
53+
3954
// find - all/paginated
4055

4156
@RequestMapping( params = { "page", "size" },method = RequestMethod.GET )

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ public AbstractRESTTemplate( final Class< T > clazzToSet ){
2727
clazz = clazzToSet;
2828
}
2929

30+
// search
31+
32+
@Override
33+
public Response searchAsResponse( final Long id ){
34+
throw new UnsupportedOperationException();
35+
}
36+
@Override
37+
public List< T > search( final Long id ){
38+
throw new UnsupportedOperationException();
39+
}
40+
3041
// findOne
3142

3243
@Override

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.rest.client.template;
22

3+
import java.util.List;
4+
35
import org.rest.common.IEntity;
46
import org.rest.common.IRestDao;
57
import org.rest.testing.marshaller.IMarshaller;
@@ -15,4 +17,8 @@ public interface IRESTTemplate< T extends IEntity > extends IRestDao< T >, IEnti
1517
RequestSpecification givenAuthenticated();
1618
IMarshaller getMarshaller();
1719

20+
// search
21+
22+
List< T > search( final Long id );
23+
1824
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public interface ITemplateAsResponse< T extends IEntity >{
1515

1616
Response findAllAsResponse(); // 1
1717

18+
// search
19+
20+
Response searchAsResponse( Long id );
21+
1822
// create
1923

2024
Response createAsResponse( final T resource ); // 14

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
44

5+
import java.util.List;
6+
7+
import org.apache.http.HttpHeaders;
58
import org.rest.client.template.AbstractRESTTemplate;
69
import org.rest.sec.client.ExamplePaths;
710
import org.rest.sec.model.Privilege;
@@ -10,7 +13,9 @@
1013
import org.springframework.beans.factory.annotation.Autowired;
1114
import org.springframework.stereotype.Component;
1215

16+
import com.google.common.base.Preconditions;
1317
import com.google.common.collect.Sets;
18+
import com.jayway.restassured.response.Response;
1419
import com.jayway.restassured.specification.RequestSpecification;
1520

1621
@Component
@@ -29,6 +34,22 @@ public final Role findByName( final String name ){
2934
return marshaller.decode( resourceAsXML, clazz );
3035
}
3136

37+
@Override
38+
public final Response searchAsResponse( final Long id ){
39+
final String queryURI = getURI() + "?q=id:" + id;
40+
return givenAuthenticated().header( HttpHeaders.ACCEPT, marshaller.getMime() ).get( queryURI );
41+
}
42+
43+
@SuppressWarnings( { "unchecked", "rawtypes" } )
44+
@Override
45+
public final List< Role > search( final Long id ){
46+
final String queryURI = getURI() + "?q=id:" + id;
47+
final Response searchResponse = givenAuthenticated().header( HttpHeaders.ACCEPT, marshaller.getMime() ).get( queryURI );
48+
Preconditions.checkState( searchResponse.getStatusCode() == 200 );
49+
50+
return getMarshaller().<List> decode( searchResponse.getBody().asString(), List.class );
51+
}
52+
3253
// template method
3354

3455
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public UserRESTTemplateImpl(){
2323
}
2424

2525
// API
26-
26+
2727
// template method
2828

2929
@Override
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)