|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2017 Greg Messner <greg@messners.com> |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | + * this software and associated documentation files (the "Software"), to deal in |
| 8 | + * the Software without restriction, including without limitation the rights to |
| 9 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 10 | + * the Software, and to permit persons to whom the Software is furnished to do so, |
| 11 | + * subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 18 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 19 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 20 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +package org.gitlab4j.api; |
| 25 | + |
| 26 | +import org.gitlab4j.api.GitLabApi.ApiVersion; |
| 27 | +import org.gitlab4j.api.models.*; |
| 28 | +import org.glassfish.jersey.internal.guava.Lists; |
| 29 | +import org.junit.*; |
| 30 | +import org.junit.runners.MethodSorters; |
| 31 | + |
| 32 | +import javax.ws.rs.core.Response; |
| 33 | +import java.util.Arrays; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Map; |
| 36 | +import java.util.Optional; |
| 37 | + |
| 38 | +import static org.junit.Assert.*; |
| 39 | +import static org.junit.Assume.assumeTrue; |
| 40 | + |
| 41 | +/** |
| 42 | + * In order for these tests to run you must set the following properties in ~/test-gitlab4j.properties |
| 43 | + * <p> |
| 44 | + * TEST_NAMESPACE |
| 45 | + * TEST_PROJECT_NAME |
| 46 | + * TEST_HOST_URL |
| 47 | + * TEST_PRIVATE_TOKEN |
| 48 | + * TEST_GROUP_PROJECT |
| 49 | + * <p> |
| 50 | + * If any of the above are NULL, all tests in this class will be skipped. |
| 51 | + * <p> |
| 52 | + * NOTE: &FixMethodOrder(MethodSorters.NAME_ASCENDING) is very important to insure that the tests are in the correct order |
| 53 | + */ |
| 54 | +@FixMethodOrder(MethodSorters.JVM) |
| 55 | +public class TestRunnersApi { |
| 56 | + |
| 57 | + // The following needs to be set to your test repository |
| 58 | + private static final String TEST_NAMESPACE; |
| 59 | + private static final String TEST_PROJECT_NAME; |
| 60 | + private static final String TEST_HOST_URL; |
| 61 | + private static final String TEST_PRIVATE_TOKEN; |
| 62 | + private static final String TEST_GROUP; |
| 63 | + private static final String TEST_GROUP_PROJECT; |
| 64 | + |
| 65 | + static { |
| 66 | + TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE"); |
| 67 | + TEST_PROJECT_NAME = TestUtils.getProperty("TEST_PROJECT_NAME"); |
| 68 | + TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL"); |
| 69 | + TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN"); |
| 70 | + TEST_GROUP = TestUtils.getProperty("TEST_GROUP"); |
| 71 | + TEST_GROUP_PROJECT = TestUtils.getProperty("TEST_GROUP_PROJECT"); |
| 72 | + } |
| 73 | + |
| 74 | + private static final String TEST_PROJECT_NAME_1 = "test-gitlab4j-create-project"; |
| 75 | + private static final String TEST_PROJECT_NAME_2 = "test-gitlab4j-create-project-2"; |
| 76 | + private static final String TEST_PROJECT_NAME_UPDATE = "test-gitlab4j-create-project-update"; |
| 77 | + private static GitLabApi gitLabApi; |
| 78 | + |
| 79 | + public TestRunnersApi() { |
| 80 | + super(); |
| 81 | + } |
| 82 | + |
| 83 | + @BeforeClass |
| 84 | + public static void setup() throws GitLabApiException { |
| 85 | + |
| 86 | + String problems = ""; |
| 87 | + if (TEST_NAMESPACE == null || TEST_NAMESPACE.trim().isEmpty()) { |
| 88 | + problems += "TEST_NAMESPACE cannot be empty\n"; |
| 89 | + } |
| 90 | + |
| 91 | + if (TEST_HOST_URL == null || TEST_HOST_URL.trim().isEmpty()) { |
| 92 | + problems += "TEST_HOST_URL cannot be empty\n"; |
| 93 | + } |
| 94 | + |
| 95 | + if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().isEmpty()) { |
| 96 | + problems += "TEST_PRIVATE_TOKEN cannot be empty\n"; |
| 97 | + } |
| 98 | + |
| 99 | + if (problems.isEmpty()) { |
| 100 | + gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN); |
| 101 | + } else { |
| 102 | + System.err.print(problems); |
| 103 | + } |
| 104 | + |
| 105 | + List<Runner> allRunners = gitLabApi.getRunnersApi().getAllRunners(); |
| 106 | + |
| 107 | + if (allRunners.size() > 0) { |
| 108 | + |
| 109 | + for (Runner runner : allRunners) { |
| 110 | + RunnerDetail runnerDetail = gitLabApi.getRunnersApi().getRunnerDetail(runner.getId()); |
| 111 | + gitLabApi.getRunnersApi().deleteRunner(runnerDetail.getToken()); |
| 112 | + } |
| 113 | + |
| 114 | + allRunners = gitLabApi.getRunnersApi().getAllRunners(); |
| 115 | + |
| 116 | + assertEquals(0, allRunners.size()); |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * creates a new runner for a random project |
| 123 | + */ |
| 124 | + private static void createRunner() throws GitLabApiException { |
| 125 | + |
| 126 | + // WHEN |
| 127 | + Project project = gitLabApi.getProjectApi().getProjects().get(0); |
| 128 | + project = gitLabApi.getProjectApi().getProject(project.getId()); |
| 129 | + String runnersToken = project.getRunnersToken(); |
| 130 | + |
| 131 | + // THEN |
| 132 | + gitLabApi.getRunnersApi().registerRunner(runnersToken, |
| 133 | + "Junit registered runner", true, |
| 134 | + Arrays.asList("wow"), false, |
| 135 | + false, null); |
| 136 | + |
| 137 | + // ASSERT |
| 138 | + List<Runner> allRunners = gitLabApi.getRunnersApi().getAllRunners(); |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + @Before |
| 144 | + public void beforeMethod() throws GitLabApiException { |
| 145 | + assumeTrue(gitLabApi != null); |
| 146 | + |
| 147 | + List<Runner> allRunners = gitLabApi.getRunnersApi().getAllRunners(); |
| 148 | + |
| 149 | + for (Runner runner : allRunners) { |
| 150 | + RunnerDetail runnerDetail = gitLabApi.getRunnersApi().getRunnerDetail(runner.getId()); |
| 151 | + gitLabApi.getRunnersApi().deleteRunner(runnerDetail.getToken()); |
| 152 | + } |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void shouldHaveRunnerDetails() throws GitLabApiException { |
| 158 | + |
| 159 | + createRunner(); |
| 160 | + |
| 161 | + List<Runner> runners = gitLabApi.getRunnersApi().getAllRunners(); |
| 162 | + |
| 163 | + assertEquals(1, runners.size()); |
| 164 | + assertNotNull("Description should not be null", runners.get(0).getDescription()); |
| 165 | + |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void shouldDeleteRunner() throws GitLabApiException { |
| 170 | + |
| 171 | + createRunner(); |
| 172 | + createRunner(); |
| 173 | + createRunner(); |
| 174 | + |
| 175 | + List<Runner> allRunners = gitLabApi.getRunnersApi().getAllRunners(); |
| 176 | + assertEquals(3, allRunners.size()); |
| 177 | + |
| 178 | + |
| 179 | + for (Runner runner : allRunners) { |
| 180 | + RunnerDetail runnerDetail = gitLabApi.getRunnersApi().getRunnerDetail(runner.getId()); |
| 181 | + gitLabApi.getRunnersApi().deleteRunner(runnerDetail.getToken()); |
| 182 | + } |
| 183 | + |
| 184 | + allRunners = gitLabApi.getRunnersApi().getAllRunners(); |
| 185 | + assertEquals(0, allRunners.size()); |
| 186 | + |
| 187 | + |
| 188 | + } |
| 189 | + |
| 190 | + |
| 191 | +} |
0 commit comments