-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve: better namespace naming for junit extension and custom patte…
…rns (#2171) Signed-off-by: Attila Mészáros <csviri@gmail.com>
- Loading branch information
Showing
9 changed files
with
282 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...-junit5/src/main/java/io/javaoperatorsdk/operator/junit/DefaultNamespaceNameSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.javaoperatorsdk.operator.junit; | ||
|
||
import java.util.Locale; | ||
import java.util.UUID; | ||
import java.util.function.Function; | ||
|
||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
||
import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil; | ||
|
||
import static io.javaoperatorsdk.operator.junit.AbstractOperatorExtension.MAX_NAMESPACE_NAME_LENGTH; | ||
|
||
public class DefaultNamespaceNameSupplier implements Function<ExtensionContext, String> { | ||
|
||
public static final int RANDOM_SUFFIX_LENGTH = 5; | ||
public static final int DELIMITERS_LENGTH = 2; | ||
|
||
public static final int MAX_NAME_LENGTH_TOGETHER = | ||
MAX_NAMESPACE_NAME_LENGTH - DELIMITERS_LENGTH - RANDOM_SUFFIX_LENGTH; | ||
public static final int PART_RESERVED_NAME_LENGTH = MAX_NAME_LENGTH_TOGETHER / 2; | ||
|
||
public static final String DELIMITER = "-"; | ||
|
||
@Override | ||
public String apply(ExtensionContext context) { | ||
String classPart = context.getRequiredTestClass().getSimpleName(); | ||
String methodPart = context.getRequiredTestMethod().getName(); | ||
if (classPart.length() + methodPart.length() + DELIMITERS_LENGTH | ||
+ RANDOM_SUFFIX_LENGTH > MAX_NAMESPACE_NAME_LENGTH) { | ||
if (classPart.length() > PART_RESERVED_NAME_LENGTH) { | ||
int classPartMaxLength = | ||
methodPart.length() > PART_RESERVED_NAME_LENGTH ? PART_RESERVED_NAME_LENGTH | ||
: MAX_NAME_LENGTH_TOGETHER - methodPart.length(); | ||
classPart = classPart.substring(0, Math.min(classPartMaxLength, classPart.length())); | ||
} | ||
if (methodPart.length() > PART_RESERVED_NAME_LENGTH) { | ||
int methodPartMaxLength = | ||
classPart.length() > PART_RESERVED_NAME_LENGTH ? PART_RESERVED_NAME_LENGTH | ||
: MAX_NAME_LENGTH_TOGETHER - classPart.length(); | ||
methodPart = methodPart.substring(0, Math.min(methodPartMaxLength, methodPart.length())); | ||
} | ||
} | ||
|
||
String namespace = classPart + DELIMITER + methodPart + DELIMITER + UUID.randomUUID().toString() | ||
.substring(0, RANDOM_SUFFIX_LENGTH); | ||
namespace = KubernetesResourceUtil.sanitizeName(namespace).toLowerCase(Locale.US); | ||
return namespace; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...src/main/java/io/javaoperatorsdk/operator/junit/DefaultPerClassNamespaceNameSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.javaoperatorsdk.operator.junit; | ||
|
||
import java.util.Locale; | ||
import java.util.UUID; | ||
import java.util.function.Function; | ||
|
||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
||
import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil; | ||
|
||
import static io.javaoperatorsdk.operator.junit.AbstractOperatorExtension.MAX_NAMESPACE_NAME_LENGTH; | ||
import static io.javaoperatorsdk.operator.junit.DefaultNamespaceNameSupplier.DELIMITER; | ||
import static io.javaoperatorsdk.operator.junit.DefaultNamespaceNameSupplier.RANDOM_SUFFIX_LENGTH; | ||
|
||
public class DefaultPerClassNamespaceNameSupplier implements Function<ExtensionContext, String> { | ||
|
||
public static final int MAX_CLASS_NAME_LENGTH = | ||
MAX_NAMESPACE_NAME_LENGTH - RANDOM_SUFFIX_LENGTH - 1; | ||
|
||
@Override | ||
public String apply(ExtensionContext context) { | ||
String className = context.getRequiredTestClass().getSimpleName(); | ||
String namespace = | ||
className.length() > MAX_CLASS_NAME_LENGTH ? className.substring(0, MAX_CLASS_NAME_LENGTH) | ||
: className; | ||
namespace += DELIMITER; | ||
namespace += UUID.randomUUID().toString().substring(0, RANDOM_SUFFIX_LENGTH); | ||
namespace = KubernetesResourceUtil.sanitizeName(namespace).toLowerCase(Locale.US); | ||
namespace = namespace.substring(0, Math.min(namespace.length(), MAX_NAMESPACE_NAME_LENGTH)); | ||
return namespace; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...it5/src/test/java/io/javaoperatorsdk/operator/junit/DefaultNamespaceNameSupplierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.javaoperatorsdk.operator.junit; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static io.javaoperatorsdk.operator.junit.AbstractOperatorExtension.MAX_NAMESPACE_NAME_LENGTH; | ||
import static io.javaoperatorsdk.operator.junit.DefaultNamespaceNameSupplier.*; | ||
import static io.javaoperatorsdk.operator.junit.NamespaceNamingTestUtils.*; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class DefaultNamespaceNameSupplierTest { | ||
|
||
|
||
DefaultNamespaceNameSupplier supplier = new DefaultNamespaceNameSupplier(); | ||
|
||
@Test | ||
void trivialCase() { | ||
String ns = supplier.apply(mockExtensionContext(SHORT_CLASS_NAME, SHORT_METHOD_NAME)); | ||
|
||
assertThat(ns).startsWith(SHORT_CLASS_NAME + DELIMITER + SHORT_METHOD_NAME + DELIMITER); | ||
shortEnoughAndEndsWithRandomString(ns); | ||
} | ||
|
||
@Test | ||
void classPartLongerCase() { | ||
String ns = supplier.apply(mockExtensionContext(LONG_CLASS_NAME, SHORT_METHOD_NAME)); | ||
|
||
assertThat(ns).startsWith(LONG_CLASS_NAME + DELIMITER + SHORT_METHOD_NAME + DELIMITER); | ||
shortEnoughAndEndsWithRandomString(ns); | ||
} | ||
|
||
@Test | ||
void methodPartLonger() { | ||
String ns = supplier.apply(mockExtensionContext(SHORT_CLASS_NAME, LONG_METHOD_NAME)); | ||
|
||
assertThat(ns).startsWith(SHORT_CLASS_NAME + DELIMITER + LONG_METHOD_NAME + DELIMITER); | ||
shortEnoughAndEndsWithRandomString(ns); | ||
} | ||
|
||
@Test | ||
void methodPartAndClassPartLonger() { | ||
String ns = supplier.apply(mockExtensionContext(LONG_CLASS_NAME, LONG_METHOD_NAME)); | ||
|
||
assertThat(ns).startsWith(LONG_CLASS_NAME.substring(0, PART_RESERVED_NAME_LENGTH) + DELIMITER | ||
+ LONG_METHOD_NAME.substring(0, PART_RESERVED_NAME_LENGTH) | ||
+ DELIMITER); | ||
shortEnoughAndEndsWithRandomString(ns); | ||
} | ||
|
||
|
||
private static void shortEnoughAndEndsWithRandomString(String ns) { | ||
assertThat(ns.length()).isLessThanOrEqualTo(MAX_NAMESPACE_NAME_LENGTH); | ||
assertThat(ns.split("-")[2]).hasSize(RANDOM_SUFFIX_LENGTH); | ||
} | ||
|
||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...test/java/io/javaoperatorsdk/operator/junit/DefaultPerClassNamespaceNameSupplierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.javaoperatorsdk.operator.junit; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
||
import static io.javaoperatorsdk.operator.junit.AbstractOperatorExtension.MAX_NAMESPACE_NAME_LENGTH; | ||
import static io.javaoperatorsdk.operator.junit.DefaultNamespaceNameSupplier.DELIMITER; | ||
import static io.javaoperatorsdk.operator.junit.DefaultNamespaceNameSupplier.RANDOM_SUFFIX_LENGTH; | ||
import static io.javaoperatorsdk.operator.junit.DefaultPerClassNamespaceNameSupplier.MAX_CLASS_NAME_LENGTH; | ||
import static io.javaoperatorsdk.operator.junit.NamespaceNamingTestUtils.SHORT_CLASS_NAME; | ||
import static io.javaoperatorsdk.operator.junit.NamespaceNamingTestUtils.VERY_LONG_CLASS_NAME; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class DefaultPerClassNamespaceNameSupplierTest { | ||
|
||
DefaultPerClassNamespaceNameSupplier supplier = new DefaultPerClassNamespaceNameSupplier(); | ||
|
||
@Test | ||
void shortClassCase() { | ||
var ns = supplier.apply(mockExtensionContext(SHORT_CLASS_NAME)); | ||
|
||
assertThat(ns).startsWith(SHORT_CLASS_NAME + DELIMITER); | ||
shortEnoughAndEndsWithRandomString(ns); | ||
} | ||
|
||
@Test | ||
void longClassCase() { | ||
var ns = supplier.apply(mockExtensionContext(VERY_LONG_CLASS_NAME)); | ||
|
||
assertThat(ns).startsWith(VERY_LONG_CLASS_NAME.substring(0, MAX_CLASS_NAME_LENGTH) + DELIMITER); | ||
shortEnoughAndEndsWithRandomString(ns); | ||
assertThat(ns).hasSize(MAX_NAMESPACE_NAME_LENGTH); | ||
} | ||
|
||
public static ExtensionContext mockExtensionContext(String className) { | ||
return NamespaceNamingTestUtils.mockExtensionContext(className, null); | ||
} | ||
|
||
private static void shortEnoughAndEndsWithRandomString(String ns) { | ||
assertThat(ns.length()).isLessThanOrEqualTo(MAX_NAMESPACE_NAME_LENGTH); | ||
assertThat(ns.split("-")[1]).hasSize(RANDOM_SUFFIX_LENGTH); | ||
} | ||
|
||
} |
Oops, something went wrong.