-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: TestResult error message truncation
- Loading branch information
Thorsten Schlathoelter
committed
Nov 7, 2023
1 parent
408b563
commit 4f51d5e
Showing
15 changed files
with
150 additions
and
51 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
simulator-starter/src/main/java/org/citrusframework/simulator/model/EntityUtils.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,75 @@ | ||
/* | ||
* Copyright 2006-2017 the original author or authors. | ||
* | ||
* 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.citrusframework.simulator.model; | ||
|
||
import jakarta.persistence.Column; | ||
import org.citrusframework.exceptions.CitrusRuntimeException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* EntityUtils - A utility class for handling data model operations. | ||
* | ||
* This class provides utility methods for working with data models and entities. | ||
* | ||
* @author Thorsten Schlathoelter | ||
*/ | ||
public class EntityUtils { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(EntityUtils.class); | ||
|
||
private EntityUtils() { | ||
// only static access | ||
} | ||
|
||
/** | ||
* Truncate a string value to fit the column size specified for an entity property. | ||
* | ||
* This method is used to truncate a string value to fit the maximum column size | ||
* specified in the corresponding entity class for a specific property. If the provided | ||
* value is longer than the specified column size, it will be silently truncated to match | ||
* the column size. | ||
* | ||
* The size of the column is determined using reflection. If this fails for any reason, the | ||
* exception will silently be ignored, has this is only harmful, if a value would actually | ||
* need truncation, which will be revealed by the persistence layer anyway. | ||
* | ||
* @param entityClass The class of the entity containing the property definition. | ||
* @param entityProperty The name of the property for which the column size is defined. | ||
* @param value The value to be truncated. | ||
* @return The truncated value. | ||
*/ | ||
public static String truncateToColumnSize(Class<?> entityClass, String entityProperty, String value) { | ||
if (StringUtils.hasLength(value)) { | ||
try { | ||
int size = entityClass.getDeclaredField(entityProperty).getAnnotation(Column.class) | ||
.length(); | ||
int inLength = value.length(); | ||
if (inLength > size) { | ||
value = value.substring(0, size); | ||
} | ||
} catch (NoSuchFieldException e) { | ||
throw new CitrusRuntimeException(String.format("entityProperty '%s' unknown for class '%s'", entityProperty, entityClass.getName())); | ||
} catch (SecurityException e) { | ||
logger.warn("Unable to perform truncation for entity class '{}' and field '{}'.", entityClass, entityProperty, e); | ||
} | ||
} | ||
return value; | ||
} | ||
|
||
} |
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
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
48 changes: 48 additions & 0 deletions
48
simulator-starter/src/test/java/org/citrusframework/simulator/model/EntityUtilsTest.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,48 @@ | ||
package org.citrusframework.simulator.model; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import jakarta.persistence.Column; | ||
import org.citrusframework.exceptions.CitrusRuntimeException; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class EntityUtilsTest { | ||
|
||
@Test | ||
void truncateToColumnSizePerformed() { | ||
String message = "The quick brown fox jumps over the lazy dog"; | ||
String truncatedMessage = EntityUtils.truncateToColumnSize(TestEntity.class, "message", | ||
message); | ||
assertEquals("The quick brown fox jumps over the la", truncatedMessage); | ||
} | ||
|
||
@Test | ||
void truncateToColumnSizeNotPerformed() { | ||
String message = "Waltz, bad nymph, for quick jigs vex"; | ||
String truncatedMessage = EntityUtils.truncateToColumnSize(TestEntity.class, "message", | ||
message); | ||
assertEquals(message, truncatedMessage); | ||
} | ||
|
||
@Test | ||
void truncateToColumnSizeExceptionOnUnknownProperty() { | ||
String message = "Sphinx of black quartz, judge my vow."; | ||
Assertions.assertThrows(CitrusRuntimeException.class, () -> EntityUtils.truncateToColumnSize(TestEntity.class, "otherMessage", | ||
message)); | ||
} | ||
|
||
public static class TestEntity { | ||
|
||
@Column(length=37) | ||
String message; | ||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
} | ||
} |
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
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
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