Skip to content

Commit ba201de

Browse files
author
eugenp
committed
work on search - negation, additional integration tests
1 parent 8ecbc8d commit ba201de

7 files changed

Lines changed: 114 additions & 20 deletions

File tree

‎eclipse/INTEGRATION-SINGLE.launch‎

Lines changed: 2 additions & 2 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/web/role/RoleSearchRESTIntegrationTest.java"/>
4+
<listEntry value="/rest/src/test/java/org/rest/sec/util/ConstructQueryStringUnitTest.java"/>
55
</listAttribute>
66
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
77
<listEntry value="1"/>
@@ -15,7 +15,7 @@
1515
<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.web.role.RoleSearchRESTIntegrationTest"/>
18+
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.rest.sec.util.ConstructQueryStringUnitTest"/>
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/persistence/service/IRoleService.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ public interface IRoleService extends IService< Role >{
1010

1111
Role findByName( final String name );
1212

13-
List< Role > search( final Long id );
14-
1513
List< Role > search( final ImmutablePair< String, ? >... constraints );
1614

1715
}

‎src/main/java/org/rest/sec/persistence/service/impl/RoleServiceImpl.java‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public class RoleServiceImpl extends AbstractService< Role > implements IRoleSer
2727

2828
@Autowired IRoleJpaDAO dao;
2929

30-
// @Autowired private IPrivilegeJpaDAO associationDao;
31-
3230
public RoleServiceImpl(){
3331
super( Role.class );
3432
}
@@ -51,35 +49,42 @@ public List< Role > search( final ImmutablePair< String, ? >... constraints ){
5149
return getDao().findAll( specifications );
5250
}
5351
private Specification< Role > resolveConstraint( final ImmutablePair< String, ? > constraint ){
54-
final String constraintName = constraint.getLeft();
52+
String constraintName = constraint.getLeft();
53+
boolean negated = false;
54+
if( constraintName.startsWith( "~" ) ){
55+
negated = true;
56+
constraintName = constraintName.substring( 1 );
57+
}
58+
5559
if( constraintName.equals( "name" ) ){
56-
return byName( (String) constraint.getRight() );
60+
return byName( (String) constraint.getRight(), negated );
5761
}
5862
if( constraintName.equals( "id" ) ){
59-
return byId( (Long) constraint.getRight() );
63+
return byId( (Long) constraint.getRight(), negated );
6064
}
6165
return null;
6266
}
63-
67+
6468
// search
6569

