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
Show file tree
Hide file tree
Changes from all commits
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
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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2018 Red Hat, Inc.
* Copyright (c) 2012-2024 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand All @@ -11,6 +11,8 @@
*/
package org.eclipse.che.api.factory.shared;

import java.util.Map;

/**
* Constants for Factory API.
*
Expand All @@ -29,5 +31,7 @@ public final class Constants {
// url factory parameter names
public static final String URL_PARAMETER_NAME = "url";

public static final Map<String, Object> DEFAULT_DEVFILE = Map.of("schemaVersion", "2.3.0");

private Constants() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import static java.util.stream.Collectors.toMap;
import static org.eclipse.che.api.factory.shared.Constants.CURRENT_VERSION;
import static org.eclipse.che.api.factory.shared.Constants.DEFAULT_DEVFILE;
import static org.eclipse.che.dto.server.DtoFactory.newDto;

import java.util.Collections;
Expand All @@ -24,7 +25,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 +61,12 @@ protected FactoryMetaDto createFactory(
contentProvider,
extractOverrideParams(factoryParameters),
getSkipAuthorisation(factoryParameters))
.orElseGet(() -> newDto(FactoryDto.class).withV(CURRENT_VERSION).withSource("repo"))
.orElseGet(
() ->
newDto(FactoryDevfileV2Dto.class)
.withDevfile(DEFAULT_DEVFILE)
.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);
}

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));
}
}
Loading