1616import org .rest .spring .context .ContextTestConfig ;
1717import org .rest .spring .persistence .jpa .PersistenceJPAConfig ;
1818import org .rest .spring .testing .TestingTestConfig ;
19+ import org .rest .util .IdUtil ;
1920import org .springframework .beans .factory .annotation .Autowired ;
2021import org .springframework .test .context .ContextConfiguration ;
2122import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
2930@ SuppressWarnings ( "unchecked" )
3031@ TransactionConfiguration ( defaultRollback = true )
3132public class RoleServiceSearchPersistenceIntegrationTest {
33+ private static final String ID = "id" ;
34+ private static final String NAME = "name" ;
3235
3336 @ Autowired private IPrivilegeService privilegeService ;
3437 @ Autowired private IRoleService roleService ;
@@ -50,35 +53,101 @@ public final void whenSearchByNameIsPerformed_thenNoExceptions(){
5053 final Role existingEntity = getService ().create ( createNewEntity () );
5154
5255 // When
53- final ImmutablePair < String , String > nameConstraint = new ImmutablePair < String , String >( "name" , existingEntity .getName () );
56+ final ImmutablePair < String , String > nameConstraint = new ImmutablePair < String , String >( NAME , existingEntity .getName () );
5457 getService ().search ( nameConstraint );
5558 }
5659
60+ // search - by id
61+
62+ @ Test
63+ public final void givenEntityWithIdExists_whenSearchByIdIsPerformed_thenResultIsFound (){
64+ final Role existingEntity = getService ().create ( createNewEntity () );
65+
66+ // When
67+ final ImmutablePair < String , Long > idConstraint = new ImmutablePair < String , Long >( ID , existingEntity .getId () );
68+ final List < Role > searchResults = getService ().search ( idConstraint );
69+
70+ // Then
71+ assertThat ( searchResults , hasItem ( existingEntity ) );
72+ }
73+ @ Test
74+ public final void givenEntityWithIdDoesNotExist_whenSearchByIdIsPerformed_thenResultIsNotFound (){
75+ final Role existingEntity = getService ().create ( createNewEntity () );
76+
77+ // When
78+ final ImmutablePair < String , Long > idConstraint = new ImmutablePair < String , Long >( ID , IdUtil .randomPositiveLong () );
79+ final List < Role > searchResults = getService ().search ( idConstraint );
80+
81+ // Then
82+ assertThat ( searchResults , not ( hasItem ( existingEntity ) ) );
83+ }
84+
85+ // search - by name
86+
5787 @ Test
58- public final void givenEntityExists_whenSearchByNameIsPerformed_thenResultIsFound (){
88+ public final void givenEntityWithNameExists_whenSearchByNameIsPerformed_thenResultIsFound (){
5989 final Role existingEntity = getService ().create ( createNewEntity () );
6090
6191 // When
62- final ImmutablePair < String , String > nameConstraint = new ImmutablePair < String , String >( "name" , existingEntity .getName () );
92+ final ImmutablePair < String , String > nameConstraint = new ImmutablePair < String , String >( NAME , existingEntity .getName () );
6393 final List < Role > searchResults = getService ().search ( nameConstraint );
6494
6595 // Then
6696 assertThat ( searchResults , hasItem ( existingEntity ) );
6797 }
6898 @ Test
69- public final void givenEntityExists_whenSearchByIdNegatedNameIsPerformed_thenResultsAreFound (){
99+ public final void givenEntityWithNameDoesNotExist_whenSearchByNameIsPerformed_thenResultIsNotFound (){
100+ final Role existingEntity = getService ().create ( createNewEntity () );
101+
102+ // When
103+ final ImmutablePair < String , String > nameConstraint = new ImmutablePair < String , String >( NAME , randomAlphabetic ( 8 ) );
104+ final List < Role > searchResults = getService ().search ( nameConstraint );
105+
106+ // Then
107+ assertThat ( searchResults , not ( hasItem ( existingEntity ) ) );
108+ }
109+
110+ @ Test
111+ public final void givenEntitiesExists_whenSearchByNegatedNameIsPerformed_thenResultsAreCorrect (){
70112 final Role existingEntity1 = getService ().create ( createNewEntity () );
71113 final Role existingEntity2 = getService ().create ( createNewEntity () );
72114
73115 // When
74- final ImmutablePair < String , String > nameConstraint = new ImmutablePair < String , String >( "name" , existingEntity1 .getName () );
116+ final ImmutablePair < String , String > nameConstraint = new ImmutablePair < String , String >( NAME , existingEntity1 .getName () );
75117 final List < Role > searchResults = getService ().search ( nameConstraint );
76118
77119 // Then
78120 assertThat ( searchResults , hasItem ( existingEntity1 ) );
79121 assertThat ( searchResults , not ( hasItem ( existingEntity2 ) ) );
80122 }
81123
124+ // search - by id and name
125+
126+ @ Test
127+ public final void givenEntityExists_whenSearchIsPerformedByIdAndName_thenResultIsFound (){
128+ final Role existingEntity = getService ().create ( createNewEntity () );
129+
130+ // When
131+ final ImmutablePair < String , String > nameConstraint = new ImmutablePair < String , String >( NAME , existingEntity .getName () );
132+ final ImmutablePair < String , Long > idConstraint = new ImmutablePair < String , Long >( ID , existingEntity .getId () );
133+ final List < Role > searchResults = getService ().search ( nameConstraint , idConstraint );
134+
135+ // Then
136+ assertThat ( searchResults , hasItem ( existingEntity ) );
137+ }
138+ @ Test
139+ public final void givenEntityExists_whenSearchIsPerformedByIncorrectIdAndCorrectName_thenResultIsNotFound (){
140+ final Role existingEntity = getService ().create ( createNewEntity () );
141+
142+ // When
143+ final ImmutablePair < String , String > nameConstraint = new ImmutablePair < String , String >( NAME , existingEntity .getName () );
144+ final ImmutablePair < String , Long > idConstraint = new ImmutablePair < String , Long >( ID , IdUtil .randomPositiveLong () );
145+ final List < Role > searchResults = getService ().search ( nameConstraint , idConstraint );
146+
147+ // Then
148+ assertThat ( searchResults , not ( hasItem ( existingEntity ) ) );
149+ }
150+
82151 // template method
83152
84153 protected final IRoleService getService (){
0 commit comments