Skip to content

Commit

Permalink
Fix AppRegistry findAll(Pageable)
Browse files Browse the repository at this point in the history
 - Add ability to retrieve paginated list of AppRegistration resources when Pagination is requested
 - Add test

Resolves spring-cloud#1397
  • Loading branch information
ilayaperumalg committed Jun 23, 2017
1 parent 731cc7d commit 662bbd0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
4 changes: 4 additions & 0 deletions spring-cloud-dataflow-registry/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand All @@ -39,6 +40,9 @@
import org.springframework.cloud.deployer.resource.registry.UriRegistry;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

Expand All @@ -58,6 +62,7 @@
* @author Gunnar Hillert
* @author Thomas Risberg
* @author Eric Bottard
* @author Ilayaperumal Gopinathan
*/
public class AppRegistry {

Expand Down Expand Up @@ -91,6 +96,23 @@ public List<AppRegistration> findAll() {
.collect(Collectors.toList());
}

public Page<AppRegistration> findAll(Pageable pageable) {
List<AppRegistration> appRegistrations = this.uriRegistry.findAll().entrySet().stream()
.flatMap(toValidAppRegistration(metadataUriFromRegistry()))
.collect(Collectors.toList());
appRegistrations.sort(new Comparator<AppRegistration>() {
@Override
public int compare(AppRegistration o1, AppRegistration o2) {
return o1.compareTo(o2);
}
});
long count = appRegistrations.size();
long to = Math.min(count, pageable.getOffset() + pageable.getPageSize());

return new PageImpl<>(appRegistrations.subList(pageable.getOffset(), (int) to), pageable,
appRegistrations.size());
}

public AppRegistration save(String name, ApplicationType type, URI uri, URI metadataUri) {
this.uriRegistry.register(key(name, type), uri);
if (metadataUri != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import static org.hamcrest.Matchers.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.springframework.cloud.dataflow.core.ApplicationType.sink;
import static org.springframework.cloud.dataflow.core.ApplicationType.source;
Expand All @@ -40,6 +48,7 @@
* Unit tests for {@link AppRegistry}.
*
* @author Eric Bottard
* @author Ilayaperumal Gopinathan
*/
public class AppRegistryTests {

Expand Down Expand Up @@ -90,6 +99,27 @@ public void testFindAll() {
hasProperty("metadataUri", nullValue()), hasProperty("type", is(sink)))));
}

@Test
public void testFindAllPageable() {
uriRegistry.register("source.foo", URI.create("classpath:/foo-source"));
uriRegistry.register("sink.foo", URI.create("classpath:/foo-sink"));
uriRegistry.register("source.foo.metadata", URI.create("classpath:/foo-source-metadata"));
uriRegistry.register("source.bar", URI.create("classpath:/bar-source"));
uriRegistry.register("source.bar.metadata", URI.create("classpath:/bar-source-metadata"));

PageRequest pageRequest1 = new PageRequest(0, 2);
Page<AppRegistration> registrations1 = appRegistry.findAll(pageRequest1);
assertTrue(registrations1.getTotalElements() == 3);
assertTrue(registrations1.getContent().size() == 2);
assertTrue(registrations1.getContent().get(0).getName().equals("bar"));
assertTrue(registrations1.getContent().get(1).getName().equals("foo"));
PageRequest pageRequest2 = new PageRequest(1, 2);
Page<AppRegistration> registrations2 = appRegistry.findAll(pageRequest2);
assertTrue(registrations2.getTotalElements() == 3);
assertTrue(registrations2.getContent().size() == 1);
assertTrue(registrations2.getContent().get(0).getName().equals("foo"));
}

@Test
public void testSave() {
appRegistry.save("foo", source, URI.create("classpath:/foo"), URI.create("foo-metadata"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty;
import org.springframework.cloud.dataflow.configuration.metadata.ApplicationConfigurationMetadataResolver;
import org.springframework.cloud.dataflow.core.ApplicationType;
Expand All @@ -42,6 +43,7 @@
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PagedResourcesAssembler;
Expand Down Expand Up @@ -109,16 +111,14 @@ public PagedResources<? extends AppRegistrationResource> list(
@RequestParam(value = "type", required = false) ApplicationType type,
@RequestParam(value = "detailed", defaultValue = "false") boolean detailed) {

List<AppRegistration> list = new ArrayList<>(appRegistry.findAll());
for (Iterator<AppRegistration> iterator = list.iterator(); iterator.hasNext();) {
Page<AppRegistration> pagedRegistrations = appRegistry.findAll(pageable);
for (Iterator<AppRegistration> iterator = pagedRegistrations.iterator(); iterator.hasNext();) {
ApplicationType applicationType = iterator.next().getType();
if (type != null && applicationType != type) {
iterator.remove();
}
}
Collections.sort(list);
return pagedResourcesAssembler
.toResource(new PageImpl<>(list, pageable, list.size()), assembler);
return pagedResourcesAssembler.toResource(pagedRegistrations, this.assembler);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.cloud.dataflow.core.ApplicationType;
import org.springframework.cloud.dataflow.core.StreamAppDefinition;
import org.springframework.cloud.dataflow.core.StreamDefinition;
Expand Down

0 comments on commit 662bbd0

Please sign in to comment.