Skip to content

Commit 132c3ed

Browse files
committed
Exception added when missing http settings in config file
1 parent e035037 commit 132c3ed

File tree

8 files changed

+32
-23
lines changed

8 files changed

+32
-23
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public static String getEnvironment() {
3636

3737
public static <T> T get(Class<T> configSection) {
3838
T mappedObject = (T)new Object();
39-
if (environment==null) {
39+
if (environment == null) {
4040
String environmentOverride = System.getProperty("environment");
41-
if (environmentOverride==null) {
41+
if (environmentOverride == null) {
4242
InputStream input = ConfigurationService.class.getResourceAsStream("/application.properties");
4343
var p = new Properties();
4444
try {

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/src/main/java/solutions/bellatrix/data/http/authentication/AuthenticationMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public enum AuthenticationMethod {
1717
public static AuthenticationMethod parse(String y) {
1818
for (var state : values()) {
1919
String enumDisplayValue = state.getMethod();
20-
if (enumDisplayValue!=null && enumDisplayValue.equalsIgnoreCase(y)) {
20+
if (enumDisplayValue != null && enumDisplayValue.equalsIgnoreCase(y)) {
2121
return state;
2222
}
2323
}

bellatrix.data/src/main/java/solutions/bellatrix/data/http/configuration/HttpSettings.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public HttpSettings(HttpSettings httpSettings) {
3232

3333
public static HttpSettings custom(Consumer<HttpSettings> httpSettings) {
3434
var settings = ConfigurationService.get(DataSettings.class).getHttpSettings();
35+
if (settings == null) {
36+
throw new IllegalStateException("Include the httpSettings section in your config file");
37+
38+
}
3539
httpSettings.accept(settings);
3640
return settings;
3741
}

bellatrix.data/src/main/java/solutions/bellatrix/data/http/httpContext/HttpContext.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import solutions.bellatrix.data.http.authentication.AuthSchemaFactory;
99
import solutions.bellatrix.data.http.authentication.AuthenticationMethod;
1010
import solutions.bellatrix.data.http.configuration.HttpSettings;
11-
import solutions.bellatrix.data.http.infrastructure.Entity;
1211
import solutions.bellatrix.data.http.infrastructure.HTTPMethod;
1312
import solutions.bellatrix.data.http.infrastructure.QueryParameter;
1413

@@ -68,7 +67,7 @@ private RequestSpecBuilder getRequestBuilder() {
6867
}
6968

7069
public RequestSpecification requestSpecification() {
71-
if (requestBody!=null) {
70+
if (requestBody != null) {
7271
specBuilder.setBody(requestBody);
7372
}
7473

@@ -106,7 +105,7 @@ public void addQueryParameter(QueryParameter parameter) {
106105
private Map<String, String> getRequestQueryParameters() {
107106
//todo: this logic should be extracted because create tightly coupling with settings
108107
LinkedHashMap<String, String> queryParams = new LinkedHashMap<>();
109-
if (httpSettings.getAuthenticationMethod()==AuthenticationMethod.QUERY_PARAMETER) {
108+
if (httpSettings.getAuthenticationMethod() == AuthenticationMethod.QUERY_PARAMETER) {
110109
var option = httpSettings.getAuthentication().getAuthenticationOptions().stream().filter(x -> x.get("type").equals("QueryParameters")).findFirst().get();
111110
String insertionOrder = (String)option.get("insertionOrder");
112111
if (insertionOrder.equals("start")) {

bellatrix.data/src/main/java/solutions/bellatrix/data/http/infrastructure/Entity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public TEntity create() {
1818

1919
public TEntity update() {
2020
var repository = (Repository<Entity>)RepositoryFactory.INSTANCE.getRepository(this.getClass());
21-
return (TEntity)repository.update(this);`
21+
return (TEntity)repository.update(this);
2222
}
2323

2424
public void delete() {

bellatrix.data/src/main/java/solutions/bellatrix/data/http/infrastructure/HttpRepository.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
public abstract class HttpRepository<THttpEntity extends HttpEntity> implements Repository<THttpEntity> {
2020
public static final EventListener<HttpRequestEventArgs> SENDING_REQUEST = new EventListener<>();
21-
public static final EventListener<ResponseProcessingEventArgs> REQUEST_SEND = new EventListener<>();
21+
public static final EventListener<ResponseProcessingEventArgs> REQUEST_SENT = new EventListener<>();
2222
public static final EventListener<EntityCreatedEventArgs> CREATING_ENTITY = new EventListener<>();
2323
public static final EventListener<EntityCreatedEventArgs> ENTITY_CREATED = new EventListener<>();
2424
public static final EventListener<EntityUpdatedEventArgs> UPDATING_ENTITY = new EventListener<>();
@@ -44,7 +44,6 @@ public THttpEntity getById(HttpEntity entity) {
4444
if (entity.hasInvalidIdentifier()) {
4545
throw new IllegalArgumentException("Entity identifier cannot be null.");
4646
}
47-
4847
updateRequestContext(requestContext -> {
4948
requestContext.addPathParameter(entity.getIdentifier());
5049
requestContext.addRequestMethod(GET);
@@ -160,7 +159,7 @@ private RequestSpecification client() {
160159

161160
private <R> R deserializeInternal(HttpResponse response, DeserializationMode mode) {
162161
try {
163-
if (mode==DeserializationMode.LIST) {
162+
if (mode == DeserializationMode.LIST) {
164163
List<THttpEntity> entities = objectConverter.fromStringToList(response.getBody(), entityType);
165164
entities.forEach(entity -> entity.setResponse(response.getResponse()));
166165
return (R)entities;
@@ -178,7 +177,7 @@ private Response broadcastRequest(Supplier<Response> responseSupplier) {
178177
try {
179178
SENDING_REQUEST.broadcast(new HttpRequestEventArgs(requestContext));
180179
Response response = responseSupplier.get();
181-
REQUEST_SEND.broadcast(new ResponseProcessingEventArgs(response));
180+
REQUEST_SENT.broadcast(new ResponseProcessingEventArgs(response));
182181
return response;
183182
} finally {
184183
restoreRequestContext();

0 commit comments

Comments
 (0)