Skip to content

Commit

Permalink
Microprofile Config Service Catalog Addon throws exception at address…
Browse files Browse the repository at this point in the history
… resolution (apache#3278)

* kie-issues_680: Microprofile Config Service Catalog Addon throws exception at address resolution

* Code review suggetions 1
  • Loading branch information
wmedvede authored Nov 8, 2023
1 parent 741ed2f commit 6322ddd
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static <T> T convert(Object value, Class<T> clazz) {

/**
* Converts a string into a list of objects using `,` as a separator
*
*
* @param <T>
* @param value object to be converted into list
* @param clazz the item target class
Expand All @@ -55,7 +55,7 @@ public static <T> Collection<T> convertToCollection(Object value, Class<T> clazz

/**
* Converts a string into a list of objects
*
*
* @param <T>
* @param value object to be converted into list
* @param clazz the item target class
Expand All @@ -71,7 +71,7 @@ public static <T> Collection<T> convertToCollection(Object value, Class<T> clazz

/**
* Converts an object to an instance of the provided class
*
*
* @param <T>
* @param value
* @param clazz
Expand All @@ -81,6 +81,8 @@ public static <T> Collection<T> convertToCollection(Object value, Class<T> clazz
public static <T> T convert(Object value, Class<T> clazz, Function<Object, String> stringConverter) {
if (value == null || clazz.isAssignableFrom(value.getClass())) {
return clazz.cast(value);
} else if (isConvertibleFromStringJavaBaseType(clazz)) {
return convertFromStringJavaBaseType(clazz, stringConverter.apply(value));
} else {
Method convert = getConvertMethod(clazz);
if (convert != null) {
Expand Down Expand Up @@ -114,9 +116,48 @@ private static Method getConvertMethod(Class<?> clazz) {
return null;
}

/**
* @return true if the given type corresponds to a java base type that has the method valueOf(String value).
*/
private static boolean isConvertibleFromStringJavaBaseType(Class<?> clazz) {
return clazz == Short.class || clazz == Integer.class || clazz == Long.class || clazz == Byte.class ||
clazz == Float.class || clazz == Double.class ||
clazz == Boolean.class;
}

/**
* @return the value created by applying the static method valueOf(String) on the given class. We use this method on
* these base java types to ensure conversion continues working on code generated by the quarkus native build,
* since it was detected that valueOf is not discovered by reflection on these classes after the native build.
*/
private static <T> T convertFromStringJavaBaseType(Class<T> clazz, String value) throws NumberFormatException {
if (clazz == Short.class) {
return (T) Short.valueOf(value);
}
if (clazz == Integer.class) {
return (T) Integer.valueOf(value);
}
if (clazz == Long.class) {
return (T) Long.valueOf(value);
}
if (clazz == Byte.class) {
return (T) Byte.valueOf(value);
}
if (clazz == Float.class) {
return (T) Float.valueOf(value);
}
if (clazz == Double.class) {
return (T) Double.valueOf(value);
}
if (clazz == Boolean.class) {
return (T) Boolean.valueOf(value);
}
throw new IllegalArgumentException("This method can not be applied to this class: " + clazz);
}

/**
* Convert to camel case
*
*
* @param text
* @return
*/
Expand Down Expand Up @@ -147,7 +188,7 @@ public static String toCamelCase(String text) {

/**
* Concatenate two paths using / as separator
*
*
* @param onePath
* @param anotherPath
* @return
Expand All @@ -158,7 +199,7 @@ public static String concatPaths(String onePath, String anotherPath) {

/**
* Concatenate two paths using a separator
*
*
* @param onePath
* @param anotherPath
* @param concatChars separator to use
Expand All @@ -182,7 +223,7 @@ public static String concatPaths(String onePath, String anotherPath, String conc

/**
* Check empty string
*
*
* @param value
* @return
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static Expression getLiteralExpr(Object object) {
} else if (object instanceof Character) {
return new CharLiteralExpr(((Character) object));
} else if (object instanceof Long) {
return new LongLiteralExpr(object.toString());
return new LongLiteralExpr(object + "L");
} else if (object instanceof Integer || object instanceof Short) {
return new IntegerLiteralExpr(object.toString());
} else if (object instanceof BigInteger) {
Expand Down
17 changes: 17 additions & 0 deletions quarkus/addons/knative/serving/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-serverless-workflow-rest-parser</artifactId>
</dependency>

<!-- Testing dependencies -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.addons.quarkus.knative.serving.deployment;

import org.kie.kogito.addons.quarkus.knative.serving.customfunctions.CloudEventKnativeParamsDecorator;
import org.kie.kogito.addons.quarkus.knative.serving.customfunctions.PlainJsonKnativeParamsDecorator;
import org.kie.kogito.quarkus.addons.common.deployment.KogitoCapability;
import org.kie.kogito.quarkus.addons.common.deployment.OneOfCapabilityKogitoAddOnProcessor;

import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;

public class KogitoAddonKnativeServingProcessor extends OneOfCapabilityKogitoAddOnProcessor {

static final String FEATURE = "kogito-addons-quarkus-knative-serving";

@BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}

KogitoAddonKnativeServingProcessor() {
super(KogitoCapability.SERVERLESS_WORKFLOW);
}

@BuildStep
public ReflectiveClassBuildItem reflectiveClasses() {
return new ReflectiveClassBuildItem(true,
true,
true,
CloudEventKnativeParamsDecorator.class, PlainJsonKnativeParamsDecorator.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.addons.quarkus.knative.serving.deployment;

import org.junit.jupiter.api.Test;
import org.kie.kogito.addons.quarkus.knative.serving.customfunctions.CloudEventKnativeParamsDecorator;
import org.kie.kogito.addons.quarkus.knative.serving.customfunctions.PlainJsonKnativeParamsDecorator;

import static org.assertj.core.api.Assertions.assertThat;

class KogitoAddonKnativeServingProcessorTest {

private final KogitoAddonKnativeServingProcessor processor = new KogitoAddonKnativeServingProcessor();

@Test
void feature() {
assertThat(processor.feature()).isNotNull();
assertThat(processor.feature().getName()).isEqualTo(KogitoAddonKnativeServingProcessor.FEATURE);
}

@Test
void reflectiveClasses() {
assertThat(processor.reflectiveClasses()).isNotNull();
assertThat(processor.reflectiveClasses().getClassNames())
.containsExactlyInAnyOrder(CloudEventKnativeParamsDecorator.class.getName(),
PlainJsonKnativeParamsDecorator.class.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
import org.junit.jupiter.api.Test;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusIntegrationTest;
import io.restassured.http.ContentType;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;

@QuarkusTest
@QuarkusIntegrationTest
@QuarkusTestResource(ServiceMock.class)
class MicroProfileConfigServiceAddonIT {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
import java.util.Optional;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.kie.kogito.addons.k8s.resource.catalog.KubernetesServiceCatalog;
import org.kie.kogito.addons.k8s.resource.catalog.KubernetesServiceCatalogKey;

Expand All @@ -33,13 +32,9 @@ public class MicroProfileConfigServiceCatalog implements KubernetesServiceCatalo

private static final String CONFIG_PREFIX = "org.kie.kogito.addons.discovery.";

@Inject
Config config;

@Override
public Optional<URI> getServiceAddress(KubernetesServiceCatalogKey key) {

return config.getOptionalValue(CONFIG_PREFIX + key.getProtocol().getValue() + ":" + key.getCoordinates(), String.class)
.map(URI::create);
return ConfigProvider.getConfig()
.getOptionalValue(CONFIG_PREFIX + key.getProtocol().getValue() + ":" + key.getCoordinates(), String.class).map(URI::create);
}
}

0 comments on commit 6322ddd

Please sign in to comment.