@JsonIgnoreProperties(ignoreUnknown = true)
public class BitBucketChangeset {

    /**
     * A modification on a single file.
     * 
     * @author ericbottard
     * 
     */
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class FileModification {

        @JsonProperty
        private String file;

        @JsonProperty
        private FileModificationType type;

        /**
         * The path of the file (or directory) affected.
         */
        public String getFile() {
            return file;
        }

        /**
         * The kind of modification.
         */
        public FileModificationType getType() {
            return type;
        }
    }

    /**
     * The kind of modification on a file path.
     */
    public static enum FileModificationType {
        modified, added, removed;
    }

    @JsonProperty
    private String author;

    @JsonProperty
    private String branch;

    @JsonProperty
    private List<FileModification> files;

    @JsonProperty
    private String message;

    @JsonProperty
    private String node;

    @JsonProperty
    private List<String> parents;

    @JsonProperty("raw_author")
    private String rawAuthor;

    @JsonProperty("raw_node")
    private String rawNode;

    @JsonProperty
    private long revision;

    /**
     * The username of the {@link BitBucketUser} that made the change.
     */
    public String getAuthor() {
        return author;
    }

    /**
     * The name of the branch where the commit occurred.
     */
    public String getBranch() {
        return branch;
    }

    /**
     * A list of affected files.
     */
    public List<FileModification> getFiles() {
        return files;
    }

    /**
     * The commit message as entered by the {@link #getAuthor() author}.
     */
    public String getMessage() {
        return message;
    }

    /**
     * The short node hash.
     */
    public String getNode() {
        return node;
    }

    /**
     * The parent commit(s) of this change.
     */
    public List<String> getParents() {
        return parents;
    }

    /**
     * The raw author identifier of this change (includes email address).
     */
    public String getRawAuthor() {
        return rawAuthor;
    }

    /**
     * The full node hash.
     */
    public String getRawNode() {
        return rawNode;
    }

    public long getRevision() {
        return revision;
    }

    @Override
    public String toString() {
        return getNode();
    }

} public static enum FileModificationType {
    modified, added, removed;
} @JsonIgnoreProperties(ignoreUnknown = true)
public class BitBucketChangesets {

    @JsonProperty
    private List<BitBucketChangeset> changesets;

    @JsonProperty
    private int limit;

    @JsonProperty
    private String start;

    @JsonProperty
    private int count;

    public List<BitBucketChangeset> getChangesets() {
        return changesets;
    }

    public int getLimit() {
        return limit;
    }

    public String getStart() {
        return start;
    }

    public int getCount() {
        return count;
    }
} @JsonIgnoreProperties(ignoreUnknown = true)
public class BitBucketDirectory {

    @JsonProperty
    private List<String> directories;

    @JsonProperty
    private List<BitBucketFileMetadata> files;

    @JsonProperty
    private String path;

    @JsonProperty
    private String node;

    public List<String> getDirectories() {
        return directories;
    }

    public List<BitBucketFileMetadata> getFiles() {
        return files;
    }

    public String getPath() {
        return path;
    }

    public String getNode() {
        return node;
    }

} @JsonIgnoreProperties(ignoreUnknown = true)
public class BitBucketFile {

    @JsonProperty
    private String node;

    @JsonProperty
    private String path;

    @JsonProperty
    private String data;

    public String getNode() {
        return node;
    }

    /**
     * Returns the file path, relative to the root of the repository.
     */
    public String getPath() {
        return path;
    }

    /**
     * Returns the actual content of the file, as a String.
     */
    public String getData() {
        return data;
    }
} @JsonIgnoreProperties(ignoreUnknown = true)
public class BitBucketFileMetadata {

    @JsonProperty
    private String path;

    @JsonProperty
    private String revision;

    @JsonProperty("utctimestamp")
    @JsonDeserialize(using = UTCDateDeserializer.class)
    private Date timestamp;

    @JsonProperty
    private int size;

    public String getPath() {
        return path;
    }

    public String getRevision() {
        return revision;
    }

    public Date getTimestamp() {
        return timestamp;
    }

    public int getSize() {
        return size;
    }
} @JsonIgnoreProperties(ignoreUnknown = true)
public class BitBucketRepository implements Serializable {

    private static final long serialVersionUID = 1L;

