ProjectConverter { public ProjectConfigImpl toWorkspaceProject(Project devProject) throws DevfileException { String clonePath = devProject.getClonePath(); if (clonePath == null || clonePath.isEmpty()) { clonePath = "/" + devProject.getName(); } else { clonePath = "/" + sanitizeClonePath(clonePath); } ProjectConfigImpl projectConfig = new ProjectConfigImpl(); projectConfig.setName(devProject.getName()); projectConfig.setPath(clonePath); projectConfig.setSource(toSourceStorage(devProject.getSource())); return projectConfig; }  ProjectImpl toDevfileProject(ProjectConfigImpl projectConfig); ProjectConfigImpl toWorkspaceProject(Project devProject);  }
@Test public void testConvertingDevfileProjectToProjectConfig() throws Exception { ProjectImpl devfileProject = new ProjectImpl( "myProject", new SourceImpl( "git", "https: null); ProjectConfigImpl workspaceProject = projectConverter.toWorkspaceProject(devfileProject); assertEquals(workspaceProject.getName(), "myProject"); assertEquals(workspaceProject.getPath(), "/myProject"); SourceStorageImpl source = workspaceProject.getSource(); assertEquals(source.getType(), "git"); assertEquals(source.getLocation(), "https: assertEquals(source.getParameters().get(BRANCH_PARAMETER_NAME), "master"); assertEquals(source.getParameters().get(START_POINT_PARAMETER_NAME), "3434d"); assertEquals(source.getParameters().get("keepDir"), "core"); }
@Test public void testClonePathSetWhenConvertingDevfileToProjectConfig() throws Exception { ProjectImpl devfileProject = new ProjectImpl( "myProject", new SourceImpl( "git", "https: "down/the/rabbit/hole/myProject"); ProjectConfigImpl workspaceProject = projectConverter.toWorkspaceProject(devfileProject); assertEquals(workspaceProject.getName(), "myProject"); assertEquals(workspaceProject.getPath(), "/down/the/rabbit/hole/myProject"); SourceStorageImpl source = workspaceProject.getSource(); assertEquals(source.getType(), "git"); assertEquals(source.getLocation(), "https: }
@Test(expectedExceptions = DevfileException.class) public void testClonePathCannotEscapeProjectsRoot() throws Exception { ProjectImpl devfileProject = new ProjectImpl( "myProject", new SourceImpl( "git", "https: "cant/hack/../../../usr/bin"); projectConverter.toWorkspaceProject(devfileProject); }
@Test(expectedExceptions = DevfileException.class) public void testClonePathCannotBeAbsolute() throws Exception { ProjectImpl devfileProject = new ProjectImpl( "myProject", new SourceImpl( "git", "https: "/usr/bin"); projectConverter.toWorkspaceProject(devfileProject); }
@Test public void testUpDirOkInClonePathAsLongAsItDoesntEscapeProjectsRoot() throws Exception { ProjectImpl devfileProject = new ProjectImpl( "myProject", new SourceImpl( "git", "https: "cant/hack/../../usr/bin"); ProjectConfigImpl workspaceProject = projectConverter.toWorkspaceProject(devfileProject); assertEquals(workspaceProject.getPath(), "/usr/bin"); }
@Test(expectedExceptions = DevfileException.class) public void testCloningIntoProjectsRootFails() throws Exception { ProjectImpl devfileProject = new ProjectImpl( "myProject", new SourceImpl( "git", "https: "not/../in/root/../.."); projectConverter.toWorkspaceProject(devfileProject); }
@Test( expectedExceptions = DevfileException.class, expectedExceptionsMessageRegExp = "Only one of '" + START_POINT_PARAMETER_NAME + "', '" + TAG_PARAMETER_NAME + "', '" + COMMIT_ID_PARAMETER_NAME + "' can be specified\\.", dataProvider = "invalidStartPointOrTagOrCommitIdCombinations") public void testOnlyOneOfStartPointAttributesAllowed( String startPoint, String tag, String commitId) throws Exception { ProjectImpl devfileProject = new ProjectImpl( "myProject", new SourceImpl( "git", "https: null); projectConverter.toWorkspaceProject(devfileProject); }
@Test public void testUndefinedCloneParametersNotTransferredToWorkspaceConfig() throws Exception { ProjectImpl devfileProject = new ProjectImpl( "myProject", new SourceImpl( "git", "https: null); ProjectConfigImpl wsProject = projectConverter.toWorkspaceProject(devfileProject); SourceStorageImpl wsSource = wsProject.getSource(); assertFalse(wsSource.getParameters().containsKey(BRANCH_PARAMETER_NAME)); assertFalse(wsSource.getParameters().containsKey(START_POINT_PARAMETER_NAME)); assertFalse(wsSource.getParameters().containsKey(TAG_PARAMETER_NAME)); assertFalse(wsSource.getParameters().containsKey(COMMIT_ID_PARAMETER_NAME)); assertFalse(wsSource.getParameters().containsKey(SPARSE_CHECKOUT_DIR_PARAMETER_NAME)); }