Merge pull request #45 from OnixByte/develop

v1.7.0
This commit is contained in:
Zihlu Wang
2024-12-29 12:23:25 +08:00
committed by GitHub
17 changed files with 255 additions and 116 deletions
+1 -1
View File
@@ -187,7 +187,7 @@
same "printed page" as the copyright notice for easier same "printed page" as the copyright notice for easier
identification within third-party archives. identification within third-party archives.
Copyright 2023 CodeCrafters (CN) Copyright 2023-2024 OnixByte (HK)
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -18,14 +18,12 @@
package com.onixbyte.devkit.core.exceptions; package com.onixbyte.devkit.core.exceptions;
/** /**
* The {@code NotImplementedException} class is a custom runtime exception * The {@code NotImplementedException} class is a custom runtime exception that represents a situation where a
* that represents a situation where a particular method or functionality is * particular method or functionality is not implemented or is currently unavailable in the codebase.
* not implemented or is currently unavailable in the codebase.
* <p> * <p>
* This exception is typically thrown when developers need to indicate that a * This exception is typically thrown when developers need to indicate that a specific part of the code is incomplete
* specific part of the code is incomplete or requires further implementation. * or requires further implementation. It serves as a placeholder to highlight unfinished sections of the application
* It serves as a placeholder to highlight unfinished sections of the * during development and testing phases.
* application during development and testing phases.
* <p> * <p>
* Usage Example: * Usage Example:
* <pre> * <pre>
@@ -54,15 +52,13 @@ package com.onixbyte.devkit.core.exceptions;
public class NotImplementedException extends RuntimeException { public class NotImplementedException extends RuntimeException {
/** /**
* Creates a new {@code NotImplementedException} with no specific error * Creates a new {@code NotImplementedException} with no specific error message.
* message.
*/ */
public NotImplementedException() { public NotImplementedException() {
} }
/** /**
* Creates a new {@code NotImplementedException} with the provided error * Creates a new {@code NotImplementedException} with the provided error message.
* message.
* *
* @param message the error message associated with this exception * @param message the error message associated with this exception
*/ */
@@ -71,8 +67,7 @@ public class NotImplementedException extends RuntimeException {
} }
/** /**
* Creates a new {@code NotImplementedException} with the specified error * Creates a new {@code NotImplementedException} with the specified error message and a cause for this exception.
* message and a cause for this exception.
* *
* @param message the error message associated with this exception * @param message the error message associated with this exception
* @param cause the cause of this exception * @param cause the cause of this exception
@@ -91,8 +86,8 @@ public class NotImplementedException extends RuntimeException {
} }
/** /**
* Creates a new {@code NotImplementedException} with the specified error * Creates a new {@code NotImplementedException} with the specified error message, cause, suppression flag, and
* message, cause, suppression flag, and stack trace writable flag. * stack trace writable flag.
* *
* @param message the error message associated with this * @param message the error message associated with this
* exception * exception
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <!--
~ Copyright (C) 2023 CodeCraftersCN. ~ Copyright (C) 2023-2024 OnixByte.
~ ~
~ Licensed under the Apache License, Version 2.0 (the "License"); ~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License. ~ you may not use this file except in compliance with the License.
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2024-2024 OnixByte.
*
* 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 com.onixbyte.devkit.core.exceptions;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
@Slf4j
public class NotImplementationExceptionTest {
@Test
public void testExceptionWithEmptyConstructor() {
try {
throw new NotImplementedException();
} catch (NotImplementedException e) {
log.error("NotImplementedException: ", e);
}
}
@Test
public void testExceptionWithStringConstructor() {
try {
throw new NotImplementedException("This function is not implemented yet, please contact developer for further information.");
} catch (NotImplementedException e) {
log.error("NotImplementedException: ", e);
}
}
}
@@ -119,7 +119,7 @@ public final class AesUtil {
} }
/** /**
* Generates 16 characters-long random secret. * Generates 16-character random secret.
* *
* @return the generated secure secret * @return the generated secure secret
*/ */
@@ -31,7 +31,7 @@ import java.util.Optional;
* Please see documentation for more information. * Please see documentation for more information.
* *
* @author zihluwang * @author zihluwang
* @version 1.4.2 * @version 1.7.0
* @since 1.0.0 * @since 1.0.0
*/ */
@Slf4j @Slf4j
@@ -42,14 +42,11 @@ public final class MapUtil {
* *
* @param <T> the type of the object * @param <T> the type of the object
* @param entity the object to be converted to a map * @param entity the object to be converted to a map
* @param adapters adapts the entity for mapping to a map * @param adapter adapts the entity for mapping to a map
* @return a map representing the fields and their values of the object * @return a map representing the fields and their values of the object
*/ */
public static <T> Map<String, Object> objectToMap(T entity, public static <T> Map<String, Object> objectToMap(T entity, ObjectMapAdapter<T> adapter) {
Map<String, ObjectMapAdapter<T, ?>> adapters) { return adapter.toMap(entity);
var resultMap = new HashMap<String, Object>();
adapters.forEach((fieldName, adapter) -> resultMap.put(fieldName, adapter.fetch(entity)));
return resultMap;
} }
/** /**
@@ -57,64 +54,17 @@ public final class MapUtil {
* map entries. * map entries.
* *
* @param objectMap the map representing the fields and their values * @param objectMap the map representing the fields and their values
* @param entity an empty entity of the target class * @param adapter the adapter to execute the setter for the entity
* @param adapters the adapters to execute the setter for the entity
* @param <T> the type of the object to be created * @param <T> the type of the object to be created
* @return an object of the specified type with the field values set from the map * @return an object of the specified type with the field values set from the map
*/ */
public static <T> T mapToObject(Map<String, Object> objectMap, public static <T> T mapToObject(Map<String, Object> objectMap, ObjectMapAdapter<T> adapter) {
T entity, return adapter.toObject(objectMap);
Map<String, ObjectMapAdapter<T, ?>> adapters) {
adapters.forEach((fieldName, adapter) -> Optional.ofNullable(objectMap)
.map((data) -> data.get(fieldName))
.ifPresent((fieldValue) -> adapter.setValue(entity, fieldValue)));
return entity;
} }
/** /**
* Retrieves the value of a field from an object using reflection. * Private constructor prevent class being instantiated.
*
* @param <E> the type of the entity
* @param <T> the type of the field value
* @param entity the object from which to retrieve the field value
* @param adapter the adapter to execute the getter
* @return the value of the field in the object, or null if the field does not exist or cannot
* be accessed
*/ */
public static <E, T> T getFieldValue(E entity, ObjectMapAdapter<E, T> adapter) {
return adapter.fetch(entity);
}
/**
* Sets the value of a field in an object using reflection.
*
* @param <E> the type of the entity
* @param <T> the type of the field value
* @param entity the object in which to set the field value
* @param adapter the adapter to execute the setter
* @param fieldValue the value to be set
*/
public static <E, T> void setFieldValue(E entity,
ObjectMapAdapter<E, T> adapter,
Object fieldValue) {
adapter.setValue(entity, fieldValue);
}
/**
* Casts the specified value to the required type with Optional.
*
* @param value the value to be cast
* @param requiredType the type to which the value should be cast
* @param <T> the type to which the value should be cast
* @return the cast value, or {@code null} if the value is not an instance of the requiredType
*/
public static <T> T cast(Object value, Class<T> requiredType) {
return Optional.ofNullable(requiredType)
.filter((clazz) -> clazz.isInstance(value))
.map((clazz) -> clazz.cast(value))
.orElse(null);
}
private MapUtil() { private MapUtil() {
} }
} }
@@ -17,53 +17,32 @@
package com.onixbyte.devkit.utils; package com.onixbyte.devkit.utils;
import java.util.function.BiConsumer; import java.util.Map;
import java.util.function.Function;
/** /**
* Adapts an Object to a Map, making conversion between Map and Object much more safe. * Adapts an Object to a Map, making conversion between Map and Object much more safe.
* *
* @param <E> entity type
* @param <T> field type * @param <T> field type
* @author zihluwang * @author zihluwang
* @version 1.4.2 * @version 1.7.0
* @since 1.4.2 * @since 1.4.2
*/ */
public class ObjectMapAdapter<E, T> { public interface ObjectMapAdapter<T> {
private final Function<E, T> getter;
private final BiConsumer<E, Object> setter;
/** /**
* Create an adapter. * Convert an object to a map.
* *
* @param getter the getter of the field * @param element the element that will be converted to Map
* @param setter the setter of the field * @return a Map that is converted from the element
*/ */
public ObjectMapAdapter(Function<E, T> getter, BiConsumer<E, Object> setter) { Map<String, Object> toMap(T element);
this.getter = getter;
this.setter = setter;
}
/** /**
* Get data from the entity. * Convert a Map to an object.
* *
* @param entity the source of the data * @param map the map that will be converted to Object
* @return the data * @return the object that is converted from the Map
*/ */
public T fetch(E entity) { T toObject(Map<String, Object> map);
return getter.apply(entity);
}
/**
* Set value to the entity.
*
* @param entity the target of the data
* @param value the value
*/
public void setValue(E entity, Object value) {
setter.accept(entity, value);
}
} }
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <!--
~ Copyright (C) 2023 CodeCraftersCN. ~ Copyright (C) 2023-2024 OnixByte.
~ ~
~ Licensed under the Apache License, Version 2.0 (the "License"); ~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License. ~ you may not use this file except in compliance with the License.
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2024-2024 OnixByte.
*
* 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 com.onixbyte.devkit.utils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@Slf4j
public class TestAesUtil {
@Test
public void testGenerateRandomSecret() {
log.info("Secret is {}", AesUtil.generateRandomSecret());
}
@Test
public void testEncrypt() {
var secret = "43f72073956d4c81";
Assertions.assertEquals("IbbYZu8GtMruBURfMBVy/w==", AesUtil.encrypt("Hello World", secret));
Assertions.assertEquals("1eVA7oQpTIhI7jc+6cdkmg==", AesUtil.encrypt("OnixByte", secret));
Assertions.assertEquals("fk6oNRJK8a+Pz7zVwtlD0UQocq5c3GkRuem0Z6jdAN8=", AesUtil.encrypt("Welcome to use JDevKit!", secret));
Assertions.assertEquals("dqzGjawNcQdBpXJWk/08UQ==", AesUtil.encrypt("127.0.0.1", secret));
Assertions.assertEquals("uwQQI60yAGL91q9jCDgoeA==", AesUtil.encrypt("root", secret));
}
@Test
public void testDecrypt() {
var secret = "43f72073956d4c81";
Assertions.assertEquals("Hello World", AesUtil.decrypt("IbbYZu8GtMruBURfMBVy/w==", secret));
Assertions.assertEquals("OnixByte", AesUtil.decrypt("1eVA7oQpTIhI7jc+6cdkmg==", secret));
Assertions.assertEquals("Welcome to use JDevKit!", AesUtil.decrypt("fk6oNRJK8a+Pz7zVwtlD0UQocq5c3GkRuem0Z6jdAN8=", secret));
Assertions.assertEquals("127.0.0.1", AesUtil.decrypt("dqzGjawNcQdBpXJWk/08UQ==", secret));
Assertions.assertEquals("root", AesUtil.decrypt("uwQQI60yAGL91q9jCDgoeA==", secret));
}
}
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2024-2024 OnixByte.
*
* 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 com.onixbyte.devkit.utils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@Slf4j
public class TestBase64Util {
@Test
public void testEncode() {
Assertions.assertEquals("SGVsbG8gV29ybGQ=", Base64Util.encode("Hello World"));
Assertions.assertEquals("MTI3LjAuMC4x", Base64Util.encode("127.0.0.1"));
Assertions.assertEquals("cm9vdA==", Base64Util.encode("root"));
}
@Test
public void testDecode() {
Assertions.assertEquals("Hello World", Base64Util.decode("SGVsbG8gV29ybGQ="));
Assertions.assertEquals("127.0.0.1", Base64Util.decode("MTI3LjAuMC4x"));
Assertions.assertEquals("root", Base64Util.decode("cm9vdA=="));
}
@Test
public void testEncodeUriComponent() {
Assertions.assertEquals("aHR0cHM6Ly9nb29nbGUuY29t", Base64Util.encodeUrlComponents("https://google.com"));
Assertions.assertEquals("aHR0cDovLzEyNy4wLjAuMTo4MDgwL2FwaS91c2VyLzEyMzQ1", Base64Util.encodeUrlComponents("http://127.0.0.1:8080/api/user/12345"));
}
@Test
public void testDecodeUriComponent() {
Assertions.assertEquals("https://google.com", Base64Util.decodeUrlComponents("aHR0cHM6Ly9nb29nbGUuY29t"));
Assertions.assertEquals("http://127.0.0.1:8080/api/user/12345", Base64Util.decodeUrlComponents("aHR0cDovLzEyNy4wLjAuMTo4MDgwL2FwaS91c2VyLzEyMzQ1"));
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <!--
~ Copyright (C) 2023 CodeCraftersCN. ~ Copyright (C) 2023-2024 OnixByte.
~ ~
~ Licensed under the Apache License, Version 2.0 (the "License"); ~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License. ~ you may not use this file except in compliance with the License.
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (C) 2024-2024 OnixByte.
~
~ 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.
-->
<configuration>
<property name="COLOURFUL_OUTPUT" value="%black(%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK}) %highlight(%-5level) %black(---) %black([%10.10t]) %cyan(%-20.20logger{20}) %black(:) %msg%n"/>
<property name="STANDARD_OUTPUT" value="%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK} %-5level %black(---) [%10.10t] %-20.20logger{20} : %msg%n"/>
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${COLOURFUL_OUTPUT}</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
+32
View File
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (C) 2024-2024 OnixByte.
~
~ 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.
-->
<configuration>
<property name="COLOURFUL_OUTPUT" value="%black(%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK}) %highlight(%-5level) %black(---) %black([%10.10t]) %cyan(%-20.20logger{20}) %black(:) %msg%n"/>
<property name="STANDARD_OUTPUT" value="%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK} %-5level %black(---) [%10.10t] %-20.20logger{20} : %msg%n"/>
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${COLOURFUL_OUTPUT}</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <!--
~ Copyright (C) 2023 CodeCraftersCN. ~ Copyright (C) 2023-2024 OnixByte.
~ ~
~ Licensed under the Apache License, Version 2.0 (the "License"); ~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License. ~ you may not use this file except in compliance with the License.
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <!--
~ Copyright (C) 2023 CodeCraftersCN. ~ Copyright (C) 2023-2024 OnixByte.
~ ~
~ Licensed under the Apache License, Version 2.0 (the "License"); ~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License. ~ you may not use this file except in compliance with the License.
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <!--
~ Copyright (C) 2023 CodeCraftersCN. ~ Copyright (C) 2023-2024 OnixByte.
~ ~
~ Licensed under the Apache License, Version 2.0 (the "License"); ~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License. ~ you may not use this file except in compliance with the License.
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <!--
~ Copyright (C) 2023 CodeCraftersCN. ~ Copyright (C) 2023-2024 OnixByte.
~ ~
~ Licensed under the Apache License, Version 2.0 (the "License"); ~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License. ~ you may not use this file except in compliance with the License.