Skip to content

Commit a6ea880

Browse files
extensive integration testing work
1 parent 0020130 commit a6ea880

16 files changed

Lines changed: 360 additions & 179 deletions

File tree

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

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.rest.common.util.QueryConstants;
1010
import org.rest.common.util.SearchCommonUtil;
1111
import org.rest.common.web.WebConstants;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
1214
import org.springframework.beans.factory.annotation.Autowired;
1315
import org.springframework.beans.factory.annotation.Qualifier;
1416
import org.springframework.http.HttpEntity;
@@ -17,29 +19,28 @@
1719
import org.springframework.http.ResponseEntity;
1820
import org.springframework.web.client.HttpClientErrorException;
1921
import org.springframework.web.client.RestTemplate;
20-
import org.springframework.web.util.UriComponentsBuilder;
2122

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

2425
@SuppressWarnings("rawtypes")
2526
public abstract class AbstractClientRESTTemplate<T extends INameableEntity> implements IClientTemplate<T> {
27+
protected final Logger logger = LoggerFactory.getLogger(getClass());
2628

2729
protected Class<T> clazz;
2830

29-
@Autowired
30-
protected RestTemplate restTemplate;
3131
@Autowired
3232
@Qualifier("jacksonMarshaller")
3333
protected IMarshaller marshaller;
3434

35+
@Autowired
36+
protected RestTemplate restTemplate;
37+
3538
public AbstractClientRESTTemplate(final Class<T> clazzToSet) {
3639
super();
3740

3841
clazz = clazzToSet;
3942
}
4043

41-
// API
42-
4344
// find - one
4445

4546
@Override
@@ -61,13 +62,13 @@ public final T findOneByURI(final String uri) {
6162
// find one - by attributes
6263

6364
@Override
64-
public T findByName(final String name) {
65+
public final T findByName(final String name) {
6566
return findOneByAttributes("name", name);
6667
}
6768

6869
@Override
69-
public T findOneByAttributes(final String... attributes) {
70-
final List<T> resourcesByName = findAllByURI(getURI() + QueryConstants.QUERY_PREFIX + constructURI(attributes));
70+
public final T findOneByAttributes(final String... attributes) {
71+
final List<T> resourcesByName = findAllByURI(getURI() + QueryConstants.QUERY_PREFIX + SearchCommonUtil.constructURI(attributes));
7172
if (resourcesByName.isEmpty()) {
7273
return null;
7374
}
@@ -89,6 +90,7 @@ public final List<T> findAllPaginatedAndSorted(final int page, final int size, f
8990
return marshaller.decodeList(allPaginatedAndSortedAsResponse.getBody(), clazz);
9091
}
9192

93+
@SuppressWarnings("unchecked")
9294
@Override
9395
public final List<T> findAllSorted(final String sortBy, final String sortOrder) {
9496
final ResponseEntity<List> findAllResponse = restTemplate.exchange(getURI() + QueryConstants.Q_SORT_BY + sortBy, HttpMethod.GET, findRequestEntity(), List.class);
@@ -97,16 +99,24 @@ public final List<T> findAllSorted(final String sortBy, final String sortOrder)
9799

98100
@Override
99101
public final List<T> findAllPaginated(final int page, final int size) {
100-
throw new UnsupportedOperationException();
102+
final StringBuilder uri = new StringBuilder(getURI());
103+
uri.append(QueryConstants.QUESTIONMARK);
104+
uri.append("page=");
105+
uri.append(page);
106+
uri.append(SearchCommonUtil.SEPARATOR_AMPER);
107+
uri.append("size=");
108+
uri.append(size);
109+
final ResponseEntity<String> findAllResponse = restTemplate.exchange(uri.toString(), HttpMethod.GET, findRequestEntity(), String.class);
110+
return getMarshaller().decodeList(findAllResponse.getBody(), clazz);
101111
}
102112

103113
@Override
104114
public final List<T> findAllByAttributes(final String... attributes) {
105-
final List<T> resourcesByAttributes = findAllByURI(getURI() + QueryConstants.QUERY_PREFIX + constructURI(attributes));
115+
final List<T> resourcesByAttributes = findAllByURI(getURI() + QueryConstants.QUERY_PREFIX + SearchCommonUtil.constructURI(attributes));
106116
return resourcesByAttributes;
107117
}
108118

109-
public ResponseEntity<String> findAllPaginatedAndSortedAsResponse(final int page, final int size, final String sortBy, final String sortOrder) {
119+
final ResponseEntity<String> findAllPaginatedAndSortedAsResponse(final int page, final int size, final String sortBy, final String sortOrder) {
110120
final StringBuilder uri = new StringBuilder(getURI());
111121
uri.append(QueryConstants.QUESTIONMARK);
112122
uri.append("page=");
@@ -129,10 +139,17 @@ public ResponseEntity<String> findAllPaginatedAndSortedAsResponse(final int page
129139
return restTemplate.exchange(uri.toString(), HttpMethod.GET, findRequestEntity(), String.class);
130140
}
131141

142+
@SuppressWarnings("unchecked")
143+
@Override
144+
public final List<T> findAllByURI(final String uri) {
145+
final ResponseEntity<List> response = restTemplate.exchange(uri, HttpMethod.GET, findRequestEntity(), List.class);
146+
return response.getBody();
147+
}
148+
132149
// create
133150

134151
@Override
135-
public T create(final T resource) {
152+
public final T create(final T resource) {
136153
final String locationOfCreatedResource = createAsURI(resource);
137154

138155
return findOneByURI(locationOfCreatedResource);
@@ -141,7 +158,7 @@ public T create(final T resource) {
141158
@Override
142159
public final String createAsURI(final T resource) {
143160
givenAuthenticated();
144-
final ResponseEntity responseEntity = restTemplate.exchange(getURI(), HttpMethod.POST, new HttpEntity<T>(resource, createContentTypeHeaders()), List.class);
161+
final ResponseEntity<List> responseEntity = restTemplate.exchange(getURI(), HttpMethod.POST, new HttpEntity<T>(resource, createContentTypeHeaders()), List.class);
145162

146163
final String locationOfCreatedResource = responseEntity.getHeaders().getLocation().toString();
147164
Preconditions.checkNotNull(locationOfCreatedResource);
@@ -154,7 +171,7 @@ public final String createAsURI(final T resource) {
154171
@Override
155172
public final void update(final T resource) {
156173
givenAuthenticated();
157-
final ResponseEntity responseEntity = restTemplate.exchange(getURI(), HttpMethod.PUT, new HttpEntity<T>(resource, createContentTypeHeaders()), clazz);
174+
final ResponseEntity<T> responseEntity = restTemplate.exchange(getURI(), HttpMethod.PUT, new HttpEntity<T>(resource, createContentTypeHeaders()), clazz);
158175
Preconditions.checkState(responseEntity.getStatusCode().value() == 200);
159176
}
160177

@@ -175,25 +192,20 @@ public final void deleteAll() {
175192
// search
176193

177194
@Override
178-
public List<T> search(final Triple<String, ClientOperation, String>... constraints) {
195+
public final List<T> search(final Triple<String, ClientOperation, String>... constraints) {
179196
throw new UnsupportedOperationException();
180197
}
181198

182199
// count
183200

184201
@Override
185-
public long count() {
202+
public final long count() {
186203
throw new UnsupportedOperationException();
187204
}
188205

189206
// util
190207

191-
final List<T> findAllByURI(final String uri) {
192-
final ResponseEntity<List> response = restTemplate.exchange(uri, HttpMethod.GET, findRequestEntity(), List.class);
193-
return response.getBody();
194-
}
195-
196-
protected HttpHeaders createGetHeaders() {
208+
protected final HttpHeaders createGetHeaders() {
197209
final HttpHeaders headers = new HttpHeaders() {
198210
{
199211
set(com.google.common.net.HttpHeaders.ACCEPT, marshaller.getMime());
@@ -211,22 +223,7 @@ protected final HttpHeaders createContentTypeHeaders() {
211223
return headers;
212224
}
213225

214-
protected static String constructURI(final String... attributes) {
215-
Preconditions.checkNotNull(attributes);
216-
Preconditions.checkArgument(attributes.length > 0);
217-
Preconditions.checkArgument(attributes.length % 2 == 0);
218-
219-
final UriComponentsBuilder queryParam = UriComponentsBuilder.newInstance();
220-
for (int i = 0; i <= attributes.length / 2; i += 2) {
221-
queryParam.queryParam(attributes[i], attributes[i + 1]);
222-
}
223-
224-
String query = queryParam.build().encode().getQuery();
225-
// query = query.replaceAll("=", "%3D");
226-
return query.replaceAll("&", ",");
227-
}
228-
229-
protected HttpEntity<Void> findRequestEntity() {
226+
protected final HttpEntity<Void> findRequestEntity() {
230227
return new HttpEntity<Void>(createGetHeaders());
231228
}
232229

@@ -253,4 +250,8 @@ protected final void digestAuth() {
253250
throw new UnsupportedOperationException();
254251
}
255252

253+
public final IMarshaller getMarshaller() {
254+
return marshaller;
255+
}
256+
256257
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.rest.common.client.template;
22

3-
import java.util.List;
4-
53
import org.rest.common.IOperations;
64
import org.rest.common.persistence.model.INameableEntity;
75
import org.rest.common.persistence.service.INameSupport;
@@ -10,12 +8,6 @@ public interface IClientTemplate<T extends INameableEntity> extends IOperations<
108

119
IClientTemplate<T> givenAuthenticated();
1210

13-
T findOneByAttributes(final String... attributes);
14-
15-
List<T> findAllByAttributes(final String... attributes);
16-
17-
T findOneByURI(final String uri);
18-
1911
//
2012

2113
String getURI();

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.rest.common.client.template;
22

3+
import java.util.List;
4+
35
import org.rest.common.persistence.model.IEntity;
46

57
public interface ITemplateAsURI<T extends IEntity> {
@@ -8,4 +10,18 @@ public interface ITemplateAsURI<T extends IEntity> {
810

911
String createAsURI(final T resource);
1012

13+
// find - one
14+
15+
T findOneByURI(final String uri);
16+
17+
// find - all
18+
19+
List<T> findAllByURI(final String uri);
20+
21+
// sandboxed
22+
23+
T findOneByAttributes(final String... attributes);
24+
25+
List<T> findAllByAttributes(final String... attributes);
26+
1127
}

‎common/src/main/java/org/rest/common/util/SearchCommonUtil.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import org.apache.commons.lang3.tuple.ImmutableTriple;
88
import org.rest.common.search.ClientOperation;
9+
import org.springframework.web.util.UriComponentsBuilder;
910

1011
import com.google.common.base.Preconditions;
1112
import com.google.common.collect.Lists;
@@ -112,4 +113,21 @@ static ClientOperation determineOperation(final boolean negated, final String va
112113
return op;
113114
}
114115

116+
// template utils
117+
118+
public static String constructURI(final String... attributes) {
119+
Preconditions.checkNotNull(attributes);
120+
Preconditions.checkArgument(attributes.length > 0);
121+
Preconditions.checkArgument(attributes.length % 2 == 0);
122+
123+
final UriComponentsBuilder queryParam = UriComponentsBuilder.newInstance();
124+
for (int i = 0; i <= attributes.length / 2; i += 2) {
125+
queryParam.queryParam(attributes[i], attributes[i + 1]);
126+
}
127+
128+
String query = queryParam.build().encode().getQuery();
129+
// query = query.replaceAll("=", "%3D");
130+
return query.replaceAll("&", ",");
131+
}
132+
115133
}
Lines changed: 3 additions & 3 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-sec/src/test/java/org/rest/sec/web/privilege/PrivilegePaginationRESTIntegrationTest.java"/>
4+
<listEntry value="/rest-sec/src/test/java/org/rest/sec/web/role/RoleLogicRESTIntegrationTest.java"/>
55
</listAttribute>
66
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
77
<listEntry value="1"/>
@@ -12,10 +12,10 @@
1212
</mapAttribute>
1313
<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
1414
<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
15-
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
15+
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value="givenWorkingWithSpecialCharacters_whtnResourcesIfRetrievedByName_thenResourceIsCorrectlyRetrieved"/>
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.privilege.PrivilegePaginationRESTIntegrationTest"/>
18+
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.rest.sec.web.role.RoleLogicRESTIntegrationTest"/>
1919
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="rest-sec"/>
2020
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/>
2121
</launchConfiguration>

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,19 @@ public UserServiceImpl() {
3838

3939
@Override
4040
public List<User> search(final Triple<String, ClientOperation, String>... constraints) {
41-
throw new UnsupportedOperationException();
41+
final List<Principal> principalsResultedFromSearch = principalService.search(constraints);
42+
final List<User> usersResultedFromSearch = Lists.transform(principalsResultedFromSearch, new PrincipalToUserFunction());
43+
44+
return usersResultedFromSearch;
4245
}
4346

4447
@Override
4548
public Page<User> searchPaginated(final int page, final int size, final Triple<String, ClientOperation, String>... constraints) {
46-
throw new UnsupportedOperationException();
49+
final Page<Principal> principalsPaginated = principalService.searchPaginated(page, size, constraints);
50+
51+
final List<User> usersPaginated = Lists.transform(principalsPaginated.getContent(), new PrincipalToUserFunction());
52+
53+
return new PageImpl<User>(usersPaginated, new PageRequest(page, size, null), principalsPaginated.getTotalElements());
4754
}
4855

4956
// find - one

‎sec/src/test/java/org/rest/common/client/template/URIUnitTest.java‎

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

77
import org.junit.BeforeClass;
88
import org.junit.Test;
9-
import org.rest.common.client.template.AbstractClientRESTTemplate;
109
import org.rest.common.util.SearchCommonUtil;
1110
import org.rest.sec.client.template.UserClientRESTTemplate;
1211

@@ -22,33 +21,33 @@ public static void beforeClass() {
2221

2322
@Test(expected = NullPointerException.class)
2423
public void givenNullArgumentArray_whenURIIsConstructed_thenException() {
25-
AbstractClientRESTTemplate.constructURI((String[]) null);
24+
SearchCommonUtil.constructURI((String[]) null);
2625
}
2726

2827
@Test(expected = IllegalArgumentException.class)
2928
public void givenSingleAttributeInArray_whenURIIsConstructed_thenException() {
30-
AbstractClientRESTTemplate.constructURI(new String[] { "Single" });
29+
SearchCommonUtil.constructURI(new String[] { "Single" });
3130
}
3231

3332
@Test
3433
public void givenTwoAttributesInArray_whenURIIsConstructed_thenNoExceptions() {
35-
AbstractClientRESTTemplate.constructURI(new String[] { "One", "Two" });
34+
SearchCommonUtil.constructURI(new String[] { "One", "Two" });
3635
}
3736

3837
@Test
3938
public void whenURIIsConstructed_thenResultIsNonNull() {
40-
assertNotNull(AbstractClientRESTTemplate.constructURI(new String[] { "One", "Two" }));
39+
assertNotNull(SearchCommonUtil.constructURI(new String[] { "One", "Two" }));
4140
}
4241

4342
@Test
4443
public void whenURIIsConstructed_thenResultIsCorrect() {
45-
final String uri = AbstractClientRESTTemplate.constructURI(new String[] { "key", "value" });
44+
final String uri = SearchCommonUtil.constructURI(new String[] { "key", "value" });
4645
assertThat(uri, equalTo("key=value"));
4746
}
4847

4948
@Test
5049
public void givenTwoParameters_whenURIIsConstructed_thenResultIsCorrect() {
51-
final String uri = AbstractClientRESTTemplate.constructURI(new String[] { "key1", "value1", "key2", "value2" });
50+
final String uri = SearchCommonUtil.constructURI(new String[] { "key1", "value1", "key2", "value2" });
5251
assertThat(uri, equalTo("key1=value1" + SearchCommonUtil.SEPARATOR + "key2=value2"));
5352
}
5453

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
44

55
import org.rest.common.client.template.AbstractRESTTemplate;
6+
import org.rest.common.util.SearchField;
67
import org.rest.sec.client.SecBusinessPaths;
78
import org.rest.sec.model.Privilege;
89
import org.rest.sec.model.Role;
@@ -28,10 +29,15 @@ public RoleRESTTemplateImpl() {
2829
// API
2930

3031
public final Role findByName(final String name) {
31-
final String resourceAsXML = findOneAsMime(getURI() + "?name=" + name);
32+
final String resourceAsXML = findOneByUriAsString(getURI() + "?name=" + name);
3233
return marshaller.decode(resourceAsXML, clazz);
3334
}
3435

36+
// TODO: solve the marshalling issue and replace
37+
public final Role findByNameNew(final String name) {
38+
return findOneByAttributes(SearchField.name.name(), name);
39+
}
40+
3541
// template method
3642

3743
@Override

0 commit comments

Comments
 (0)