|
| 1 | +/* |
| 2 | + * Copyright (c) 2012-2025 Red Hat, Inc. |
| 3 | + * This program and the accompanying materials are made |
| 4 | + * available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Red Hat, Inc. - initial API and implementation |
| 11 | + */ |
| 12 | +package org.eclipse.che.api.factory.server.git.ssh; |
| 13 | + |
| 14 | +import static java.util.Collections.singletonMap; |
| 15 | +import static org.eclipse.che.api.factory.shared.Constants.CURRENT_VERSION; |
| 16 | +import static org.eclipse.che.api.factory.shared.Constants.URL_PARAMETER_NAME; |
| 17 | +import static org.eclipse.che.dto.server.DtoFactory.newDto; |
| 18 | +import static org.mockito.ArgumentMatchers.any; |
| 19 | +import static org.mockito.ArgumentMatchers.eq; |
| 20 | +import static org.mockito.Mockito.when; |
| 21 | +import static org.testng.Assert.assertEquals; |
| 22 | +import static org.testng.Assert.assertFalse; |
| 23 | +import static org.testng.Assert.assertTrue; |
| 24 | + |
| 25 | +import com.google.common.collect.ImmutableMap; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Optional; |
| 29 | +import org.eclipse.che.api.core.ApiException; |
| 30 | +import org.eclipse.che.api.factory.server.scm.AuthorisationRequestManager; |
| 31 | +import org.eclipse.che.api.factory.server.scm.PersonalAccessTokenManager; |
| 32 | +import org.eclipse.che.api.factory.server.urlfactory.DevfileFilenamesProvider; |
| 33 | +import org.eclipse.che.api.factory.server.urlfactory.URLFactoryBuilder; |
| 34 | +import org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto; |
| 35 | +import org.eclipse.che.api.factory.shared.dto.ScmInfoDto; |
| 36 | +import org.eclipse.che.api.workspace.server.devfile.FileContentProvider; |
| 37 | +import org.eclipse.che.api.workspace.server.devfile.URLFetcher; |
| 38 | +import org.mockito.Mock; |
| 39 | +import org.mockito.testng.MockitoTestNGListener; |
| 40 | +import org.testng.annotations.BeforeMethod; |
| 41 | +import org.testng.annotations.Listeners; |
| 42 | +import org.testng.annotations.Test; |
| 43 | + |
| 44 | +@Listeners(MockitoTestNGListener.class) |
| 45 | +public class GitSshFactoryParametersResolverTest { |
| 46 | + |
| 47 | + @Mock private DevfileFilenamesProvider devfileFilenamesProvider; |
| 48 | + @Mock private URLFetcher urlFetcher; |
| 49 | + @Mock private URLFactoryBuilder urlFactoryBuilder; |
| 50 | + @Mock private PersonalAccessTokenManager personalAccessTokenManager; |
| 51 | + @Mock private AuthorisationRequestManager authorisationRequestManager; |
| 52 | + @Mock private GitSshURLParser gitSshURLParser; |
| 53 | + @Mock private GitSshUrl gitSshUrl; |
| 54 | + private GitSshFactoryParametersResolver gitSshFactoryParametersResolver; |
| 55 | + |
| 56 | + @BeforeMethod |
| 57 | + protected void init() { |
| 58 | + gitSshFactoryParametersResolver = |
| 59 | + new GitSshFactoryParametersResolver( |
| 60 | + gitSshURLParser, |
| 61 | + urlFetcher, |
| 62 | + urlFactoryBuilder, |
| 63 | + personalAccessTokenManager, |
| 64 | + authorisationRequestManager); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void ShouldNotAcceptMissingParameter() { |
| 69 | + // given |
| 70 | + Map<String, String> parameters = singletonMap("foo", "this is a foo bar"); |
| 71 | + // when |
| 72 | + boolean accept = gitSshFactoryParametersResolver.accept(parameters); |
| 73 | + // then |
| 74 | + assertFalse(accept); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void ShouldNotAcceptInvalidUrl() { |
| 79 | + // given |
| 80 | + String url = "https://provider.com/user/repo.git"; |
| 81 | + when(gitSshURLParser.isValid(eq(url))).thenReturn(false); |
| 82 | + Map<String, String> parameters = singletonMap(URL_PARAMETER_NAME, url); |
| 83 | + // when |
| 84 | + boolean accept = gitSshFactoryParametersResolver.accept(parameters); |
| 85 | + // then |
| 86 | + assertFalse(accept); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void shouldAcceptValidUrl() { |
| 91 | + // given |
| 92 | + String url = "git@provider.com:user/repo.git"; |
| 93 | + when(gitSshURLParser.isValid(eq(url))).thenReturn(true); |
| 94 | + Map<String, String> parameters = singletonMap(URL_PARAMETER_NAME, url); |
| 95 | + // when |
| 96 | + boolean accept = gitSshFactoryParametersResolver.accept(parameters); |
| 97 | + // then |
| 98 | + assertTrue(accept); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void shouldCreateFactoryWithDevfile() throws Exception { |
| 103 | + // given |
| 104 | + String url = "git@provider.com:user/repo.git"; |
| 105 | + when(gitSshUrl.getProviderName()).thenReturn("git-ssh"); |
| 106 | + when(gitSshUrl.getRepositoryLocation()).thenReturn("repository-location"); |
| 107 | + ImmutableMap<String, String> params = ImmutableMap.of(URL_PARAMETER_NAME, url); |
| 108 | + when(gitSshURLParser.parse(eq(url))).thenReturn(gitSshUrl); |
| 109 | + when(urlFactoryBuilder.createFactoryFromDevfile( |
| 110 | + eq(gitSshUrl), any(FileContentProvider.class), eq(Collections.emptyMap()), eq(true))) |
| 111 | + .thenReturn(Optional.of(generateDevfileV2Factory())); |
| 112 | + // when |
| 113 | + FactoryDevfileV2Dto factory = |
| 114 | + (FactoryDevfileV2Dto) gitSshFactoryParametersResolver.createFactory(params); |
| 115 | + // then |
| 116 | + ScmInfoDto scmInfo = factory.getScmInfo(); |
| 117 | + assertEquals(scmInfo.getScmProviderName(), "git-ssh"); |
| 118 | + assertEquals(scmInfo.getRepositoryUrl(), "repository-location"); |
| 119 | + } |
| 120 | + |
| 121 | + @Test( |
| 122 | + expectedExceptions = ApiException.class, |
| 123 | + expectedExceptionsMessageRegExp = "Failed to fetch devfile") |
| 124 | + public void shouldThrowException() throws Exception { |
| 125 | + // given |
| 126 | + String url = "git@provider.com:user/repo.git"; |
| 127 | + ImmutableMap<String, String> params = ImmutableMap.of(URL_PARAMETER_NAME, url); |
| 128 | + when(gitSshURLParser.parse(eq(url))).thenReturn(gitSshUrl); |
| 129 | + when(urlFactoryBuilder.createFactoryFromDevfile( |
| 130 | + eq(gitSshUrl), any(FileContentProvider.class), eq(Collections.emptyMap()), eq(true))) |
| 131 | + .thenReturn(Optional.empty()); |
| 132 | + // when |
| 133 | + FactoryDevfileV2Dto factory = |
| 134 | + (FactoryDevfileV2Dto) gitSshFactoryParametersResolver.createFactory(params); |
| 135 | + } |
| 136 | + |
| 137 | + private FactoryDevfileV2Dto generateDevfileV2Factory() { |
| 138 | + return newDto(FactoryDevfileV2Dto.class) |
| 139 | + .withV(CURRENT_VERSION) |
| 140 | + .withSource("repo") |
| 141 | + .withDevfile(Map.of("schemaVersion", "2.0.0")); |
| 142 | + } |
| 143 | +} |
0 commit comments