|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package com.cdancy.bitbucket.rest.features; |
| 18 | + |
| 19 | +import com.cdancy.bitbucket.rest.BitbucketApi; |
| 20 | +import com.cdancy.bitbucket.rest.BitbucketApiMetadata; |
| 21 | +import com.cdancy.bitbucket.rest.domain.repository.Repository; |
| 22 | +import com.cdancy.bitbucket.rest.domain.tags.Tag; |
| 23 | +import com.cdancy.bitbucket.rest.internal.BaseBitbucketMockTest; |
| 24 | +import com.cdancy.bitbucket.rest.options.CreateRepository; |
| 25 | +import com.cdancy.bitbucket.rest.options.CreateTag; |
| 26 | +import com.squareup.okhttp.mockwebserver.MockResponse; |
| 27 | +import com.squareup.okhttp.mockwebserver.MockWebServer; |
| 28 | +import org.testng.annotations.Test; |
| 29 | + |
| 30 | +import static org.testng.Assert.assertNull; |
| 31 | +import static org.testng.Assert.assertNotNull; |
| 32 | +import static org.testng.Assert.assertTrue; |
| 33 | + |
| 34 | +/** |
| 35 | + * Mock tests for the {@link TagApi} class. |
| 36 | + */ |
| 37 | +@Test(groups = "unit", testName = "TagApiMockTest") |
| 38 | +public class TagApiMockTest extends BaseBitbucketMockTest { |
| 39 | + |
| 40 | + public void testCreateTag() throws Exception { |
| 41 | + MockWebServer server = mockEtcdJavaWebServer(); |
| 42 | + |
| 43 | + server.enqueue(new MockResponse().setBody(payloadFromResource("/tag.json")).setResponseCode(200)); |
| 44 | + BitbucketApi baseApi = api(server.getUrl("/")); |
| 45 | + TagApi api = baseApi.tagApi(); |
| 46 | + try { |
| 47 | + String projectKey = "PRJ"; |
| 48 | + String repoKey = "myrepo"; |
| 49 | + String tagName = "release-2.0.0"; |
| 50 | + String commitHash = "8d351a10fb428c0c1239530256e21cf24f136e73"; |
| 51 | + |
| 52 | + CreateTag createTag = CreateTag.create(tagName, commitHash, null); |
| 53 | + Tag tag = api.create(projectKey, repoKey, createTag); |
| 54 | + assertNotNull(tag); |
| 55 | + assertTrue(tag.errors().size() == 0); |
| 56 | + assertTrue(tag.id().endsWith(tagName)); |
| 57 | + assertTrue(tag.latestCommit().equalsIgnoreCase(commitHash)); |
| 58 | + assertSent(server, "POST", "/rest/api/" + BitbucketApiMetadata.API_VERSION + "/projects/" + projectKey + "/repos/" + repoKey + "/tags"); |
| 59 | + } finally { |
| 60 | + baseApi.close(); |
| 61 | + server.shutdown(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public void testGetTag() throws Exception { |
| 66 | + MockWebServer server = mockEtcdJavaWebServer(); |
| 67 | + |
| 68 | + server.enqueue(new MockResponse().setBody(payloadFromResource("/tag.json")).setResponseCode(200)); |
| 69 | + BitbucketApi baseApi = api(server.getUrl("/")); |
| 70 | + TagApi api = baseApi.tagApi(); |
| 71 | + try { |
| 72 | + String projectKey = "PRJ"; |
| 73 | + String repoKey = "myrepo"; |
| 74 | + String tagName = "release-2.0.0"; |
| 75 | + String commitHash = "8d351a10fb428c0c1239530256e21cf24f136e73"; |
| 76 | + |
| 77 | + Tag tag = api.get(projectKey, repoKey, tagName); |
| 78 | + assertNotNull(tag); |
| 79 | + assertTrue(tag.errors().size() == 0); |
| 80 | + assertTrue(tag.id().endsWith(tagName)); |
| 81 | + assertTrue(tag.latestCommit().equalsIgnoreCase(commitHash)); |
| 82 | + assertSent(server, "GET", "/rest/api/" + BitbucketApiMetadata.API_VERSION + "/projects/" + projectKey + "/repos/" + repoKey + "/tags/" + tagName); |
| 83 | + } finally { |
| 84 | + baseApi.close(); |
| 85 | + server.shutdown(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public void testGetTagNonExistent() throws Exception { |
| 90 | + MockWebServer server = mockEtcdJavaWebServer(); |
| 91 | + |
| 92 | + server.enqueue(new MockResponse().setResponseCode(404)); |
| 93 | + BitbucketApi baseApi = api(server.getUrl("/")); |
| 94 | + TagApi api = baseApi.tagApi(); |
| 95 | + try { |
| 96 | + String projectKey = "PRJ"; |
| 97 | + String repoKey = "myrepo"; |
| 98 | + String tagName = "non-existent-tag"; |
| 99 | + |
| 100 | + Tag tag = api.get(projectKey, repoKey, tagName); |
| 101 | + assertNull(tag); |
| 102 | + assertSent(server, "GET", "/rest/api/" + BitbucketApiMetadata.API_VERSION + "/projects/" + projectKey + "/repos/" + repoKey + "/tags/" + tagName); |
| 103 | + } finally { |
| 104 | + baseApi.close(); |
| 105 | + server.shutdown(); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments