Skip to content

Commit b474c3d

Browse files
author
eugenp
committed
added integration tests
1 parent 9361a3c commit b474c3d

1 file changed

Lines changed: 81 additions & 37 deletions

File tree

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

Lines changed: 81 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
44
import static org.hamcrest.CoreMatchers.is;
5-
import static org.hamcrest.Matchers.equalTo;
65
import static org.hamcrest.Matchers.hasItem;
76
import static org.hamcrest.Matchers.not;
87
import static org.junit.Assert.assertEquals;
98
import static org.junit.Assert.assertThat;
109

11-
import java.util.Set;
12-
1310
import org.hamcrest.Matchers;
1411
import org.junit.Test;
1512
import org.rest.sec.client.template.PrivilegeRESTTemplateImpl;
@@ -19,7 +16,6 @@
1916
import org.rest.sec.test.SecLogicRESTIntegrationTest;
2017
import org.springframework.beans.factory.annotation.Autowired;
2118

22-
import com.google.common.base.Preconditions;
2319
import com.google.common.collect.Sets;
2420
import com.jayway.restassured.response.Response;
2521
import com.jayway.restassured.specification.RequestSpecification;
@@ -35,6 +31,19 @@ public RoleLogicRESTIntegrationTest(){
3531

3632
// tests
3733

34+
// escaping characters
35+
36+
@Test
37+
public final void givenWorkingWithSpecialCharacters_whtnResourcesIfRetrievedByName_thenResourceIsCorrectlyRetrieved(){
38+
final Role newResource = getAPI().createNewEntity();
39+
newResource.setName( "Macy's,Dell, Inc." );
40+
getAPI().createAsResponse( newResource );
41+
42+
// When
43+
final Role retrievedResource = getAPI().findByName( newResource.getName() );
44+
assertEquals( newResource, retrievedResource );
45+
}
46+
3847
// find one
3948

4049
@Test
@@ -45,6 +54,8 @@ public final void givenResourceExists_whenResourceIsRetrievedByName_thenResource
4554
assertEquals( newResource, existingResourceByName );
4655
}
4756