    @JsonProperty("is_private")
    private boolean _private;

    @JsonProperty("utc_created_on")
    @JsonDeserialize(using = UTCDateDeserializer.class)
    private Date createdAt;

    @JsonProperty
    private String description;

    @JsonProperty("has_wiki")
    private boolean hasWiki;

    @JsonProperty("utc_last_updated")
    @JsonDeserialize(using = UTCDateDeserializer.class)
    private Date lastUpdatedOn;

    @JsonProperty
    private String name;

    @JsonProperty
    private String owner;

    @JsonProperty("read_only")
    private boolean readOnly;

    @JsonProperty
    private BitBucketSCM scm;

    @JsonProperty
    private long size;

    @JsonProperty
    private String slug;

    /**
     * The date when the repository was created.
     */
    public Date getCreatedAt() {
        return createdAt;
    }

    /**
     * A description of the repository, as entered by its creator.
     */
    public String getDescription() {
        return description;
    }

    /**
     * When this repository was last updated.
     * 
     */
    public Date getLastUpdatedOn() {
        return lastUpdatedOn;
    }

    /**
     * A user friendly name for this repository (may differ from its
     * {@link #getSlug() slug}).
     * 
     */
    public String getName() {
        return name;
    }

    /**
     * The username of the repository owner.
     */
    public String getOwner() {
        return owner;
    }

    /**
     * The source control management system this repository uses.
     */
    public BitBucketSCM getScm() {
        return scm;
    }

    /**
     * The size of this repository, in bytes.
     */
    public long getSize() {
        return size;
    }

    /**
     * This repository's "slug", <i>ie.</i> its technical id in BitBucket terms.
     */
    public String getSlug() {
        return slug;
    }

    /**
     * Whether this repository has an attached wiki.
     */
    public boolean isHasWiki() {
        return hasWiki;
    }

    /**
     * Whether this repository is private (authenticated user needs access to
     * it) or is for everyone to see.
     */
    public boolean isPrivate() {
        return _private;
    }

    public boolean isReadOnly() {
        return readOnly;
    }

} public enum BitBucketSCM {

    /**
     * Mercurial.
     */
    hg,

    /**
     * Git.
     */
    git;

} @JsonIgnoreProperties(ignoreUnknown = true)
public class BitBucketUser implements Serializable {

    private static final long serialVersionUID = 1L;

    @JsonProperty
    private String username;

    @JsonProperty("first_name")
    private String firstName;

    @JsonProperty("last_name")
    private String lastName;

    @JsonProperty("avatar")
    private String avatarImageUrl;

