Skip to content

Commit

Permalink
incubator-kie-issues#1614: Fix UserTasks Codegen issue in windows (#3773
Browse files Browse the repository at this point in the history
)

* incubator-kie-issues#1614: Fix UserTasks Codegen issue in windows

* - fix

* - fix import order

* - fix test imports
  • Loading branch information
pefernan authored Nov 13, 2024
1 parent 34a4323 commit 852d098
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -167,17 +166,17 @@ 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() {
String packageName = context().getPackageName();
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<GeneratedFile> generateUserTask() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.kie.kogito.codegen.usertask;

import java.io.File;
import java.nio.file.Path;
import java.util.List;

Expand Down Expand Up @@ -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());
}

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

0 comments on commit 852d098

Please sign in to comment.