Skip to content

ADDED: FileApi with single endpoint 'rawContent'. #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/com/cdancy/bitbucket/rest/BitbucketApi.java
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
import com.cdancy.bitbucket.rest.features.CommentsApi;
import com.cdancy.bitbucket.rest.features.CommitsApi;
import com.cdancy.bitbucket.rest.features.DefaultReviewersApi;
import com.cdancy.bitbucket.rest.features.FileApi;
import com.cdancy.bitbucket.rest.features.ProjectApi;
import com.cdancy.bitbucket.rest.features.PullRequestApi;
import com.cdancy.bitbucket.rest.features.RepositoryApi;
@@ -54,6 +55,9 @@ public interface BitbucketApi extends Closeable {
@Delegate
DefaultReviewersApi defaultReviewersApi();

@Delegate
FileApi fileApi();

@Delegate
ProjectApi projectApi();

51 changes: 51 additions & 0 deletions src/main/java/com/cdancy/bitbucket/rest/features/FileApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 com.cdancy.bitbucket.rest.features;

import com.cdancy.bitbucket.rest.annotations.Documentation;
import com.cdancy.bitbucket.rest.filters.BitbucketAuthentication;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.rest.annotations.Fallback;
import org.jclouds.rest.annotations.RequestFilters;

import javax.inject.Named;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.jclouds.Fallbacks.NullOnNotFoundOr404;

@Produces(MediaType.APPLICATION_JSON)
@RequestFilters(BitbucketAuthentication.class)
@Path("/projects")
public interface FileApi {

@Named("file:raw-content")
@Documentation({"https://jira.atlassian.com/browse/BSERV-4036"})
@Consumes(MediaType.TEXT_PLAIN)
@Path("/{project}/repos/{repo}/raw/{filePath}")
@Fallback(NullOnNotFoundOr404.class)
@GET
String rawContent(@PathParam("project") String project,
@PathParam("repo") String repo,
@PathParam("filePath") String filePath,
@Nullable @QueryParam("at") String commitHash);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 com.cdancy.bitbucket.rest.features;

import static org.assertj.core.api.Assertions.assertThat;

import com.cdancy.bitbucket.rest.BaseBitbucketApiLiveTest;
import com.cdancy.bitbucket.rest.GeneratedTestContents;
import com.cdancy.bitbucket.rest.domain.commit.CommitPage;
import com.cdancy.bitbucket.rest.domain.pullrequest.ChangePage;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

import org.testng.annotations.BeforeClass;

@Test(groups = "live", testName = "FileApiLiveTest", singleThreaded = true)
public class FileApiLiveTest extends BaseBitbucketApiLiveTest {

private GeneratedTestContents generatedTestContents;

private String projectKey;
private String repoKey;
private String commitHash;
private String filePath;

@BeforeClass
public void init() {
generatedTestContents = initGeneratedTestContents();
this.projectKey = generatedTestContents.project.key();
this.repoKey = generatedTestContents.repository.name();

final CommitPage commitPage = api.commitsApi().list(projectKey, repoKey, true, 1, null);
assertThat(commitPage).isNotNull();
assertThat(commitPage.errors().isEmpty()).isTrue();
assertThat(commitPage.values().isEmpty()).isFalse();
assertThat(commitPage.totalCount() > 0).isTrue();
this.commitHash = commitPage.values().get(0).id();

final ChangePage commit = api.commitsApi().listChanges(projectKey, repoKey, commitHash, 0, 100);
assertThat(commit).isNotNull();
assertThat(commit.errors().isEmpty()).isTrue();
assertThat(commit.size() > 0).isTrue();
this.filePath = commit.values().get(0).path()._toString();
}

@Test
public void getRawContent() {
String rawContent = api().rawContent(projectKey, repoKey, filePath, commitHash);
assertThat(rawContent).isNotNull();
}

@Test
public void getRawContentOnNotFound() {
String rawContent = api().rawContent(projectKey, repoKey, randomString() + ".txt", null);
assertThat(rawContent).isNull();
}

@AfterClass
public void fin() {
terminateGeneratedTestContents(generatedTestContents);
}

private FileApi api() {
return api.fileApi();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 com.cdancy.bitbucket.rest.features;

import com.cdancy.bitbucket.rest.BitbucketApi;
import com.cdancy.bitbucket.rest.internal.BaseBitbucketMockTest;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import org.testng.annotations.Test;

import static org.assertj.core.api.Assertions.assertThat;

@Test(groups = "unit", testName = "FileApiMockTest")
public class FileApiMockTest extends BaseBitbucketMockTest {

public void testGetRawContent() throws Exception {
MockWebServer server = mockEtcdJavaWebServer();

final String content = "Hello, World!";
server.enqueue(new MockResponse().setBody(content).setResponseCode(200));
BitbucketApi baseApi = api(server.getUrl("/"));
FileApi api = baseApi.fileApi();
try {

final String projectKey = "PRJ";
final String repoKey = "myrepo";
final String filePath = "some/random/path/MyFile.txt";
final String rawContent = api.rawContent(projectKey, repoKey, filePath, null);
assertThat(rawContent).isNotNull();
assertThat(rawContent).isEqualTo(content);
assertSentAcceptText(server, "GET", "/projects/" + projectKey + "/repos/" + repoKey + "/raw/" + filePath);

} finally {
baseApi.close();
server.shutdown();
}
}

public void testGetRawContentOnNotFound() throws Exception {
MockWebServer server = mockEtcdJavaWebServer();

server.enqueue(new MockResponse().setResponseCode(404));
BitbucketApi baseApi = api(server.getUrl("/"));
FileApi api = baseApi.fileApi();
try {

final String projectKey = "PRJ";
final String repoKey = "myrepo";
final String filePath = "some/random/path/MyFile.txt";
final String rawContent = api.rawContent(projectKey, repoKey, filePath, null);
assertThat(rawContent).isNull();
assertSentAcceptText(server, "GET", "/projects/" + projectKey + "/repos/" + repoKey + "/raw/" + filePath);

} finally {
baseApi.close();
server.shutdown();
}
}
}