    public String getUsername() {
        return username;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getAvatarImageUrl() {
        return avatarImageUrl;
    }

} public class RepoCreation extends ParameterMap {

    public RepoCreation(String name) {
        set("name", name);
    }

    /**
     * The name of the repository.
     */
    public String getName() {
        return getFirst("name");
    }

    public void setName(String name) {
        set("name", name);
    }

    /**
     * Boolean specifying if the repository is private (true) or public (false).
     * The default is false.
     */
    public boolean isMakePrivate() {
        return Boolean.valueOf(getFirst("is_private"));
    }

    public void setMakePrivate(boolean makePrivate) {
        set("is_private", Boolean.toString(makePrivate));
    }

    /**
     * A description of the repository.
     */
    public String getDescription() {
        return getFirst("description");
    }

    public void setDescription(String description) {
        set("description", description);
    }

    /**
     * A value of git or hg. The default is git if you leave this parameter
     * unspecified.
     */
    public BitBucketSCM getScm() {
        return BitBucketSCM.valueOf(getFirst("scm"));
    }

    public void setScm(BitBucketSCM scm) {
        set("scm", scm.name());
    }

    /**
     * The language used for source code in the repository.
     */
    public String getLanguage() {
        return getFirst("language");
    }

    public void setLanguage(String language) {
        set("language", language);
    }

}
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.http.HttpMethod.*;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
import static org.springframework.test.web.client.response.MockRestResponseCreators.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.social.bitbucket.api.BitBucketChangeset;
import org.springframework.social.bitbucket.api.BitBucketChangeset.FileModificationType;
import org.springframework.social.bitbucket.api.BitBucketChangesets;
import org.springframework.social.bitbucket.api.BitBucketDirectory;
import org.springframework.social.bitbucket.api.BitBucketFile;
import org.springframework.social.bitbucket.api.BitBucketFileMetadata;
import org.springframework.social.bitbucket.api.BitBucketRepository;
import org.springframework.social.bitbucket.api.BitBucketSCM;
import org.springframework.social.bitbucket.api.BitBucketUser;
import org.springframework.social.bitbucket.api.RepoCreation;
/**
 * Copyright 2012 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.springframework.social.bitbucket.api.impl;




public class RepoTemplateTest extends BaseTemplateTest {

    @Test
    public void testGetOneRepo() {

        mockServer
                .expect(requestTo("https://api.bitbucket.org/1.0/repositories/jespen/django-piston/"))
                .andExpect(method(GET))
                .andRespond(
                        withSuccess(jsonResource("get-one-repo"),
                                MediaType.APPLICATION_JSON));

        BitBucketRepository repo = bitBucket.repoOperations().getRepository(
                "jespen", "django-piston");
        assertEquals(BitBucketSCM.hg, repo.getScm());
        assertEquals("jespern", repo.getOwner());
        assertEquals("django-piston", repo.getName());
        assertEquals("django-piston", repo.getSlug());
        assertEquals("Piston is a Django mini-framework creating APIs.",
                repo.getDescription());

    }

    @Test
    public void testGetUserRepositories() {

        mockServer
                .expect(requestTo("https://api.bitbucket.org/1.0/user/repositories/"))
                .andExpect(method(GET))
                .andRespond(
                        withSuccess(jsonResource("get-user-repositories"),
                                MediaType.APPLICATION_JSON));

        List<BitBucketRepository> repos = bitBucket.repoOperations()
                .getUserRepositories();

        assertEquals(3, repos.size());
        BitBucketRepository first = repos.iterator().next();
        assertEquals("ericbottard", first.getOwner());
        assertEquals(BitBucketSCM.git, first.getScm());
        assertEquals("mahjong", first.getSlug());
        assertTrue(first.isPrivate());
        assertEquals("mahjong", first.getName());
    }

    @Test
    public void testSearchForRepos() {

        mockServer
                .expect(requestTo("https://api.bitbucket.org/1.0/repositories/?name=box2d"))
                .andExpect(method(GET))
                .andRespond(
                        withSuccess(jsonResource("search-repos"),
                                MediaType.APPLICATION_JSON));

        List<BitBucketRepository> results = bitBucket.repoOperations().search(
                "box2d");
        assertEquals(6, results.size());
        BitBucketRepository first = results.get(0);
        assertEquals(BitBucketSCM.hg, first.getScm());
        assertEquals("Box2DXNA", first.getName());
        assertEquals("fuzzybinary", first.getOwner());
        assertEquals("box2dxna", first.getSlug());
        assertEquals(
                "Personal version of Box2DX that is being used in AngelXNA.",
                first.getDescription());

    }

    @Test
    public void testRepoTags() {

        mockServer
                .expect(requestTo("https://api.bitbucket.org/1.0/repositories/tortoisehg/thg/tags/"))
                .andExpect(method(GET))
                .andRespond(
                        withSuccess(jsonResource("get-tags"),
                                MediaType.APPLICATION_JSON));

        Map<String, BitBucketChangeset> tags = bitBucket.repoOperations()
                .getTags("tortoisehg", "thg");
        assertEquals(68, tags.size());

        BitBucketChangeset tag = tags.get("0.9");
        assertEquals("400e85fa0b84", tag.getNode());
        assertEquals("400e85fa0b8442af109d00081e8840359f095a67",
                tag.getRawNode());
        assertEquals("Steve Borho <steve@borho.org>", tag.getRawAuthor());
        assertEquals("sborho", tag.getAuthor());
        assertEquals("stable", tag.getBranch());
        assertEquals("i18n: pull final Ukranian translation for 0.9",
                tag.getMessage());
        assertEquals(4922, tag.getRevision());
        assertEquals(Arrays.asList("e6bec1d7925b"), tag.getParents());
        assertEquals(1, tag.getFiles().size());
        assertEquals("i18n/tortoisehg/uk.po", tag.getFiles().get(0).getFile());
        assertEquals(FileModificationType.modified, tag.getFiles().get(0)