Skip to content

Commit 8ecbc8d

Browse files
author
eugenp
committed
work on search
1 parent d088f2d commit 8ecbc8d

3 files changed

Lines changed: 60 additions & 8 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@ public RoleController(){
4646
public List< Role > search( @RequestParam( "q" ) final String queryString ){
4747
final List< ImmutablePair< String, ? >> parsedQuery = SearchUtil.parseQueryString( queryString );
4848

49-
final List< Role > results = getService().search( parsedQuery.get( 0 ) );
49+
final List< Role > results = getService().search( parsedQuery.toArray( new ImmutablePair[parsedQuery.size()] ) );
5050

5151
return results;
5252
}
53-
5453
// find - all/paginated
5554

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public final Response searchAsResponse( final Long id, final String name ){
4444
@SuppressWarnings( { "unchecked", "rawtypes" } )
4545
@Override
4646
public final List< Role > search( final Long id, final String name ){
47-
final String queryURI = getURI() + "?q=id:" + id;
47+
final String queryURI = getURI() + "?q=" + SearchUtil.constructQueryString( id, name );
4848
final Response searchResponse = givenAuthenticated().header( HttpHeaders.ACCEPT, marshaller.getMime() ).get( queryURI );
4949
Preconditions.checkState( searchResponse.getStatusCode() == 200 );
5050

‎src/test/java/org/rest/sec/web/role/RoleSearchRESTIntegrationTest.java‎

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
44
import static org.hamcrest.CoreMatchers.is;
55
import static org.hamcrest.Matchers.hasItem;
6+
import static org.hamcrest.Matchers.not;
67
import static org.junit.Assert.assertThat;
78

89
import java.util.List;
@@ -15,6 +16,7 @@
1516
import org.rest.spring.client.ClientTestConfig;
1617
import org.rest.spring.context.ContextTestConfig;
1718
import org.rest.spring.testing.TestingTestConfig;
19+
import org.rest.util.IdUtil;
1820
import org.springframework.beans.factory.annotation.Autowired;
1921
import org.springframework.test.context.ContextConfiguration;
2022
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -88,23 +90,74 @@ public final void givenResourceExists_whenResourceIfSearchedByName_then200IsRece
8890
assertThat( searchResponse.getStatusCode(), is( 200 ) );
8991
}
9092

91-
/*
92-
9393
@Test
9494
public final void givenResourceExists_whenResourceIfSearchedByNameAndUnmarshalled_thenNoException(){
9595
final Role existingResource = getTemplate().create( getTemplate().createNewEntity() );
96-
getTemplate().search( existingResource.getId() );
96+
getTemplate().search( null, existingResource.getName() );
9797
}
9898
@Test
9999
public final void givenResourceExists_whenResourceIfSearchedByNameAndUnmarshalled_thenResourceIsFound(){
100100
final Role existingResource = getTemplate().create( getTemplate().createNewEntity() );
101101

102102
// When
103-
final List< Role > found = getTemplate().search( existingResource.getId() );
103+
final List< Role > found = getTemplate().search( null, existingResource.getName() );
104104

105105
// Then
106106
assertThat( found, hasItem( existingResource ) );
107-
}*/
107+
}
108+
109+
// search - by id and name
110+
111+
@Test
112+
public final void givenResourceExists_whenResourceIfSearchedByIdAndName_then200IsReceived(){
113+
final Role existingResource = getTemplate().create( getTemplate().createNewEntity() );
114+
115+
// When
116+
final Response searchResponse = getTemplate().searchAsResponse( existingResource.getId(), existingResource.getName() );
117+
118+
// Then
119+
assertThat( searchResponse.getStatusCode(), is( 200 ) );
120+
}
121+
@Test
122+
public final void givenResourceExists_whenResourceIfSearchedByIdAndNameAndUnmarshalled_thenResourceIsFound(){
123+
final Role existingResource = getTemplate().create( getTemplate().createNewEntity() );
124+
125+
// When
126+
final List< Role > found = getTemplate().search( existingResource.getId(), existingResource.getName() );
127+
128+
// Then
129+
assertThat( found, hasItem( existingResource ) );
130+
}
131+
@Test
132+
public final void givenResourceExists_whenResourceIfSearchedByIdAndWrongNameAndUnmarshalled_thenResourceIsFound(){
133+
final Role existingResource = getTemplate().create( getTemplate().createNewEntity() );
134+
135+
// When
136+
final List< Role > found = getTemplate().search( existingResource.getId(), randomAlphabetic( 8 ) );
137+
138+
// Then
139+
assertThat( found, not( hasItem( existingResource ) ) );
140+
}
141+
@Test
142+
public final void givenResourceExists_whenResourceIfSearchedByWrongIdAndCorrectNameAndUnmarshalled_thenResourceIsFound(){
143+
final Role existingResource = getTemplate().create( getTemplate().createNewEntity() );
144+
145+
// When
146+
final List< Role > found = getTemplate().search( IdUtil.randomPositiveLong(), existingResource.getName() );
147+
148+
// Then
149+
assertThat( found, not( hasItem( existingResource ) ) );
150+
}
151+
@Test
152+
public final void givenResourceExists_whenResourceIfSearchedByWrongIdAndWrongNameAndUnmarshalled_thenResourceIsFound(){
153+
final Role existingResource = getTemplate().create( getTemplate().createNewEntity() );
154+
155+
// When
156+
final List< Role > found = getTemplate().search( IdUtil.randomPositiveLong(), randomAlphabetic( 8 ) );
157+
158+
// Then
159+
assertThat( found, not( hasItem( existingResource ) ) );
160+
}
108161

109162
// template
110163

0 commit comments

Comments
 (0)