|
| 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 static org.junit.Assert.assertNotNull; |
| 27 | +import static org.junit.Assert.assertNull; |
| 28 | +import static org.junit.Assert.assertTrue; |
| 29 | +import static org.junit.Assume.assumeNotNull; |
| 30 | + |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +import org.gitlab4j.api.Constants.ApplicationScope; |
| 35 | +import org.gitlab4j.api.models.Application; |
| 36 | +import org.junit.AfterClass; |
| 37 | +import org.junit.Before; |
| 38 | +import org.junit.BeforeClass; |
| 39 | +import org.junit.Test; |
| 40 | +import org.junit.experimental.categories.Category; |
| 41 | + |
| 42 | +/** |
| 43 | + * In order for these tests to run you must set the following properties in test-gitlab4j.properties |
| 44 | + * <p> |
| 45 | + * TEST_HOST_URL |
| 46 | + * TEST_PRIVATE_TOKEN |
| 47 | + * <p> |
| 48 | + * If any of the above are NULL, all tests in this class will be skipped. |
| 49 | + */ |
| 50 | +@Category(IntegrationTest.class) |
| 51 | +public class TestApplications extends AbstractIntegrationTest { |
| 52 | + |
| 53 | + private static final String TEST_APPLICATION_NAME = "Test Application for GitLab4J-API"; |
| 54 | + private static final String TEST_APPLICATION_REDIRECT = "http://example.com/application"; |
| 55 | + private static final List<ApplicationScope> TEST_APPLICATION_SCOPES = |
| 56 | + Arrays.asList(ApplicationScope.SUDO, ApplicationScope.EMAIL); |
| 57 | + |
| 58 | + private static GitLabApi gitLabApi; |
| 59 | + |
| 60 | + public TestApplications() { |
| 61 | + super(); |
| 62 | + } |
| 63 | + |
| 64 | + @BeforeClass |
| 65 | + public static void setup() { |
| 66 | + // Must setup the connection to the GitLab test server |
| 67 | + gitLabApi = baseTestSetup(); |
| 68 | + |
| 69 | + if (gitLabApi != null) { |
| 70 | + try { |
| 71 | + List<Application> apps = gitLabApi.getApplicationsApi().getApplications(); |
| 72 | + for (Application app : apps) { |
| 73 | + if (TEST_APPLICATION_NAME.equals(app.getApplicationName())) { |
| 74 | + gitLabApi.getApplicationsApi().deleteApplication(app.getId()); |
| 75 | + } |
| 76 | + } |
| 77 | + } catch (Exception ignore) {} |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @AfterClass |
| 82 | + public static void teardown() throws GitLabApiException { |
| 83 | + } |
| 84 | + |
| 85 | + @Before |
| 86 | + public void beforeMethod() { |
| 87 | + assumeNotNull(gitLabApi); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testGetApplications() throws GitLabApiException { |
| 92 | + List<Application> apps = gitLabApi.getApplicationsApi().getApplications(); |
| 93 | + assertNotNull(apps); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testAddAndDeleteApplication() throws GitLabApiException { |
| 98 | + |
| 99 | + List<Application> apps = gitLabApi.getApplicationsApi().getApplications(); |
| 100 | + int appCount = apps.size(); |
| 101 | + |
| 102 | + Application app = gitLabApi.getApplicationsApi().createApplication(TEST_APPLICATION_NAME, TEST_APPLICATION_REDIRECT, TEST_APPLICATION_SCOPES); |
| 103 | + assertNotNull(app); |
| 104 | + |
| 105 | + apps = gitLabApi.getApplicationsApi().getApplications(); |
| 106 | + assertTrue(apps.size() == appCount + 1); |
| 107 | + |
| 108 | + Application found = apps.stream().filter(a -> TEST_APPLICATION_NAME.equals(a.getApplicationName())).findAny().orElse(null); |
| 109 | + assertNotNull(found); |
| 110 | + |
| 111 | + gitLabApi.getApplicationsApi().deleteApplication(app.getId()); |
| 112 | + apps = gitLabApi.getApplicationsApi().getApplications(); |
| 113 | + assertTrue(apps.size() == appCount); |
| 114 | + found = apps.stream().filter(a -> TEST_APPLICATION_NAME.equals(a.getApplicationName())).findAny().orElse(null); |
| 115 | + assertNull(found); |
| 116 | + } |
| 117 | +} |
0 commit comments