66-
@Override
67-
public List< Role > search( final Long id ){
68-
return getDao().findAll( byId( id ) );
69-
}
70-
71-
public static Specification< Role > byId( final Long id ){
70+
public static Specification< Role > byId( final Long id, final boolean negated ){
7271
return new Specification< Role >(){
7372
@Override
7473
public final Predicate toPredicate( final Root< Role > root, final CriteriaQuery< ? > query, final CriteriaBuilder cb ){
74+
if( negated ){
75+
return cb.notEqual( root.get( Role_.id ), id );
76+
}
7577
return cb.equal( root.get( Role_.id ), id );
7678
}
7779
};
7880
}
79-
public static Specification< Role > byName( final String name ){
81+
public static Specification< Role > byName( final String name, final boolean negated ){
8082
return new Specification< Role >(){
8183
@Override
8284
public final Predicate toPredicate( final Root< Role > root, final CriteriaQuery< ? > query, final CriteriaBuilder cb ){
85+
if( negated ){
86+
return cb.notEqual( root.get( Role_.name ), name );
87+
}
8388
return cb.equal( root.get( Role_.name ), name );
8489
}
8590
};

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,22 @@ private SearchUtil(){
4040
//
4141

4242
public static String constructQueryString( final Long id, final String name ){
43+
return constructQueryString( id, false, name, false );
44+
}
45+
46+
public static String constructQueryString( final Long id, final boolean negatedId, final String name, final boolean negatedName ){
4347
final StringBuffer queryString = new StringBuffer();
48+
String key = null;
4449
if( id != null ){
45-
queryString.append( "id:" + id );
50+
key = ( negatedId ) ? "~id" : "id";
51+
queryString.append( key + ":" + id );
4652
}
4753
if( name != null ){
4854
if( queryString.length() != 0 ){
4955
queryString.append( ',' );
5056
}
51-
queryString.append( "name:" + name );
57+
key = ( negatedName ) ? "~name" : "name";
58+
queryString.append( key + ":" + name );
5259
}
5360

5461
return queryString.toString();

‎src/test/java/org/rest/sec/persistence/service/RoleServiceSearchPersistenceIntegrationTest.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,35 @@ public final void givenEntityExists_whenSearchIsPerformedByCorrectIdAndIncorrect
160160
assertThat( searchResults, not( hasItem( existingEntity ) ) );
161161
}
162162

163+
// search - by negated id,name
164+
165+
@Test
166+
public final void whenSearchByNegatedNameIsPerformed_thenResultsAreFound(){
167+
final Role existingEntity1 = getService().create( createNewEntity() );
168+
final Role existingEntity2 = getService().create( createNewEntity() );
169+
170+
// When
171+
final ImmutablePair< String, String > nameConstraint = new ImmutablePair< String, String >( "~" + NAME, existingEntity1.getName() );
172+
final List< Role > searchResults = getService().search( nameConstraint );
173+
174+
// Then
175+
assertThat( searchResults, not( hasItem( existingEntity1 ) ) );
176+
assertThat( searchResults, hasItem( existingEntity2 ) );
177+
}
178+
@Test
179+
public final void whenSearchByNegatedIdIsPerformed_thenResultsAreFound(){
180+
final Role existingEntity1 = getService().create( createNewEntity() );
181+
final Role existingEntity2 = getService().create( createNewEntity() );
182+
183+
// When
184+
final ImmutablePair< String, Long > idConstraint = new ImmutablePair< String, Long >( "~" + ID, existingEntity1.getId() );
185+
final List< Role > searchResults = getService().search( idConstraint );
186+
187+
// Then
188+
assertThat( searchResults, not( hasItem( existingEntity1 ) ) );
189+
assertThat( searchResults, hasItem( existingEntity2 ) );
190+
}
191+
163192
// template method
164193

165194
protected final IRoleService getService(){

‎src/test/java/org/rest/sec/util/ConstructQueryStringUnitTest.java‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public final class ConstructQueryStringUnitTest{
88

9-
//
9+
// not considering negation
1010

1111
@Test
1212
public final void whenQueryStringIsConstructedFromNull_thenNoException(){
@@ -38,5 +38,47 @@ public final void whenQueryStringIsConstructedFromIdAndName_thenQueryStringIsCor
3838
final String queryString = SearchUtil.constructQueryString( 32l, "someName" );
3939
assertEquals( "id:32,name:someName", queryString );
4040
}
41+
42+
// considering negation
43+
44+
@Test
45+
public final void whenQueryStringIsConstructedFromNegatedId_thenNoException(){
46+
SearchUtil.constructQueryString( 2l, true, null, false );
47+
}
48+
@Test
49+
public final void whenQueryStringIsConstructedFromNegatedId_thenQueryStringIsCorrect(){
50+
final String queryString = SearchUtil.constructQueryString( 2l, true, null, false );
51+
assertEquals( "~id:2", queryString );
52+
}
53+
54+
@Test
55+
public final void whenQueryStringIsConstructedFromNegatedName_thenNoException(){
56+
SearchUtil.constructQueryString( null, false, "some", true );
57+
}
58+
@Test
59+
public final void whenQueryStringIsConstructedFromNegatedName_thenQueryStringIsCorrect(){
60+
final String queryString = SearchUtil.constructQueryString( null, false, "some", true );
61+
assertEquals( "~name:some", queryString );
62+
}
63+
64+
@Test
65+
public final void whenQueryStringIsConstructedFromIdAndNegatedName_thenNoException(){
66+
SearchUtil.constructQueryString( 2l, false, "some", true );
67+
}
68+
@Test
69+
public final void whenQueryStringIsConstructedFromIdAndNegatedName_thenQueryStringIsCorrect(){
70+
final String queryString = SearchUtil.constructQueryString( 2l, false, "some", true );
71+
assertEquals( "id:2,~name:some", queryString );
72+
}
73+
@Test
74+
public final void whenQueryStringIsConstructedFromNameAndNegatedId_thenQueryStringIsCorrect(){
75+
final String queryString = SearchUtil.constructQueryString( 2l, true, "some", false );
76+
assertEquals( "~id:2,name:some", queryString );
77+
}
78+
@Test
79+
public final void whenQueryStringIsConstructedFromNegatedNameAndNegatedId_thenQueryStringIsCorrect(){
80+
final String queryString = SearchUtil.constructQueryString( 2l, true, "some", true );
81+
assertEquals( "~id:2,~name:some", queryString );
82+
}
4183

4284
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,19 @@ public final void givenResourceExists_whenResourceIfSearchedByWrongIdAndWrongNam
159159
assertThat( found, not( hasItem( existingResource ) ) );
160160
}
161161

162+
// search - by negated id,name
163+
164+
@Test
165+
public final void givenResourceExists_whenResourceIfSearchedByNegatedName_then200IsReceived(){
166+
final Role existingResource = getTemplate().create( getTemplate().createNewEntity() );
167+
168+
// When
169+
final Response searchResponse = getTemplate().searchAsResponse( existingResource.getId(), existingResource.getName() );
170+
171+
// Then
172+
assertThat( searchResponse.getStatusCode(), is( 200 ) );
173+
}
174+
162175
// template
163176

164177
protected final Role createNewEntity(){

0 commit comments

Comments
 (0)