org.commonjava.indy.ftest.core.content.DownloadContentHasLengthHeaderTest.java Source code

Java tutorial

Introduction

Here is the source code for org.commonjava.indy.ftest.core.content.DownloadContentHasLengthHeaderTest.java

Source

/**
 * Copyright (C) 2011-2018 Red Hat, Inc. (https://github.com/Commonjava/indy)
 *
 * 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.commonjava.indy.ftest.core.content;

import org.apache.http.HttpResponse;
import org.commonjava.indy.client.core.IndyClientModule;
import org.commonjava.indy.client.core.helper.HttpResources;
import org.commonjava.indy.client.core.helper.PathInfo;
import org.commonjava.indy.client.core.module.IndyRawHttpModule;
import org.commonjava.indy.ftest.core.AbstractContentManagementTest;
import org.commonjava.indy.model.core.RemoteRepository;
import org.commonjava.test.http.expect.ExpectationServer;
import org.junit.Rule;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static org.commonjava.indy.model.core.StoreType.remote;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

public class DownloadContentHasLengthHeaderTest extends AbstractContentManagementTest {

    @Rule
    public ExpectationServer server = new ExpectationServer("repos");

    @Override
    protected boolean createStandardTestStructures() {
        return false;
    }

    @Test
    public void proxyRemoteArtifact() throws Exception {
        byte[] data = ("This is a test: " + System.nanoTime()).getBytes();
        final InputStream stream = new ByteArrayInputStream(data);
        final String path = "org/foo/foo-project/1/foo-1.txt";
        server.expect(server.formatUrl(STORE, path), 200, stream);

        client.stores().create(new RemoteRepository(STORE, server.formatUrl(STORE)), "adding test proxy",
                RemoteRepository.class);

        try (HttpResources httpResources = client.module(IndyRawHttpModule.class).getHttp()
                .getRaw(client.content().contentPath(remote, STORE, path))) {
            HttpResponse response = httpResources.getResponse();

            String contentLength = response.getFirstHeader("Content-Length").getValue();
            assertThat("Wrong content-length for download: " + contentLength + " (should have been: " + data.length
                    + ")", contentLength, equalTo(Integer.toString(data.length)));
        }

        final PathInfo result = client.content().getInfo(remote, STORE, path);

        assertThat("no result", result, notNullValue());
        assertThat("doesn't exist", result.exists(), equalTo(true));
    }

    @Override
    protected Collection<IndyClientModule> getAdditionalClientModules() {
        List<IndyClientModule> mods = new ArrayList<>();
        Collection<IndyClientModule> fromParent = super.getAdditionalClientModules();

        if (fromParent != null) {
            mods.addAll(fromParent);
        }

        mods.add(new IndyRawHttpModule());

        return mods;
    }
}