/*
 * Copyright 2013-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.cloudfoundry.client.v2;

import org.cloudfoundry.AbstractIntegrationTest;
import org.cloudfoundry.client.CloudFoundryClient;
import org.cloudfoundry.client.v2.stacks.GetStackRequest;
import org.cloudfoundry.client.v2.stacks.ListStacksRequest;
import org.cloudfoundry.client.v2.stacks.StackResource;
import org.cloudfoundry.util.PaginationUtils;
import org.cloudfoundry.util.ResourceUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public final class StacksTest extends AbstractIntegrationTest {

    @Autowired
    private CloudFoundryClient cloudFoundryClient;

    @Autowired
    private String stackName;

    @Test
    public void get() {
        getStackId(this.cloudFoundryClient, this.stackName)
            .then(stackId -> this.cloudFoundryClient.stacks()
                .get(GetStackRequest.builder()
                    .stackId(stackId)
                    .build()))
            .map(resource -> resource.getEntity().getName())
            .subscribe(this.testSubscriber()
                .assertEquals(this.stackName));
    }

    @Test
    public void list() {
        PaginationUtils
            .requestResources(page -> this.cloudFoundryClient.stacks()
                .list(ListStacksRequest.builder()
                    .page(page)
                    .build()))
            .map(resource -> resource.getEntity().getName())
            .subscribe(this.testSubscriber()
                .assertEquals(this.stackName));
    }

    @Test
    public void listFilterByName() {
        PaginationUtils
            .requestResources(page -> this.cloudFoundryClient.stacks()
                .list(ListStacksRequest.builder()
                    .name(this.stackName)
                    .page(page)
                    .build()))
            .map(resource -> resource.getEntity().getName())
            .subscribe(this.testSubscriber()
                .assertEquals(this.stackName));
    }

    private static Mono<String> getStackId(CloudFoundryClient cloudFoundryClient, String stackName) {
        return requestListStacks(cloudFoundryClient, stackName)
            .single()
            .map(ResourceUtils::getId);
    }

    private static Flux<StackResource> requestListStacks(CloudFoundryClient cloudFoundryClient, String stackName) {
        return PaginationUtils
            .requestResources(page -> cloudFoundryClient.stacks()
                .list(ListStacksRequest.builder()
                    .name(stackName)
                    .page(page)
                    .build()));
    }

}

@@ -1175,16 +1175,87 @@
 .Mono;%0A%0A
+import static org.cloudfoundry.util.PaginationUtils.requestResources;%0A%0A
 public f
@@ -1888,24 +1888,124 @@
 id list() %7B%0A
+        getStackId(this.cloudFoundryClient, this.stackName)%0A            .flatMap(stackId -%3E%0A        
         Pagi
@@ -2020,32 +2020,40 @@
 ils%0A            
+        
 .requestResource
@@ -2103,32 +2103,40 @@
                 
+        
 .list(ListStacks
@@ -2165,32 +2165,40 @@
                 
+ 
+       
 .page(page)%0A    
@@ -2205,32 +2205,40 @@
                 
+        
 .build()))%0A     
@@ -2224,32 +2224,120 @@
      .build()))%0A
+                    .filter(resource -%3E ResourceUtils.getId(resource).equals(stackId)))%0A
             .map
@@ -2540,37 +2540,8 @@
     
-PaginationUtils%0A            .
 requ
@@ -2598,36 +2598,32 @@
 s()%0A            
-    
 .list(ListStacks
@@ -2632,36 +2632,32 @@
 quest.builder()%0A
-    
                 
@@ -2686,36 +2686,32 @@
                 
-    
 .page(page)%0A    
@@ -2698,36 +2698,32 @@
     .page(page)%0A
-    
                 
@@ -3259,32 +3259,16 @@
   return
- PaginationUtils
 %0A       
@@ -3264,33 +3264,32 @@
 urn%0A            
-.
 requestResources

Fix StacksTest list()