Skip to content

Commit 0020130

Browse files
consolidation of all operation signatures
1 parent cb54566 commit 0020130

14 files changed

Lines changed: 146 additions & 116 deletions

‎common/.classpath‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
3-
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
44
<attributes>
55
<attribute name="optional" value="true"/>
66
<attribute name="maven.pomderived" value="true"/>
77
</attributes>
88
</classpathentry>
9-
<classpathentry kind="src" output="target/classes" path="src/main/java">
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
1010
<attributes>
1111
<attribute name="optional" value="true"/>
1212
<attribute name="maven.pomderived" value="true"/>

‎common/.settings/org.eclipse.wst.common.component‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<project-modules id="moduleCoreId" project-version="1.5.0">
1+
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
32
<wb-module deploy-name="rest-common">
43
<wb-resource deploy-path="/" source-path="/src/main/java"/>
54
<property name="context-root" value="rest"/>

‎common/src/main/java/org/rest/common/client/template/AbstractClientRESTTemplate.java‎

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.rest.common.persistence.model.INameableEntity;
88
import org.rest.common.search.ClientOperation;
99
import org.rest.common.util.QueryConstants;
10+
import org.rest.common.util.SearchCommonUtil;
1011
import org.rest.common.web.WebConstants;
1112
import org.springframework.beans.factory.annotation.Autowired;
1213
import org.springframework.beans.factory.annotation.Qualifier;
@@ -20,7 +21,7 @@
2021

2122
import com.google.common.base.Preconditions;
2223