57+
// find all
58+
4859
@Test
4960
public final void whenResourceIsRetrieved_thenAssociationsAreAlsoRetrieved(){
5061
final Role existingResource = getAPI().create( getAPI().createNewEntity() );
@@ -53,14 +64,49 @@ public final void whenResourceIsRetrieved_thenAssociationsAreAlsoRetrieved(){
5364

5465
// create
5566

67+
@Test
68+
public final void givenResourceHasNameWithSpace_whenResourceIsCreated_then201IsReceived(){
69+
final Role newResource = getAPI().createNewEntity();
70+
newResource.setName( randomAlphabetic( 4 ) + " " + randomAlphabetic( 4 ) );
71+
72+
// When
73+
final Response createAsResponse = getAPI().createAsResponse( newResource );
74+
75+
// Then
76+
assertThat( createAsResponse.getStatusCode(), is( 201 ) );
77+
}
78+
79+
@Test
80+
public final void givenExistingResourceHasNameWithSpace_whtnResourcesIfRetrievedByName_thenResourceIsCorrectlyRetrieved(){
81+
final Role newResource = getAPI().createNewEntity();
82+
newResource.setName( randomAlphabetic( 4 ) + " " + randomAlphabetic( 4 ) );
83+
getAPI().createAsResponse( newResource );
84+
85+
// When
86+
final Role retrievedResource = getAPI().findByName( newResource.getName() );
87+
assertEquals( newResource, retrievedResource );
88+
}
89+
90+
@Test
91+
public final void whenCreatingNewResourceWithExistingAssociations_thenNewResourceIsCorrectlyCreated(){
92+
final Privilege existingAssociation = getAssociationAPI().create( getAssociationAPI().createNewEntity() );
93+
final Role newResource = getAPI().createNewEntity();
94+
newResource.getPrivileges().add( existingAssociation );
95+
getAPI().create( newResource );
96+
97+
final Role newResource2 = getAPI().createNewEntity();
98+
newResource2.getPrivileges().add( existingAssociation );
99+
getAPI().create( newResource2 );
100+
}
101+
56102
/**
57103
* - note: this test ensures that a new User cannot automatically create new Privileges <br>
58104
* - note: the standard way to do this is: first create the Privilege resource(s), then associate them with the new User resource and then create the User resource
59105
*/
60106
@Test
61-
public final void whenRoleIsCreatedWithNewPrivilege_then409IsReceived(){
107+
public final void whenResourceIsCreatedWithNewAssociation_then409IsReceived(){
62108
final Role newResource = getAPI().createNewEntity();
63-
newResource.getPrivileges().add( getAssociationTemplate().createNewEntity() );
109+
newResource.getPrivileges().add( getAssociationAPI().createNewEntity() );
64110

65111
// When
66112
final Response response = getAPI().createAsResponse( newResource );
@@ -70,44 +116,43 @@ public final void whenRoleIsCreatedWithNewPrivilege_then409IsReceived(){
70116
}
71117

72118
@Test
73-
public final void whenRoleIsCreatedWithExistingPrivilege_then201IsReceived(){
74-
final Privilege existingAssociation = getAssociationTemplate().create( getAssociationTemplate().createNewEntity() );
119+
public final void whenResourceIsCreatedWithInvalidAssociation_then409IsReceived(){
120+
final Privilege invalidAssociation = getAssociationAPI().createNewEntity();
121+
getAssociationAPI().invalidate( invalidAssociation );
75122
final Role newResource = getAPI().createNewEntity();
76-
newResource.getPrivileges().add( existingAssociation );
123+
newResource.getPrivileges().add( invalidAssociation );
77124

78125
// When
79126
final Response response = getAPI().createAsResponse( newResource );
80127

81128
// Then
82-
assertThat( response.getStatusCode(), is( 201 ) );
129+
assertThat( response.getStatusCode(), is( 409 ) );
83130
}
84131

85132
@Test
86-
public final void whenResourceIsCreatedWithInvalidAssociation_then409IsReceived(){
87-
final Privilege invalidAssociation = getAssociationTemplate().createNewEntity();
88-
getAssociationTemplate().invalidate( invalidAssociation );
133+
public final void whenResourceIsCreatedWithExistingAssociation_then201IsReceived(){
134+
final Privilege existingAssociation = getAssociationAPI().create( getAssociationAPI().createNewEntity() );
89135
final Role newResource = getAPI().createNewEntity();
90-
newResource.getPrivileges().add( invalidAssociation );
136+
newResource.getPrivileges().add( existingAssociation );
91137

92138
// When
93139
final Response response = getAPI().createAsResponse( newResource );
94140

95141
// Then
96-
assertThat( response.getStatusCode(), is( 409 ) );
142+
assertThat( response.getStatusCode(), is( 201 ) );
97143
}
98144

99145
@Test
100-
public final void whenCreatingNewResourceWithExistingAssociation_thenAssociationsAreCorrectlyPersisted(){
101-
final Privilege existingAssociation = getAssociationTemplate().create( getAssociationTemplate().createNewEntity() );
146+
public final void whenResourceIsCreatedWithExistingAssociation_thenAssociationIsLinkedToTheResource(){
147+
final Privilege existingAssociation = getAssociationAPI().create( getAssociationAPI().createNewEntity() );
102148
final Role resourceToCreate = getAPI().createNewEntity();
103-
resourceToCreate.getPrivileges().add( existingAssociation );
104149

105150
// When
151+
resourceToCreate.getPrivileges().add( existingAssociation );
106152
final Role existingResource = getAPI().create( resourceToCreate );
107-
final Set< Privilege > associationsOfExistingResource = existingResource.getPrivileges();
108-
Preconditions.checkState( associationsOfExistingResource.size() == 1 );
109153

110-
assertThat( existingAssociation, equalTo( associationsOfExistingResource.iterator().next() ) );
154+
// Then
155+
assertThat( existingResource.getPrivileges(), hasItem( existingAssociation ) );
111156
}
112157

113158
// update
@@ -116,7 +161,7 @@ public final void whenCreatingNewResourceWithExistingAssociation_thenAssociation
116161
public final void givenResourceExists_whenResourceIsUpdatedWithExistingAsscoaition_thenAssociationIsCorrectlyUpdated(){
117162
// Given
118163
final Role existingResource = getAPI().create( getAPI().createNewEntity() );
119-
final Privilege existingAssociation = getAssociationTemplate().create( getAssociationTemplate().createNewEntity() );
164+
final Privilege existingAssociation = getAssociationAPI().create( getAssociationAPI().createNewEntity() );
120165
existingResource.setPrivileges( Sets.newHashSet( existingAssociation ) );
121166

122167
// When
@@ -127,11 +172,22 @@ public final void givenResourceExists_whenResourceIsUpdatedWithExistingAsscoaiti
127172
assertThat( updatedResource.getPrivileges(), hasItem( existingAssociation ) );
128173
}
129174

175+
@Test
176+
public final void givenExistingResourceAndExistingAssociation_whenUpdatingResourceWithTheAssociation_thanAssociationCorrectlyDone(){
177+
final Role existingResource = getAPI().create( getAPI().createNewEntity() );
178+
final Privilege existingAssociation = getAssociationAPI().create( getAssociationAPI().createNewEntity() );
179+
existingResource.setPrivileges( Sets.newHashSet( existingAssociation ) );
180+
181+
getAPI().update( existingResource );
182+
final Role updatedResource = getAPI().findOne( existingResource.getId() );
183+
assertThat( updatedResource.getPrivileges(), hasItem( existingAssociation ) );
184+
}
185+
130186
// scenarios
131187

132188
@Test
133189
public final void whenScenarioOfWorkingWithAssociations_thenTheChangesAreCorrectlyPersisted(){
134-
final Privilege existingAssociation = getAssociationTemplate().create( getAssociationTemplate().createNewEntity() );
190+
final Privilege existingAssociation = getAssociationAPI().create( getAssociationAPI().createNewEntity() );
135191
final Role resource1 = new Role( randomAlphabetic( 6 ), Sets.newHashSet( existingAssociation ) );
136192

137193
final Role resource1ViewOfServerBefore = getAPI().create( resource1 );
@@ -143,19 +199,7 @@ public final void whenScenarioOfWorkingWithAssociations_thenTheChangesAreCorrect
143199
final Role resource1ViewOfServerAfter = getAPI().findOne( resource1ViewOfServerBefore.getId() );
144200
assertThat( resource1ViewOfServerAfter.getPrivileges(), hasItem( existingAssociation ) );
145201
}
146-
147-
@Test
148-
public final void whenCreatingNewResourceWithExistingAssociations_thenNewResourceIsCorrectlyCreated(){
149-
final Privilege existingAssociation = getAssociationTemplate().create( getAssociationTemplate().createNewEntity() );
150-
final Role newResource = getAPI().createNewEntity();
151-
newResource.getPrivileges().add( existingAssociation );
152-
getAPI().create( newResource );
153-
154-
final Role newResource2 = getAPI().createNewEntity();
155-
newResource2.getPrivileges().add( existingAssociation );
156-
getAPI().create( newResource2 );
157-
}
158-
202+
159203
// template
160204

161205
@Override
@@ -175,7 +219,7 @@ protected final RequestSpecification givenAuthenticated(){
175219

176220
// util
177221

178-
final PrivilegeRESTTemplateImpl getAssociationTemplate(){
222+
final PrivilegeRESTTemplateImpl getAssociationAPI(){
179223
return associationRestTemplate;
180224
}
181225

0 commit comments

Comments
 (0)