/*
 * 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.reactor.client;

import okhttp3.Headers;
import org.cloudfoundry.client.v2.CloudFoundryException;
import org.cloudfoundry.reactor.AbstractApiTest;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.util.Exceptions;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public abstract class AbstractClientApiTest<REQ, RSP> extends AbstractApiTest<REQ, RSP> {

    private static final Pattern BOUNDARY = Pattern.compile("multipart/form-data; boundary=(.+)");

    @Test
    public final void error() throws Exception {
        mockRequest(getInteractionContext().getErrorResponse());
        this.testSubscriber.assertError(CloudFoundryException.class, "CF-UnprocessableEntity(10008): The request is semantically invalid: space_guid and name unique");
        invoke(getValidRequest()).subscribe(this.testSubscriber);

        this.testSubscriber.verify(Duration.ofSeconds(5));
        verify();
    }

    protected static Mono<byte[]> collectByteArray(Flux<byte[]> bytes) {
        return bytes
            .reduceWith(ByteArrayOutputStream::new, (prev, next) -> {
                try {
                    prev.write(next);
                } catch (IOException e) {
                    throw Exceptions.propagate(e);
                }
                return prev;
            })
            .map(ByteArrayOutputStream::toByteArray);
    }

    protected static String extractBoundary(Headers headers) {
        String contentType = headers.get("Content-Type");
        assertNotNull(contentType);

        Matcher matcher = BOUNDARY.matcher(contentType);
        assertTrue(matcher.find());
        return matcher.group(1);
    }

    protected static byte[] getBytes(String path) {
        try (InputStream in = new FileInputStream(new File("src/test/resources", path)); ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            byte[] buffer = new byte[8192];
            int len;

            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }

            return out.toByteArray();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}

@@ -806,24 +806,62 @@
 junit.Test;%0A
+import org.reactivestreams.Publisher;%0A
 import react
@@ -1770,24 +1770,25 @@
 ique%22);%0A
+%0A
         
 invoke(g
@@ -1779,16 +1779,24 @@
         
+addHide(
 invoke(g
@@ -1812,16 +1812,17 @@
 quest())
+)
 .subscri
@@ -1923,32 +1923,403 @@
 erify();%0A    %7D%0A%0A
+    @SuppressWarnings(%22unchecked%22)%0A    private static %3CRSP%3E Publisher%3CRSP%3E addHide(Publisher%3CRSP%3E rspPublisher) %7B%0A        if ( rspPublisher instanceof Flux) %7B%0A            rspPublisher = ((Flux) rspPublisher).hide();%0A        %7D else if ( rspPublisher instanceof Mono) %7B%0A            rspPublisher = ((Mono) rspPublisher).hide();%0A        %7D%0A        return rspPublisher;%0A    %7D%0A%0A
     protected st

Make hide more pervasive in error tests