23-
@SuppressWarnings({ "unchecked", "rawtypes" })
24+
@SuppressWarnings("rawtypes")
2425
public abstract class AbstractClientRESTTemplate<T extends INameableEntity> implements IClientTemplate<T> {
2526

2627
protected Class<T> clazz;
@@ -78,13 +79,14 @@ public T findOneByAttributes(final String... attributes) {
7879

7980
@Override
8081
public final List<T> findAll() {
81-
final ResponseEntity<List> findAllResponse = restTemplate.exchange(getURI(), HttpMethod.GET, findRequestEntity(), List.class);
82-
return findAllResponse.getBody();
82+
final ResponseEntity<String> findAllResponse = restTemplate.exchange(getURI(), HttpMethod.GET, findRequestEntity(), String.class);
83+
return marshaller.decodeList(findAllResponse.getBody(), clazz);
8384
}
8485

8586
@Override
8687
public final List<T> findAllPaginatedAndSorted(final int page, final int size, final String sortBy, final String sortOrder) {
87-
throw new UnsupportedOperationException();
88+
final ResponseEntity<String> allPaginatedAndSortedAsResponse = findAllPaginatedAndSortedAsResponse(page, size, sortBy, sortOrder);
89+
return marshaller.decodeList(allPaginatedAndSortedAsResponse.getBody(), clazz);
8890
}
8991

9092
@Override
@@ -104,14 +106,34 @@ public final List<T> findAllByAttributes(final String... attributes) {
104106
return resourcesByAttributes;
105107
}
106108

109+
public ResponseEntity<String> findAllPaginatedAndSortedAsResponse(final int page, final int size, final String sortBy, final String sortOrder) {
110+
final StringBuilder uri = new StringBuilder(getURI());
111+
uri.append(QueryConstants.QUESTIONMARK);
112+
uri.append("page=");
113+
uri.append(page);
114+
uri.append(SearchCommonUtil.SEPARATOR_AMPER);
115+
uri.append("size=");
116+
uri.append(size);
117+
Preconditions.checkState(!(sortBy == null && sortOrder != null));
118+
if (sortBy != null) {
119+
uri.append(SearchCommonUtil.SEPARATOR_AMPER);
120+
uri.append(QueryConstants.SORT_BY + "=");
121+
uri.append(sortBy);
122+
}
123+
if (sortOrder != null) {
124+
uri.append(SearchCommonUtil.SEPARATOR_AMPER);
125+
uri.append(QueryConstants.SORT_ORDER + "=");
126+
uri.append(sortOrder);
127+
}
128+
129+
return restTemplate.exchange(uri.toString(), HttpMethod.GET, findRequestEntity(), String.class);
130+
}
131+
107132
// create
108133

109134
@Override
110135
public T create(final T resource) {
111-
final ResponseEntity responseEntity = restTemplate.exchange(getURI(), HttpMethod.POST, new HttpEntity<T>(resource, createContentTypeHeaders()), Void.class);
112-
113-
final String locationOfCreatedResource = responseEntity.getHeaders().getLocation().toString();
114-
Preconditions.checkNotNull(locationOfCreatedResource);
136+
final String locationOfCreatedResource = createAsURI(resource);
115137

116138
return findOneByURI(locationOfCreatedResource);
117139
}
@@ -131,6 +153,7 @@ public final String createAsURI(final T resource) {
131153

132154
@Override
133155
public final void update(final T resource) {
156+
givenAuthenticated();
134157
final ResponseEntity responseEntity = restTemplate.exchange(getURI(), HttpMethod.PUT, new HttpEntity<T>(resource, createContentTypeHeaders()), clazz);
135158
Preconditions.checkState(responseEntity.getStatusCode().value() == 200);
136159
}
@@ -209,8 +232,6 @@ protected HttpEntity<Void> findRequestEntity() {
209232

210233
// template method
211234

212-
public abstract String getURI();
213-
214235
@Override
215236
public final IClientTemplate<T> givenAuthenticated() {
216237
if (isBasicAuth()) {

‎common/src/main/java/org/rest/common/client/template/IClientTemplate.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ public interface IClientTemplate<T extends INameableEntity> extends IOperations<
1616

1717
T findOneByURI(final String uri);
1818

19+
//
20+
21+
String getURI();
22+
1923
}
Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package org.rest.sec.web.privilege;
22

3-
import org.rest.common.web.WebConstants;
43
import org.rest.sec.client.template.PrivilegeRESTTemplateImpl;
54
import org.rest.sec.model.Privilege;
65
import org.rest.sec.test.SecLogicRESTIntegrationTest;
76
import org.springframework.beans.factory.annotation.Autowired;
87

9-
import com.jayway.restassured.specification.RequestSpecification;
10-
118
public class PrivilegeLogicRESTIntegrationTest extends SecLogicRESTIntegrationTest<Privilege> {
129

1310
@Autowired
@@ -26,14 +23,4 @@ protected final PrivilegeRESTTemplateImpl getAPI() {
2623
return restTemplate;
2724
}
2825

29-
@Override
30-
protected final String getURI() {
31-
return getAPI().getURI() + WebConstants.PATH_SEP;
32-
}
33-
34-
@Override
35-
protected final RequestSpecification givenAuthenticated() {
36-
return getAPI().givenAuthenticated();
37-
}
38-
3926
}

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.hamcrest.Matchers;
1111
import org.junit.Ignore;
1212
import org.junit.Test;
13-
import org.rest.common.web.WebConstants;
1413
import org.rest.sec.client.template.PrivilegeRESTTemplateImpl;
1514
import org.rest.sec.client.template.RoleRESTTemplateImpl;
1615
import org.rest.sec.model.Privilege;
@@ -20,7 +19,6 @@
2019

2120
import com.google.common.collect.Sets;
2221
import com.jayway.restassured.response.Response;
23-
import com.jayway.restassured.specification.RequestSpecification;
2422

2523
public class RoleLogicRESTIntegrationTest extends SecLogicRESTIntegrationTest<Role> {
2624

@@ -212,16 +210,6 @@ protected final RoleRESTTemplateImpl getAPI() {
212210
return restTemplate;
213211
}
214212

215-
@Override
216-
protected final String getURI() {
217-
return getAPI().getURI() + WebConstants.PATH_SEP;
218-
}
219-
220-
@Override
221-
protected final RequestSpecification givenAuthenticated() {
222-
return getAPI().givenAuthenticated();
223-
}
224-
225213
// util
226214

227215
final PrivilegeRESTTemplateImpl getAssociationAPI() {

‎sec/src/test/java/org/rest/sec/web/user/UserLogicRESTIntegrationTest.java‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.hamcrest.Matchers;
1111
import org.junit.Ignore;
1212
import org.junit.Test;
13-
import org.rest.common.web.WebConstants;
1413
import org.rest.sec.client.template.RoleRESTTemplateImpl;
1514
import org.rest.sec.client.template.UserRESTTemplateImpl;
1615
import org.rest.sec.model.Role;
@@ -20,7 +19,6 @@
2019

2120
import com.google.common.collect.Sets;
2221
import com.jayway.restassured.response.Response;
23-
import com.jayway.restassured.specification.RequestSpecification;
2422

2523
public class UserLogicRESTIntegrationTest extends SecLogicRESTIntegrationTest<User> {
2624

@@ -126,16 +124,6 @@ public final void whenScenarioOfWorkingWithAssociations_thenTheChangesAreCorrect
126124

127125
// template method
128126

129-
@Override
130-
protected final String getURI() {
131-
return getAPI().getURI() + WebConstants.PATH_SEP;
132-
}
133-
134-
@Override
135-
protected final RequestSpecification givenAuthenticated() {
136-
return getAPI().givenAuthenticated();
137-
}
138-
139127
@Override
140128
protected final UserRESTTemplateImpl getAPI() {
141129
return userRestTemplate;

‎test/src/main/java/org/rest/common/client/AbstractClientRESTIntegrationTest.java‎

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package org.rest.common.client;
22

3+
import static org.hamcrest.CoreMatchers.not;
34
import static org.hamcrest.Matchers.equalTo;
45
import static org.junit.Assert.assertEquals;
5-
import static org.junit.Assert.assertFalse;
66
import static org.junit.Assert.assertNotNull;
77
import static org.junit.Assert.assertNull;
88
import static org.junit.Assert.assertThat;
99

1010
import java.util.List;
1111

12+
import org.hamcrest.Matchers;
1213
import org.junit.Ignore;
1314
import org.junit.Test;
1415
import org.rest.common.client.template.IClientTemplate;
1516
import org.rest.common.persistence.model.INameableEntity;
1617
import org.rest.common.util.IDUtils;
18+
import org.rest.common.web.WebConstants;
1719

1820
public abstract class AbstractClientRESTIntegrationTest<T extends INameableEntity> {
1921

@@ -24,8 +26,6 @@ public AbstractClientRESTIntegrationTest() {
2426
super();
2527
}
2628

27-
// @Rule public ExpectedException thrown = ExpectedException.none();
28-
2929
// tests
3030

3131
// find one
@@ -59,7 +59,7 @@ public final void givenResourceDoesNotExist_whenResourceIsRetrieved_thenNoResour
5959
@Ignore("not yet done")
6060
public final void givenResourceExists_whenResourceIsSearchedByName_thenNoExceptions() {
6161
// Given
62-
final T existingResource = getAPI().givenAuthenticated().create(getEntityOps().createNewEntity());
62+
final T existingResource = getAPI().create(getEntityOps().createNewEntity());
6363

6464
// When
6565
getAPI().findByName(existingResource.getName());
@@ -144,32 +144,65 @@ public final void givenResourceExists_whenResourceIsSearchedByNagatedNameAttribu
144144
// find all
145145

146146
@Test
147-
public final void whenAllResourcesAreRetrieved_thenResourcesExist() {
147+
public void whenAllResourcesAreRetrieved_thenResourcesAreCorrectlyRetrieved() {
148+
// Given
149+
getAPI().createAsURI(getEntityOps().createNewEntity());
150+
151+
// When
148152
final List<T> allResources = getAPI().findAll();
149-
assertFalse(allResources.isEmpty());
153+
154+
// Then
155+
assertThat(allResources, not(Matchers.<T> empty()));
156+
}
157+
158+
@Test
159+
public void whenAllResourcesAreRetrieved_thenResourcesHaveIds() {
160+
// Given
161+
this.getAPI().createAsURI(getEntityOps().createNewEntity());
162+
163+
// When
164+
final List<T> allResources = getAPI().findAll();
165+
166+
// Then
167+
for (final T resource : allResources) {
168+
assertNotNull(resource.getId());
169+
}
150170
}
151171

172+
// create
173+
152174
// update
153175

154176
@Test
155-
public final void whenResourceIsUpdated_thenTheChangesAreCorrectlyPersisted() {
156-
final T existingResource = getAPI().givenAuthenticated().create(getEntityOps().createNewEntity());
177+
public void givenResourceExists_whenResourceIsUpdated_thenNoExceptions() {
178+
// Given
179+
final T existingResource = getAPI().create(getEntityOps().createNewEntity());
180+
181+
// When
182+
getAPI().update(existingResource);
183+
}
184+
185+
@Test
186+
public void givenResourceExists_whenResourceIsUpdated_thenUpdatesArePersisted() {
187+
// Given
188+
final T existingResource = getAPI().create(getEntityOps().createNewEntity());
157189

158190
// When
159191
getEntityOps().change(existingResource);
160-
getAPI().givenAuthenticated().update(existingResource);
161-
final T updatedResource = getAPI().findOne(existingResource.getId());
192+
getAPI().update(existingResource);
193+
194+
final T updatedResourceFromServer = getAPI().findOne(existingResource.getId());
162195

163196
// Then
164-
assertThat(existingResource, equalTo(updatedResource));
197+
assertEquals(existingResource, updatedResourceFromServer);
165198
}
166199

167200
// delete
168201

169202
@Test
170203
public final void givenResourceExists_whenResourceIsDeleted_thenResourceNoLongerExists() {
171204
// Given
172-
final T existingResource = getAPI().givenAuthenticated().create(getEntityOps().createNewEntity());
205+
final T existingResource = getAPI().create(getEntityOps().createNewEntity());
173206

174207
// When
175208
getAPI().delete(existingResource.getId());
@@ -184,4 +217,8 @@ public final void givenResourceExists_whenResourceIsDeleted_thenResourceNoLonger
184217

185218
protected abstract IEntityOperations<T> getEntityOps();
186219

220+
protected final String getURI() {
221+
return getAPI().getURI() + WebConstants.PATH_SEP;
222+
}
223+
187224
}

0 commit comments

Comments
 (0)