diff --git a/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/usertask/UserTaskCodegen.java b/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/usertask/UserTaskCodegen.java index 7fd6603aa04..81965e0c4e6 100644 --- a/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/usertask/UserTaskCodegen.java +++ b/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/usertask/UserTaskCodegen.java @@ -18,7 +18,6 @@ */ package org.kie.kogito.codegen.usertask; -import java.io.File; import java.io.IOException; import java.io.Reader; import java.nio.file.Path; @@ -167,8 +166,8 @@ public GeneratedFile generateRestEndpiont() { compilationUnit.findAll(MethodDeclaration.class).stream().filter(MethodDeclaration::isPublic).forEach(context().getDependencyInjectionAnnotator()::withTransactional); } String className = compilationUnit.findFirst(ClassOrInterfaceDeclaration.class).get().getNameAsString(); - String urlBase = packageName.replaceAll("\\.", File.separator); - return new GeneratedFile(GeneratedFileType.REST, Path.of(urlBase).resolve(className + ".java"), compilationUnit.toString()); + Path basePath = UserTaskCodegenHelper.path(packageName); + return new GeneratedFile(GeneratedFileType.REST, basePath.resolve(className + ".java"), compilationUnit.toString()); } public GeneratedFile generateProducer() { @@ -176,8 +175,8 @@ public GeneratedFile generateProducer() { CompilationUnit compilationUnit = producerTemplateGenerator.compilationUnitOrThrow("No producer template found for user tasks"); compilationUnit.setPackageDeclaration(packageName); String className = compilationUnit.findFirst(ClassOrInterfaceDeclaration.class).get().getNameAsString(); - String urlBase = packageName.replaceAll("\\.", File.separator); - return new GeneratedFile(GeneratedFileType.SOURCE, Path.of(urlBase).resolve(className + ".java"), compilationUnit.toString()); + Path basePath = UserTaskCodegenHelper.path(packageName); + return new GeneratedFile(GeneratedFileType.SOURCE, basePath.resolve(className + ".java"), compilationUnit.toString()); } public List generateUserTask() { diff --git a/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/usertask/UserTaskCodegenHelper.java b/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/usertask/UserTaskCodegenHelper.java index d61e4b9ab8b..996221f26c8 100644 --- a/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/usertask/UserTaskCodegenHelper.java +++ b/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/usertask/UserTaskCodegenHelper.java @@ -19,8 +19,8 @@ package org.kie.kogito.codegen.usertask; -import java.io.File; import java.nio.file.Path; +import java.util.Arrays; import org.jbpm.process.core.Work; import org.kie.kogito.internal.utils.ConversionUtils; @@ -44,7 +44,17 @@ public static String packageName(Work descriptor) { } public static Path path(Work descriptor) { - return Path.of(((String) descriptor.getParameter("PackageName")).replaceAll("\\.", File.separator)); + return path(packageName(descriptor)); + } + + public static Path path(String packageName) { + String[] pathFragments = packageName.split("\\."); + + if (pathFragments.length == 1) { + return Path.of(pathFragments[0]); + } + String[] children = Arrays.copyOfRange(pathFragments, 1, pathFragments.length); + return Path.of(pathFragments[0], children); } public static String fqnClassName(Work descriptor) { diff --git a/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/usertask/UserTaskConfigGenerator.java b/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/usertask/UserTaskConfigGenerator.java index d6483f92b28..f262ff6c580 100644 --- a/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/usertask/UserTaskConfigGenerator.java +++ b/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/usertask/UserTaskConfigGenerator.java @@ -18,7 +18,6 @@ */ package org.kie.kogito.codegen.usertask; -import java.io.File; import java.nio.file.Path; import java.util.List; @@ -58,8 +57,9 @@ public GeneratedFile generate() { ConstructorDeclaration declaration = clazzDeclaration.findFirst(ConstructorDeclaration.class).get(); declaration.setName(configClassName()); + Path basePath = UserTaskCodegenHelper.path(packageName); - return new GeneratedFile(GeneratedFileType.SOURCE, Path.of(packageName.replaceAll("\\.", File.separator), configClassName() + ".java"), unit.toString()); + return new GeneratedFile(GeneratedFileType.SOURCE, basePath.resolve(configClassName() + ".java"), unit.toString()); } } diff --git a/kogito-codegen-modules/kogito-codegen-processes/src/test/java/org/kie/kogito/codegen/usertask/UserTaskCodegenHelperTest.java b/kogito-codegen-modules/kogito-codegen-processes/src/test/java/org/kie/kogito/codegen/usertask/UserTaskCodegenHelperTest.java new file mode 100644 index 00000000000..1e1beb05710 --- /dev/null +++ b/kogito-codegen-modules/kogito-codegen-processes/src/test/java/org/kie/kogito/codegen/usertask/UserTaskCodegenHelperTest.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.kie.kogito.codegen.usertask; + +import java.nio.file.Path; + +import org.jbpm.process.core.Work; +import org.jbpm.process.core.impl.WorkImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class UserTaskCodegenHelperTest { + static final String PROCESS_ID = "approvals"; + static final String TASK_ID = "taskId"; + static final String PACKAGE = "org.kie.kogito.usertask"; + static final Path PACKAGE_PATH = Path.of("org/kie/kogito/usertask"); + + private Work work; + + @BeforeEach + void setUp() { + work = new WorkImpl(); + work.setParameter("ProcessId", PROCESS_ID); + work.setParameter(Work.PARAMETER_UNIQUE_TASK_ID, TASK_ID); + work.setParameter("PackageName", PACKAGE); + } + + @Test + void testGetWorkProcessId() { + assertThat(UserTaskCodegenHelper.processId(work)).isEqualTo("Approvals"); + } + + @Test + void testGetWorkClassName() { + assertThat(UserTaskCodegenHelper.className(work)).isEqualTo("Approvals_TaskId"); + } + + @Test + void testGetWorkPackagePath() { + assertThat(UserTaskCodegenHelper.path(work)) + .isNotNull() + .isEqualTo(PACKAGE_PATH); + + } + + @Test + void testGetPath() { + assertThat(UserTaskCodegenHelper.path(PACKAGE)) + .isNotNull() + .isEqualTo(PACKAGE_PATH); + + assertThat(UserTaskCodegenHelper.path("test")) + .isNotNull() + .isEqualTo(Path.of("test")); + } +}