Skip to content

Commit

Permalink
return devfile v2 as a default devfile
Browse files Browse the repository at this point in the history
  • Loading branch information
vinokurig committed Jul 17, 2024
1 parent 5459a69 commit e517545
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

import com.google.common.collect.ImmutableMap;
Expand All @@ -42,6 +41,7 @@
import org.eclipse.che.api.factory.server.urlfactory.URLFactoryBuilder;
import org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto;
import org.eclipse.che.api.factory.shared.dto.FactoryDto;
import org.eclipse.che.api.factory.shared.dto.ScmInfoDto;
import org.eclipse.che.api.workspace.server.devfile.URLFetcher;
import org.eclipse.che.api.workspace.shared.dto.devfile.DevfileDto;
import org.eclipse.che.api.workspace.shared.dto.devfile.MetadataDto;
Expand Down Expand Up @@ -112,23 +112,17 @@ public void shouldGenerateDevfileForFactoryWithNoDevfileOrJson() throws Exceptio

String bitbucketUrl = "http://bitbucket.2mcl.com/scm/test/repo.git";

FactoryDto computedFactory = generateDevfileFactory();

when(urlFactoryBuilder.buildDefaultDevfile(any())).thenReturn(computedFactory.getDevfile());

when(urlFactoryBuilder.createFactoryFromDevfile(
any(RemoteFactoryUrl.class), any(), anyMap(), anyBoolean()))
.thenReturn(Optional.empty());
Map<String, String> params = ImmutableMap.of(URL_PARAMETER_NAME, bitbucketUrl);
// when
FactoryDto factory =
(FactoryDto) bitbucketServerFactoryParametersResolver.createFactory(params);
FactoryDevfileV2Dto factory =
(FactoryDevfileV2Dto) bitbucketServerFactoryParametersResolver.createFactory(params);
// then
verify(urlFactoryBuilder).buildDefaultDevfile(eq("repo"));
assertEquals(factory, computedFactory);
SourceDto source = factory.getDevfile().getProjects().get(0).getSource();
assertEquals(source.getLocation(), bitbucketUrl);
assertNull(source.getBranch());
ScmInfoDto scmInfo = factory.getScmInfo();
assertEquals(scmInfo.getRepositoryUrl(), bitbucketUrl);
assertEquals(scmInfo.getBranch(), null);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.eclipse.che.api.factory.server.urlfactory.URLFactoryBuilder;
import org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto;
import org.eclipse.che.api.factory.shared.dto.FactoryDto;
import org.eclipse.che.api.factory.shared.dto.ScmInfoDto;
import org.eclipse.che.api.workspace.server.devfile.FileContentProvider;
import org.eclipse.che.api.workspace.server.devfile.URLFetcher;
import org.eclipse.che.api.workspace.shared.dto.devfile.DevfileDto;
Expand Down Expand Up @@ -145,22 +146,17 @@ public void shouldGenerateDevfileForFactoryWithNoDevfile() throws Exception {

String bitbucketUrl = "https://bitbucket.org/eclipse/che";

FactoryDto computedFactory = generateDevfileFactory();

when(urlFactoryBuilder.buildDefaultDevfile(any())).thenReturn(computedFactory.getDevfile());

when(urlFactoryBuilder.createFactoryFromDevfile(
any(RemoteFactoryUrl.class), any(), anyMap(), anyBoolean()))
.thenReturn(Optional.empty());
Map<String, String> params = ImmutableMap.of(URL_PARAMETER_NAME, bitbucketUrl);
// when
FactoryDto factory = (FactoryDto) bitbucketFactoryParametersResolver.createFactory(params);
FactoryDevfileV2Dto factory =
(FactoryDevfileV2Dto) bitbucketFactoryParametersResolver.createFactory(params);
// then
verify(urlFactoryBuilder).buildDefaultDevfile(eq("che"));
assertEquals(factory, computedFactory);
SourceDto source = factory.getDevfile().getProjects().get(0).getSource();
assertEquals(source.getLocation(), bitbucketUrl + ".git");
assertEquals(source.getBranch(), null);
ScmInfoDto scmInfo = factory.getScmInfo();
assertEquals(scmInfo.getRepositoryUrl(), bitbucketUrl + ".git");
assertEquals(scmInfo.getBranch(), null);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.eclipse.che.api.factory.server.urlfactory.URLFactoryBuilder;
import org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto;
import org.eclipse.che.api.factory.shared.dto.FactoryDto;
import org.eclipse.che.api.factory.shared.dto.ScmInfoDto;
import org.eclipse.che.api.workspace.server.devfile.FileContentProvider;
import org.eclipse.che.api.workspace.server.devfile.URLFetcher;
import org.eclipse.che.api.workspace.shared.dto.devfile.DevfileDto;
Expand Down Expand Up @@ -155,34 +156,23 @@ public void shouldGenerateDevfileForFactoryWithNoDevfile() throws Exception {

String githubUrl = "https://github.com/eclipse/che";

FactoryDto computedFactory = generateDevfileFactory();

when(urlFactoryBuilder.buildDefaultDevfile(any())).thenReturn(computedFactory.getDevfile());

when(urlFactoryBuilder.createFactoryFromDevfile(
any(RemoteFactoryUrl.class), any(), anyMap(), anyBoolean()))
.thenReturn(Optional.empty());

when(githubApiClient.isConnected(eq("https://github.com"))).thenReturn(true);
when(githubApiClient.getLatestCommit(anyString(), anyString(), anyString(), any()))
.thenReturn(new GithubCommit().withSha("test-sha"));

Map<String, String> params = ImmutableMap.of(URL_PARAMETER_NAME, githubUrl);
// when
FactoryDto factory = (FactoryDto) abstractGithubFactoryParametersResolver.createFactory(params);
FactoryDevfileV2Dto factory =
(FactoryDevfileV2Dto) abstractGithubFactoryParametersResolver.createFactory(params);
// then
verify(urlFactoryBuilder).buildDefaultDevfile(eq("che"));
assertEquals(factory, computedFactory);
SourceDto source = factory.getDevfile().getProjects().get(0).getSource();
assertEquals(source.getLocation(), githubUrl + ".git");
assertEquals(source.getBranch(), null);
ScmInfoDto scmInfo = factory.getScmInfo();
assertEquals(scmInfo.getRepositoryUrl(), githubUrl + ".git");
assertEquals(scmInfo.getBranch(), null);
}

@Test
public void shouldSkipAuthenticationWhenAccessDenied() throws Exception {
// given
when(urlFactoryBuilder.buildDefaultDevfile(any()))
.thenReturn(generateDevfileFactory().getDevfile());
when(githubApiClient.isConnected(eq("https://github.com"))).thenReturn(true);
when(githubApiClient.getLatestCommit(anyString(), anyString(), anyString(), any()))
.thenReturn(new GithubCommit().withSha("test-sha"));
Expand All @@ -204,8 +194,6 @@ public void shouldSkipAuthenticationWhenAccessDenied() throws Exception {
@Test
public void shouldNotSkipAuthenticationWhenNoErrorParameterPassed() throws Exception {
// given
when(urlFactoryBuilder.buildDefaultDevfile(any()))
.thenReturn(generateDevfileFactory().getDevfile());
when(githubApiClient.isConnected(eq("https://github.com"))).thenReturn(true);
when(githubApiClient.getLatestCommit(anyString(), anyString(), anyString(), any()))
.thenReturn(new GithubCommit().withSha("test-sha"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

import com.google.common.collect.ImmutableMap;
Expand All @@ -42,6 +41,7 @@
import org.eclipse.che.api.factory.server.urlfactory.URLFactoryBuilder;
import org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto;
import org.eclipse.che.api.factory.shared.dto.FactoryDto;
import org.eclipse.che.api.factory.shared.dto.ScmInfoDto;
import org.eclipse.che.api.workspace.server.devfile.URLFetcher;
import org.eclipse.che.api.workspace.shared.dto.devfile.DevfileDto;
import org.eclipse.che.api.workspace.shared.dto.devfile.MetadataDto;
Expand Down Expand Up @@ -114,22 +114,17 @@ public void shouldGenerateDevfileForFactoryWithNoDevfileOrJson() throws Exceptio

String gitlabUrl = "http://gitlab.2mcl.com/test/proj/repo.git";

FactoryDto computedFactory = generateDevfileFactory();

when(urlFactoryBuilder.buildDefaultDevfile(any())).thenReturn(computedFactory.getDevfile());

when(urlFactoryBuilder.createFactoryFromDevfile(
any(RemoteFactoryUrl.class), any(), anyMap(), anyBoolean()))
.thenReturn(Optional.empty());
Map<String, String> params = ImmutableMap.of(URL_PARAMETER_NAME, gitlabUrl);
// when
FactoryDto factory = (FactoryDto) gitlabFactoryParametersResolver.createFactory(params);
FactoryDevfileV2Dto factory =
(FactoryDevfileV2Dto) gitlabFactoryParametersResolver.createFactory(params);
// then
verify(urlFactoryBuilder).buildDefaultDevfile(eq("repo"));
assertEquals(factory, computedFactory);
SourceDto source = factory.getDevfile().getProjects().get(0).getSource();
assertEquals(source.getLocation(), gitlabUrl);
assertNull(source.getBranch());
ScmInfoDto scmInfo = factory.getScmInfo();
assertEquals(scmInfo.getRepositoryUrl(), gitlabUrl);
assertEquals(scmInfo.getBranch(), null);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.eclipse.che.api.factory.server.scm.AuthorisationRequestManager;
import org.eclipse.che.api.factory.server.urlfactory.RemoteFactoryUrl;
import org.eclipse.che.api.factory.server.urlfactory.URLFactoryBuilder;
import org.eclipse.che.api.factory.shared.dto.FactoryDto;
import org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto;
import org.eclipse.che.api.factory.shared.dto.FactoryMetaDto;
import org.eclipse.che.api.factory.shared.dto.FactoryVisitor;
import org.eclipse.che.api.workspace.server.devfile.FileContentProvider;
Expand Down Expand Up @@ -60,7 +60,12 @@ protected FactoryMetaDto createFactory(
contentProvider,
extractOverrideParams(factoryParameters),
getSkipAuthorisation(factoryParameters))
.orElseGet(() -> newDto(FactoryDto.class).withV(CURRENT_VERSION).withSource("repo"))
.orElseGet(
() ->
newDto(FactoryDevfileV2Dto.class)
.withDevfile(Map.of("schemaVersion", "2.3.0"))
.withV(CURRENT_VERSION)
.withSource("repo"))
.acceptVisitor(factoryVisitor);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@
package org.eclipse.che.api.factory.server;

import static java.util.Collections.emptyMap;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

import java.util.Map;
import org.eclipse.che.api.factory.server.scm.AuthorisationRequestManager;
import org.eclipse.che.api.factory.server.urlfactory.RemoteFactoryUrl;
import org.eclipse.che.api.factory.server.urlfactory.URLFactoryBuilder;
import org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto;
import org.eclipse.che.api.factory.shared.dto.FactoryVisitor;
import org.eclipse.che.api.workspace.server.devfile.FileContentProvider;
import org.mockito.Mock;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.annotations.BeforeMethod;
Expand All @@ -31,7 +37,9 @@ public class BaseFactoryParameterResolverTest {

@Mock private AuthorisationRequestManager authorisationRequestManager;
@Mock private URLFactoryBuilder urlFactoryBuilder;

@Mock private RemoteFactoryUrl remoteFactoryUrl;
@Mock private FactoryVisitor factoryVisitor;
@Mock private FileContentProvider contentProvider;
private static final String PROVIDER_NAME = "test";

private BaseFactoryParameterResolver baseFactoryParameterResolver;
Expand Down Expand Up @@ -73,4 +81,15 @@ public void shouldReturnTrueOnGetSkipAuthorisationFromFactoryParams() {
// then
assertTrue(result);
}

@Test
public void shouldReturnDevfileV2() throws Exception {
// given
when(authorisationRequestManager.isStored(eq(PROVIDER_NAME))).thenReturn(false);
// when
baseFactoryParameterResolver.createFactory(
emptyMap(), remoteFactoryUrl, factoryVisitor, contentProvider);
// then
verify(factoryVisitor).visit(any(FactoryDevfileV2Dto.class));
}
}

0 comments on commit e517545

Please sign in to comment.