Skip to content

Commit

Permalink
ADDED: TagApi with endpoints 'create' and 'get'.
Browse files Browse the repository at this point in the history
  • Loading branch information
cdancy committed Jun 22, 2016
1 parent 9fb9515 commit 055397b
Show file tree
Hide file tree
Showing 8 changed files with 359 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/main/java/com/cdancy/bitbucket/rest/BitbucketApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@

import java.io.Closeable;

import com.cdancy.bitbucket.rest.features.ProjectApi;
import com.cdancy.bitbucket.rest.features.RepositoryApi;
import com.cdancy.bitbucket.rest.features.*;
import org.jclouds.rest.annotations.Delegate;

import com.cdancy.bitbucket.rest.features.PullRequestApi;
import com.cdancy.bitbucket.rest.features.SystemApi;

public interface BitbucketApi extends Closeable {

@Delegate
Expand All @@ -39,4 +35,7 @@ public interface BitbucketApi extends Closeable {

@Delegate
SystemApi systemApi();

@Delegate
TagApi tagApi();
}
61 changes: 61 additions & 0 deletions src/main/java/com/cdancy/bitbucket/rest/domain/tags/Tag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.domain.tags;

import com.cdancy.bitbucket.rest.error.Error;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.json.SerializedNames;

import java.util.List;

@AutoValue
public abstract class Tag {

@Nullable
public abstract String id();

@Nullable
public abstract String displayId();

@Nullable
public abstract String type();

@Nullable
public abstract String latestCommit();

@Nullable
public abstract String latestChangeset();

@Nullable
public abstract String hash();

@Nullable
public abstract List<Error> errors();

Tag() {
}

@SerializedNames({ "id", "displayId", "type", "latestCommit", "latestChangeset", "hash", "errors" })
public static Tag create(String id, String displayId, String type,
String latestCommit, String latestChangeset, String hash, List<Error> errors) {
return new AutoValue_Tag(id, displayId, type, latestCommit, latestChangeset, hash,
errors != null ? ImmutableList.copyOf(errors) : ImmutableList.<Error> of());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.cdancy.bitbucket.rest.domain.project.Project;
import com.cdancy.bitbucket.rest.domain.pullrequest.MergeStatus;
import com.cdancy.bitbucket.rest.domain.repository.Repository;
import com.cdancy.bitbucket.rest.domain.tags.Tag;
import org.jclouds.Fallback;

import com.cdancy.bitbucket.rest.domain.pullrequest.PullRequest;
Expand All @@ -48,6 +49,15 @@ public Object createOrPropagate(Throwable t) throws Exception {
}
}

public static final class TagOnError implements Fallback<Object> {
public Object createOrPropagate(Throwable t) throws Exception {
if (checkNotNull(t, "throwable") != null) {
return createTagFromErrors(getErrors(t.getMessage()));
}
throw propagate(t);
}
}

public static final class RepositoryOnError implements Fallback<Object> {
public Object createOrPropagate(Throwable t) throws Exception {
if (checkNotNull(t, "throwable") != null) {
Expand Down Expand Up @@ -105,6 +115,10 @@ public static List<Error> getErrors(String output) {
return errors;
}

public static Tag createTagFromErrors(List<Error> errors) {
return Tag.create(null, null, null, null, null, null, errors);
}

public static Repository createRepositoryFromErrors(List<Error> errors) {
return Repository.create(null, -1, null, null, null, null, false, null, false, null, errors);
}
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/com/cdancy/bitbucket/rest/features/TagApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.domain.tags.Tag;
import com.cdancy.bitbucket.rest.fallbacks.BitbucketFallbacks;
import com.cdancy.bitbucket.rest.filters.BitbucketAuthentication;
import com.cdancy.bitbucket.rest.options.CreateTag;
import org.jclouds.Fallbacks;
import org.jclouds.rest.annotations.BinderParam;
import org.jclouds.rest.annotations.Fallback;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.binders.BindToJsonPayload;

import javax.inject.Named;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Produces(MediaType.APPLICATION_JSON)
@RequestFilters(BitbucketAuthentication.class)
@Path("/rest/api/{jclouds.api-version}/projects")
public interface TagApi {

@Named("tag:create")
@Consumes(MediaType.APPLICATION_JSON)
@Path("/{project}/repos/{repo}/tags")
@Fallback(BitbucketFallbacks.TagOnError.class)
@POST
Tag create(@PathParam("project") String project, @PathParam("repo") String repo, @BinderParam(BindToJsonPayload.class) CreateTag createTag);

@Named("tag:get")
@Consumes(MediaType.APPLICATION_JSON)
@Path("/{project}/repos/{repo}/tags/{tag}")
@Fallback(Fallbacks.NullOnNotFoundOr404.class)
@GET
Tag get(@PathParam("project") String project, @PathParam("repo") String repo, @PathParam("tag") String tag);
}
42 changes: 42 additions & 0 deletions src/main/java/com/cdancy/bitbucket/rest/options/CreateTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.options;

import com.google.auto.value.AutoValue;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.json.SerializedNames;

@AutoValue
public abstract class CreateTag {

public abstract String name();

public abstract String startPoint();

// defaults to value of name if null
@Nullable
public abstract String message();

CreateTag() {
}

@SerializedNames({ "name", "startPoint", "message" })
public static CreateTag create(String name, String startPoint, String message) {
return new AutoValue_CreateTag(name, startPoint, message != null ? message : name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.BaseBitbucketApiLiveTest;

import com.cdancy.bitbucket.rest.domain.tags.Tag;
import com.cdancy.bitbucket.rest.options.CreateTag;
import org.testng.annotations.Test;

import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

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

/*
String projectKey = randomStringLettersOnly();
String repoKey = randomStringLettersOnly();
String tagName = randomStringLettersOnly();
String commitHash = "";
*/

String projectKey = "TEST";
String repoKey = "dev";
String tagName = randomStringLettersOnly();
String commitHash = "d90ca08fa076e2e4c076592fce3832aba80a494f";

@Test
public void testCreateTag() {
CreateTag createTag = CreateTag.create(tagName, commitHash, null);
Tag tag = api().create(projectKey, repoKey, createTag);
assertNotNull(tag);
assertTrue(tag.errors().size() == 0);
assertTrue(tag.id().endsWith(tagName));
assertTrue(tag.latestCommit().equalsIgnoreCase(commitHash));
}

@Test (dependsOnMethods = "testCreateTag")
public void testGetTag() {
Tag tag = api().get(projectKey, repoKey, tagName);
assertNotNull(tag);
assertTrue(tag.errors().size() == 0);
assertTrue(tag.id().endsWith(tagName));
assertTrue(tag.latestCommit().equalsIgnoreCase(commitHash));
}

@Test
public void testGetTagNonExistent() {
Tag tag = api().get(projectKey, repoKey, tagName + "9999");
assertNull(tag);
}

private TagApi api() { return api.tagApi(); }
}
108 changes: 108 additions & 0 deletions src/test/java/com/cdancy/bitbucket/rest/features/TagApiMockTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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.BitbucketApiMetadata;
import com.cdancy.bitbucket.rest.domain.repository.Repository;
import com.cdancy.bitbucket.rest.domain.tags.Tag;
import com.cdancy.bitbucket.rest.internal.BaseBitbucketMockTest;
import com.cdancy.bitbucket.rest.options.CreateRepository;
import com.cdancy.bitbucket.rest.options.CreateTag;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import org.testng.annotations.Test;

import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

/**
* Mock tests for the {@link TagApi} class.
*/
@Test(groups = "unit", testName = "TagApiMockTest")
public class TagApiMockTest extends BaseBitbucketMockTest {

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

server.enqueue(new MockResponse().setBody(payloadFromResource("/tag.json")).setResponseCode(200));
BitbucketApi baseApi = api(server.getUrl("/"));
TagApi api = baseApi.tagApi();
try {
String projectKey = "PRJ";
String repoKey = "myrepo";
String tagName = "release-2.0.0";
String commitHash = "8d351a10fb428c0c1239530256e21cf24f136e73";

CreateTag createTag = CreateTag.create(tagName, commitHash, null);
Tag tag = api.create(projectKey, repoKey, createTag);
assertNotNull(tag);
assertTrue(tag.errors().size() == 0);
assertTrue(tag.id().endsWith(tagName));
assertTrue(tag.latestCommit().equalsIgnoreCase(commitHash));
assertSent(server, "POST", "/rest/api/" + BitbucketApiMetadata.API_VERSION + "/projects/" + projectKey + "/repos/" + repoKey + "/tags");
} finally {
baseApi.close();
server.shutdown();
}
}

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

server.enqueue(new MockResponse().setBody(payloadFromResource("/tag.json")).setResponseCode(200));
BitbucketApi baseApi = api(server.getUrl("/"));
TagApi api = baseApi.tagApi();
try {
String projectKey = "PRJ";
String repoKey = "myrepo";
String tagName = "release-2.0.0";
String commitHash = "8d351a10fb428c0c1239530256e21cf24f136e73";

Tag tag = api.get(projectKey, repoKey, tagName);
assertNotNull(tag);
assertTrue(tag.errors().size() == 0);
assertTrue(tag.id().endsWith(tagName));
assertTrue(tag.latestCommit().equalsIgnoreCase(commitHash));
assertSent(server, "GET", "/rest/api/" + BitbucketApiMetadata.API_VERSION + "/projects/" + projectKey + "/repos/" + repoKey + "/tags/" + tagName);
} finally {
baseApi.close();
server.shutdown();
}
}

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

server.enqueue(new MockResponse().setResponseCode(404));
BitbucketApi baseApi = api(server.getUrl("/"));
TagApi api = baseApi.tagApi();
try {
String projectKey = "PRJ";
String repoKey = "myrepo";
String tagName = "non-existent-tag";

Tag tag = api.get(projectKey, repoKey, tagName);
assertNull(tag);
assertSent(server, "GET", "/rest/api/" + BitbucketApiMetadata.API_VERSION + "/projects/" + projectKey + "/repos/" + repoKey + "/tags/" + tagName);
} finally {
baseApi.close();
server.shutdown();
}
}
}
Loading

0 comments on commit 055397b

Please sign in to comment.