Skip to content

Commit 50d8c83

Browse files
Feature/data module extraction (#75)
Added generic http repository implementation
1 parent b2e25c8 commit 50d8c83

29 files changed

+943
-13
lines changed

bellatrix.core/src/main/java/solutions/bellatrix/core/configuration/ConfigurationService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,4 @@ public static String getFileAsString(String fileName) {
8686
return "";
8787
}
8888
}
89-
}
89+
}

bellatrix.core/src/main/java/solutions/bellatrix/core/utilities/ObjectFactory.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@
2222
public abstract class ObjectFactory {
2323
protected static <T> T newInstance(Class<T> clazz, Object... args) throws InvocationTargetException, InstantiationException, IllegalAccessException, ConstructorNotFoundException {
2424
Constructor<T> suitableConstructor = getSuitableConstructor(clazz, args);
25-
26-
if (suitableConstructor.isVarArgs() && args.length < suitableConstructor.getParameterCount()) {
27-
return (T)suitableConstructor.newInstance(addNullVarArgsTo(args));
28-
} else
29-
return (T)suitableConstructor.newInstance(args);
25+
try {
26+
if (suitableConstructor.isVarArgs() && args.length < suitableConstructor.getParameterCount()) {
27+
return (T)suitableConstructor.newInstance(addNullVarArgsTo(args));
28+
} else
29+
return (T)suitableConstructor.newInstance(args);
30+
} catch (Exception e) {
31+
var exception = (InvocationTargetException)e;
32+
throw new InvocationTargetException(exception.getTargetException());
33+
}
3034
}
3135

3236
private static Object[] addNullVarArgsTo(Object... args) {
@@ -77,7 +81,7 @@ private static <T> Constructor<T> findVarArgsConstructor(Class clazz, Class[] ar
7781
args[args.length - 1] = getVarArgsType(clazz, argumentTypes);
7882

7983
return clazz.getDeclaredConstructor(args);
80-
} catch (NoSuchMethodException|NullPointerException ignored) {
84+
} catch (NoSuchMethodException | NullPointerException ignored) {
8185
}
8286

8387
throw new NoSuchMethodException("No matching constructor found for the provided argument types.");
@@ -101,7 +105,7 @@ private static boolean lengthMatches(Parameter[] parameters, Class[] argumentTyp
101105
}
102106

103107
private static boolean parameterTypesMatch(Parameter[] parameters, Class[] argumentTypes) {
104-
for (int i = 0; i < argumentTypes.length; i ++) {
108+
for (int i = 0; i < argumentTypes.length; i++) {
105109
if (!parameters[i].getType().equals(argumentTypes[i]))
106110
return false;
107111
}
@@ -138,4 +142,4 @@ public ConstructorNotFoundException(String message, Throwable cause, boolean ena
138142
super(message, cause, enableSuppression, writableStackTrace);
139143
}
140144
}
141-
}
145+
}

bellatrix.core/src/main/java/solutions/bellatrix/core/utilities/SingletonFactory.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ public static <T> T getInstance(Class<T> classOf, Object... initargs) {
3838
private static <T> T tryGetInstance(Class<T> classOf, Object... initargs) {
3939
try {
4040
return newInstance(classOf, initargs);
41-
} catch (InvocationTargetException | InstantiationException | IllegalAccessException |
41+
} catch (InvocationTargetException e) {
42+
Log.error(e.getTargetException().getMessage(), e);
43+
return null;
44+
} catch (InstantiationException | IllegalAccessException |
4245
ConstructorNotFoundException e) {
43-
Log.error("Failed to create instance of the class %s.\nException was:\n%s".formatted(classOf.getName(), e));
46+
Log.error("Failed to create instance of the class %s.\nException was:\n%s".formatted(classOf.getName(), e.getMessage()));
4447
return null;
4548
}
4649
}
@@ -62,8 +65,8 @@ public static boolean containsKey(Class<?> classOf) {
6265
public static boolean containsValue(Object object) {
6366
return mapHolder.get().containsValue(object);
6467
}
65-
68+
6669
public static void clear() {
6770
mapHolder.get().clear();
6871
}
69-
}
72+
}

bellatrix.data/pom.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>solutions.bellatrix</groupId>
8+
<artifactId>bellatrix</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>bellatrix.data</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>19</maven.compiler.source>
16+
<maven.compiler.target>19</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<rest.assured.version>5.5.5</rest.assured.version>
19+
<gson.version>2.13.1</gson.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>com.google.code.gson</groupId>
25+
<artifactId>gson</artifactId>
26+
<version>${gson.version}</version>
27+
<scope>compile</scope>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.projectlombok</groupId>
31+
<artifactId>lombok</artifactId>
32+
<version>${lombok.version}</version>
33+
<scope>provided</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>solutions.bellatrix</groupId>
37+
<artifactId>bellatrix.core</artifactId>
38+
<version>1.0</version>
39+
<scope>compile</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>io.rest-assured</groupId>
43+
<artifactId>rest-assured</artifactId>
44+
<version>${rest.assured.version}</version>
45+
<scope>compile</scope>
46+
</dependency>
47+
</dependencies>
48+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package solutions.bellatrix.data.configuration;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import solutions.bellatrix.data.http.configuration.HttpSettings;
6+
7+
@Data
8+
public class DataSettings {
9+
@SerializedName("dataSourceType")
10+
private String datasourceType;
11+
@SerializedName("httpSettings")
12+
private HttpSettings httpSettings;
13+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package solutions.bellatrix.data.configuration;
2+
3+
import solutions.bellatrix.core.utilities.SingletonFactory;
4+
import solutions.bellatrix.data.contracts.Repository;
5+
import solutions.bellatrix.data.http.infrastructure.Entity;
6+
7+
import java.util.Map;
8+
import java.util.Objects;
9+
import java.util.concurrent.ConcurrentHashMap;
10+
11+
public enum RepositoryFactory {
12+
INSTANCE;
13+
14+
private final Map<Class<? extends Entity>, Class<? extends Repository>> repositories = new ConcurrentHashMap<>();
15+
16+
public <T extends Entity> void registerRepository(Class<T> entityClass, Class<? extends Repository<T>> repositoryClass) {
17+
repositories.put(entityClass, repositoryClass);
18+
}
19+
20+
public <T extends Entity> Repository<T> getRepository(Class<T> entityClass) {
21+
var repositoryClassType = repositories.get(entityClass);
22+
23+
if (Objects.isNull(repositoryClassType)) {
24+
throw new IllegalArgumentException("No repository registered for entity class: " + entityClass.getName());
25+
}
26+
27+
return (Repository<T>)SingletonFactory.getInstance(repositoryClassType);
28+
}
29+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package solutions.bellatrix.data.contracts;
2+
3+
import solutions.bellatrix.data.http.infrastructure.Entity;
4+
5+
import java.util.List;
6+
7+
public interface Repository<T extends Entity> {
8+
T getById(T entity);
9+
10+
List<T> getAll();
11+
12+
T create(T entity);
13+
14+
T update(T entity);
15+
16+
void delete(T entity);
17+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package solutions.bellatrix.data.http.authentication;
2+
3+
import io.restassured.authentication.AuthenticationScheme;
4+
import io.restassured.authentication.BasicAuthScheme;
5+
import io.restassured.authentication.NoAuthScheme;
6+
import io.restassured.authentication.PreemptiveOAuth2HeaderScheme;
7+
8+
public class AuthSchemaFactory {
9+
public static AuthenticationScheme getAuthenticationScheme(Authentication authentication) {
10+
var authType = authentication.getAuthenticationMethod();
11+
var option = authentication.getAuthenticationOptions().stream().filter(x -> x.get("type").equals(authType.getMethod())).findFirst();
12+
if (option.isEmpty()) {
13+
throw new IllegalArgumentException("Authentication type not found: %s, Supported types : ".formatted(authType));
14+
}
15+
16+
switch (authType) {
17+
case BASIC -> {
18+
var basicAuth = option.get();
19+
String username = basicAuth.get("username").toString();
20+
String password = basicAuth.get("password").toString();
21+
var basicSchema = new BasicAuthScheme();
22+
basicSchema.setUserName(username);
23+
basicSchema.setPassword(password);
24+
return basicSchema;
25+
}
26+
case BEARER -> {
27+
var bearerAuth = option.get();
28+
String token = bearerAuth.get("token").toString();
29+
var bearerSchema = new PreemptiveOAuth2HeaderScheme();
30+
bearerSchema.setAccessToken(token);
31+
return bearerSchema;
32+
}
33+
case QUERY_PARAMETER -> {
34+
return new NoAuthScheme();
35+
}
36+
default -> throw new IllegalArgumentException("Unsupported authentication type: %s".formatted(authType));
37+
}
38+
}
39+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package solutions.bellatrix.data.http.authentication;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.AccessLevel;
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
8+
import java.util.LinkedHashMap;
9+
import java.util.LinkedList;
10+
11+
public class Authentication {
12+
@SerializedName("method")
13+
private String method;
14+
@SerializedName("options")
15+
@Getter private LinkedList<LinkedHashMap<String, Object>> authenticationOptions;
16+
17+
@Setter(AccessLevel.PRIVATE)
18+
private transient AuthenticationMethod authenticationMethod;
19+
20+
public AuthenticationMethod getAuthenticationMethod() {
21+
setAuthenticationMethod(AuthenticationMethod.parse(method));
22+
return authenticationMethod;
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package solutions.bellatrix.data.http.authentication;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public enum AuthenticationMethod {
7+
BEARER("Bearer"),
8+
BASIC("Basic"),
9+
QUERY_PARAMETER("QueryParameters");
10+
11+
private final String method;
12+
13+
AuthenticationMethod(String method) {
14+
this.method = method;
15+
}
16+
17+
public static AuthenticationMethod parse(String y) {
18+
for (var state : values()) {
19+
String enumDisplayValue = state.getMethod();
20+
if (enumDisplayValue != null && enumDisplayValue.equalsIgnoreCase(y)) {
21+
return state;
22+
}
23+
}
24+
25+
throw new IllegalArgumentException("No enum constant with value: %s".formatted(y));
26+
}
27+
}

0 commit comments

Comments
 (0)