Skip to content

Commit

Permalink
AF-2847: Adding template to the REST API /spaces/{spaceName}/project …
Browse files Browse the repository at this point in the history
…command in order to create new project based on the archetype template (kiegroup#1140)

* AF-2847: Adding template to the REST API /spaces/{spaceName}/project command in order to create new project based on the archetype template

* AF-2847: Adding template to the REST API /spaces/{spaceName}/project command in order to create new project based on the archetype template

 * Added Fallback implementation for Archetype Service
 * Added Template Id support for creating project by Rest API job request
  • Loading branch information
akumar074 committed Jun 22, 2021
1 parent e6ebafe commit db5128f
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 5 deletions.
Expand Up @@ -12,7 +12,7 @@
import java.util.concurrent.ConcurrentHashMap;

@RunWith(MockitoJUnitRunner.class)
public class LuceneInedexManagerTest {
public class LuceneIndexManagerTest {

@Mock
LuceneIndexFactory factory;
Expand Down
@@ -0,0 +1,33 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.guvnor.common.services.project.service;

import java.util.Optional;

import org.guvnor.structure.repositories.Repository;

public interface BaseArchetypeService {

/**
* Return the repository where the archetype is stored.
*
* @param alias archetype alias
* @param spaceName archetype space
* @return repository of the archetype
*/
Repository getTemplateRepository(String alias, String spaceName);
}
@@ -0,0 +1,36 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.guvnor.common.services.project.backend.server;

import java.util.Optional;

import org.guvnor.common.services.project.service.BaseArchetypeService;
import org.guvnor.structure.repositories.Repository;
import org.uberfire.annotations.FallbackImplementation;

@FallbackImplementation
public class BaseArchetypeServiceImpl implements BaseArchetypeService {

public BaseArchetypeServiceImpl() {
}

@Override
public Repository getTemplateRepository(String alias, String spaceName) {
return null;
}
}
@@ -0,0 +1,42 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.guvnor.common.services.project.backend.server.utils;

import javax.enterprise.inject.Instance;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;

import org.guvnor.common.services.project.backend.server.BaseArchetypeServiceImpl;
import org.guvnor.common.services.project.service.BaseArchetypeService;
import org.uberfire.annotations.Customizable;

public class BaseArchetypeServiceProducer {

@Inject
private Instance<BaseArchetypeService> archetypeService;

@Produces
@Customizable
public BaseArchetypeService baseArchetypeServiceProducer() {
if (this.archetypeService.isUnsatisfied()) {
return new BaseArchetypeServiceImpl();
}
return this.archetypeService.get();
}

}
Expand Up @@ -39,6 +39,8 @@
import org.guvnor.common.services.project.model.Module;
import org.guvnor.common.services.project.model.POM;
import org.guvnor.common.services.project.model.WorkspaceProject;
import org.guvnor.common.services.project.service.BaseArchetypeService;
import org.guvnor.common.services.project.service.DeploymentMode;
import org.guvnor.common.services.project.service.GAVAlreadyExistsException;
import org.guvnor.common.services.project.service.WorkspaceProjectService;
import org.guvnor.common.services.shared.test.TestResultMessage;
Expand All @@ -51,12 +53,14 @@
import org.guvnor.structure.organizationalunit.OrganizationalUnit;
import org.guvnor.structure.organizationalunit.OrganizationalUnitService;
import org.guvnor.structure.organizationalunit.impl.OrganizationalUnitImpl;
import org.guvnor.structure.repositories.Repository;
import org.guvnor.structure.repositories.RepositoryEnvironmentConfigurations;
import org.guvnor.structure.repositories.RepositoryService;
import org.guvnor.structure.repositories.impl.git.GitRepository;
import org.kie.soup.commons.validation.PortablePreconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.uberfire.annotations.Customizable;
import org.uberfire.backend.server.util.Paths;
import org.uberfire.io.IOService;
import org.uberfire.java.nio.file.FileAlreadyExistsException;
Expand Down Expand Up @@ -93,6 +97,10 @@ public class JobRequestHelper {
@Inject
private TestRunnerService testService;

@Inject
@Customizable
private BaseArchetypeService archetypeService;

public JobResult cloneProject(final String jobId,
final String spaceName,
final CloneProjectRequest cloneProjectRequest) {
Expand Down Expand Up @@ -149,6 +157,22 @@ public JobResult createProject(final String jobId,
String projectGroupId,
String projectVersion,
String projectDescription) {
return createProject(jobId,
spaceName,
projectName,
projectGroupId,
projectVersion,
projectDescription,
null);
}

public JobResult createProject(final String jobId,
final String spaceName,
final String projectName,
String projectGroupId,
String projectVersion,
String projectDescription,
String templateId) {

final JobResult result = new JobResult();
result.setJobId(jobId);
Expand All @@ -172,11 +196,20 @@ public JobResult createProject(final String jobId,
pom.getGav().setVersion(projectVersion);
pom.setDescription(projectDescription);
pom.setName(projectName);

WorkspaceProject project = null;
try {
project = workspaceProjectService.newProject(organizationalUnit,
pom);
if (templateId == null) {
project = workspaceProjectService.newProject(organizationalUnit,
pom);
} else {
project = workspaceProjectService.newProject(organizationalUnit,
pom,
DeploymentMode.VALIDATED,
null,
getTemplateRepository(templateId,
organizationalUnit.getName()));
}
} catch (GAVAlreadyExistsException gae) {
result.setStatus(JobStatus.DUPLICATE_RESOURCE);
result.setResult("Project's GAV [" + gae.getGAV().toString() + "] already exists at [" + toString(gae.getRepositories()) + "]");
Expand All @@ -185,6 +218,10 @@ public JobResult createProject(final String jobId,
result.setStatus(JobStatus.DUPLICATE_RESOURCE);
result.setResult("Project [" + projectName + "] already exists");
return result;
} catch (IllegalArgumentException iae) {
result.setStatus(JobStatus.FAIL);
result.setResult(iae.getMessage());
return result;
}

if (project == null) {
Expand All @@ -198,6 +235,11 @@ public JobResult createProject(final String jobId,
return result;
}

private Repository getTemplateRepository(final String templateId,
final String spaceName) {
return archetypeService.getTemplateRepository(templateId, spaceName);
}

private String toString(final Set<MavenRepositoryMetadata> repositories) {
final StringBuilder sb = new StringBuilder();
for (MavenRepositoryMetadata md : repositories) {
Expand Down
Expand Up @@ -112,6 +112,8 @@ public void createProjectRequest(final CreateProjectJobRequest jobRequest,
jobRequest.getSpaceName());
params.put("Project",
jobRequest.getProjectName());
params.put("TemplateId",
jobRequest.getTemplateId());
params.put("Operation",
"createProject");
params.put(ACCEPT_LANGUAGE,
Expand Down
Expand Up @@ -221,6 +221,7 @@ public Response createProject(
jobRequest.setProjectGroupId(createProjectRequest.getGroupId());
jobRequest.setProjectVersion(createProjectRequest.getVersion());
jobRequest.setDescription(createProjectRequest.getDescription());
jobRequest.setTemplateId(createProjectRequest.getTemplateId());
addAcceptedJobResult(id);

jobRequestObserver.createProjectRequest(jobRequest,
Expand Down
Expand Up @@ -49,7 +49,8 @@ public JobResult internalExecute(final JobRequest request) throws Exception {
jobRequest.getProjectName(),
jobRequest.getProjectGroupId(),
jobRequest.getProjectVersion(),
jobRequest.getDescription());
jobRequest.getDescription(),
jobRequest.getTemplateId());
} finally {
JobStatus status = result != null ? result.getStatus() : JobStatus.SERVER_ERROR;
String groupId = jobRequest.getProjectGroupId() == null ? jobRequest.getProjectName() : jobRequest.getProjectGroupId();
Expand Down
Expand Up @@ -25,6 +25,8 @@
import org.guvnor.common.services.project.model.MavenRepositorySource;
import org.guvnor.common.services.project.model.POM;
import org.guvnor.common.services.project.model.WorkspaceProject;
import org.guvnor.common.services.project.service.BaseArchetypeService;
import org.guvnor.common.services.project.service.DeploymentMode;
import org.guvnor.common.services.project.service.GAVAlreadyExistsException;
import org.guvnor.common.services.project.service.WorkspaceProjectService;
import org.guvnor.rest.client.JobResult;
Expand Down Expand Up @@ -64,6 +66,9 @@ public class JobRequestHelperCreateModuleTest {
@Mock
private OrganizationalUnitService organizationalUnitService;

@Mock
private BaseArchetypeService archetypeService;

@InjectMocks
private JobRequestHelper jobRequestHelper = new JobRequestHelper();

Expand Down Expand Up @@ -185,6 +190,58 @@ public void testNewProjectWhenFileAlreadyExists() throws Exception {
jobResult.getResult());
}

@Test
public void testProjectWithTemplates() {

when(workspaceProjectService.newProject(any(OrganizationalUnit.class),
any(POM.class),
eq(DeploymentMode.VALIDATED),
eq(null),
any(Repository.class))).thenReturn(mock(WorkspaceProject.class));
when(archetypeService.getTemplateRepository(any(),
any())).thenReturn(mock(Repository.class));

final JobResult jobResult = jobRequestHelper.createProject("jobId",
"myOrganizationalUnit",
"myProject",
"projectGroupId",
"projectVersion",
"projectDescription",
"org.kie.kie.drools.archetype.7.55.0.SNAPSHOT.TEMPLATE");

assertEquals("jobId",
jobResult.getJobId());
assertEquals(JobStatus.SUCCESS,
jobResult.getStatus());
assertNotNull(jobResult.getResult());
}

@Test
public void testProjectWithTemplatesNotAvailable() {

doThrow(new IllegalArgumentException("Template is not available"))
.when(workspaceProjectService).newProject(any(OrganizationalUnit.class),
any(POM.class),
eq(DeploymentMode.VALIDATED),
eq(null),
any(Repository.class));
when(archetypeService.getTemplateRepository(any(),
any())).thenReturn(mock(Repository.class));
final JobResult jobResult = jobRequestHelper.createProject("jobId",
"myOrganizationalUnit",
"myProject",
"projectGroupId",
"projectVersion",
"projectDescription",
"org.kie.kie.drools.archetype.7.55.0.SNAPSHOT.TEMPLATE");
assertEquals("jobId",
jobResult.getJobId());
assertEquals(JobStatus.FAIL,
jobResult.getStatus());
assertEquals(jobResult.getResult(),
"Template is not available");
}

@Test
public void testWeAreUsingCorrectGAV() throws Exception {

Expand Down
Expand Up @@ -25,6 +25,7 @@ public class CreateProjectJobRequest extends JobRequest {
private String projectGroupId;
private String projectVersion;
private String description;
private String templateId;

public String getSpaceName() {
return spaceName;
Expand Down Expand Up @@ -65,4 +66,12 @@ public String getDescription() {
public void setDescription(String description) {
this.description = description;
}

public String getTemplateId() {
return templateId;
}

public void setTemplateId(String templateId) {
this.templateId = templateId;
}
}
Expand Up @@ -23,6 +23,7 @@ public class CreateProjectRequest extends Entity {

private String groupId;
private String version;
private String templateId;

public CreateProjectRequest() {
}
Expand All @@ -42,4 +43,12 @@ public String getGroupId() {
public String getVersion() {
return version;
}

public String getTemplateId() {
return templateId;
}

public void setTemplateId(String templateId) {
this.templateId = templateId;
}
}

0 comments on commit db5128f

Please sign in to comment.