Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return devfile v2 as a default devfile #702

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
return devfile v2 as a default devfile
  • Loading branch information
vinokurig committed Jul 17, 2024

Verified

This commit was signed with the committer’s verified signature.
Zorbatron Zorbatron
commit e5175454a90c2978b602560534ed63ccb7a62bdf
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
@@ -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"));
@@ -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"));
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
@@ -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"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we extract it to const?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

.withV(CURRENT_VERSION)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the CURRENT_VERSION stand for ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is Che specific factory API version constant. CURRENT_VERSION = "4.0"

.withSource("repo"))
.acceptVisitor(factoryVisitor);
}

Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
@@ -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));
}
}
Loading