Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backend] Add Spotless #1634

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ steps:
MINIO_PORT: 9000
commands:
- mvn clean install -q -DskipTests
- mvn spotless:check
- cd openbas-api
- mvn test -q
- cd ../openbas-framework
Expand Down
6 changes: 3 additions & 3 deletions openbas-api/src/main/java/io/openbas/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@SpringBootApplication
public class App {

public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Custom annotation used for logging execution time of any method
*/
/** Custom annotation used for logging execution time of any method */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogExecutionTime {

}
public @interface LogExecutionTime {}
19 changes: 12 additions & 7 deletions openbas-api/src/main/java/io/openbas/aop/LoggingAspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class LoggingAspect {
private static final long EXECUTION_TIME_THRESHOLD = 500;

/**
* This method uses Around advice which ensures that an advice can run before and after the method execution, to and
* log the execution time of the method This advice will be applied to all the method which are annotate with the
* annotation @LogExecutionTime
* This method uses Around advice which ensures that an advice can run before and after the method
* execution, to and log the execution time of the method This advice will be applied to all the
* method which are annotate with the annotation @LogExecutionTime
*/
@Around("@annotation(io.openbas.aop.LogExecutionTime)")
public Object methodTimeLogger(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Expand All @@ -45,10 +45,15 @@ public Object methodTimeLogger(ProceedingJoinPoint proceedingJoinPoint) throws T

if (executionTime > EXECUTION_TIME_THRESHOLD) {
logger.warn(
"Execution of " + className + "." + methodName + " took "
+ executionTime + " ms, which exceeds the threshold of " + EXECUTION_TIME_THRESHOLD
+ " ms"
);
"Execution of "
+ className
+ "."
+ methodName
+ " took "
+ executionTime
+ " ms, which exceeds the threshold of "
+ EXECUTION_TIME_THRESHOLD
+ " ms");
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import io.openbas.collectors.expectations_expiration_manager.service.ExpectationsExpirationManagerService;
import io.openbas.integrations.CollectorService;
import jakarta.annotation.PostConstruct;
import java.time.Duration;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Service;

import java.time.Duration;

@RequiredArgsConstructor
@Service
public class ExpectationsExpirationManagerCollector {
Expand All @@ -22,9 +21,10 @@ public class ExpectationsExpirationManagerCollector {
@PostConstruct
public void init() {
if (this.config.isEnable()) {
ExpectationsExpirationManagerJob job = new ExpectationsExpirationManagerJob(this.collectorService, this.config, this.fakeDetectorService);
ExpectationsExpirationManagerJob job =
new ExpectationsExpirationManagerJob(
this.collectorService, this.config, this.fakeDetectorService);
this.taskScheduler.scheduleAtFixedRate(job, Duration.ofSeconds(this.config.getInterval()));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import io.openbas.collectors.expectations_expiration_manager.config.ExpectationsExpirationManagerConfig;
import io.openbas.collectors.expectations_expiration_manager.service.ExpectationsExpirationManagerService;
import io.openbas.integrations.CollectorService;
import java.util.logging.Level;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.logging.Level;

@Service
@Log
public class ExpectationsExpirationManagerJob implements Runnable {
Expand All @@ -17,10 +16,17 @@ public class ExpectationsExpirationManagerJob implements Runnable {
private final ExpectationsExpirationManagerService fakeDetectorService;

@Autowired
public ExpectationsExpirationManagerJob(CollectorService collectorService, ExpectationsExpirationManagerConfig config, ExpectationsExpirationManagerService fakeDetectorService) {
public ExpectationsExpirationManagerJob(
CollectorService collectorService,
ExpectationsExpirationManagerConfig config,
ExpectationsExpirationManagerService fakeDetectorService) {
this.fakeDetectorService = fakeDetectorService;
try {
collectorService.register(config.getId(), FAKE_DETECTOR_COLLECTOR_TYPE, FAKE_DETECTOR_COLLECTOR_NAME, getClass().getResourceAsStream("/img/icon-fake-detector.png"));
collectorService.register(
config.getId(),
FAKE_DETECTOR_COLLECTOR_TYPE,
FAKE_DETECTOR_COLLECTOR_NAME,
getClass().getResourceAsStream("/img/icon-fake-detector.png"));
} catch (Exception e) {
log.log(Level.SEVERE, "Error creating expectations expiration manager ");
}
Expand All @@ -35,5 +41,4 @@ public void run() {
log.log(Level.SEVERE, "Error running expectations expiration manager service", e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ public int getExpirationTimeInMinute() {
return this.expirationTime / 60;
}
}

Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package io.openbas.collectors.expectations_expiration_manager.service;

import static io.openbas.collectors.expectations_expiration_manager.config.ExpectationsExpirationManagerConfig.PRODUCT_NAME;
import static io.openbas.collectors.expectations_expiration_manager.utils.ExpectationUtils.computeFailedMessage;
import static io.openbas.collectors.expectations_expiration_manager.utils.ExpectationUtils.isExpired;

import io.openbas.collectors.expectations_expiration_manager.config.ExpectationsExpirationManagerConfig;
import io.openbas.database.model.InjectExpectation;
import io.openbas.inject_expectation.InjectExpectationService;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static io.openbas.collectors.expectations_expiration_manager.config.ExpectationsExpirationManagerConfig.PRODUCT_NAME;
import static io.openbas.collectors.expectations_expiration_manager.utils.ExpectationUtils.computeFailedMessage;
import static io.openbas.collectors.expectations_expiration_manager.utils.ExpectationUtils.isExpired;

@RequiredArgsConstructor
@Service
@Log
Expand All @@ -23,7 +22,6 @@
private final InjectExpectationService injectExpectationService;
private final ExpectationsExpirationManagerConfig config;


@Transactional(rollbackFor = Exception.class)
public void computeExpectations() {
List<InjectExpectation> expectations = this.injectExpectationService.expectationsNotFill();
Expand All @@ -38,57 +36,49 @@

private void computeExpectations(@NotNull final List<InjectExpectation> expectations) {
List<InjectExpectation> expectationAssets = expectations.stream().toList();
expectationAssets.forEach((expectation) -> {
if (isExpired(expectation)) {
String result = computeFailedMessage(expectation.getType());
this.injectExpectationService.computeExpectation(
expectation,
this.config.getId(),
"collector",
PRODUCT_NAME,
result,
false
);
}

});
expectationAssets.forEach(

Check warning on line 39 in openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java#L39

Added line #L39 was not covered by tests
(expectation) -> {
if (isExpired(expectation)) {
String result = computeFailedMessage(expectation.getType());
this.injectExpectationService.computeExpectation(
expectation, this.config.getId(), "collector", PRODUCT_NAME, result, false);

Check warning on line 44 in openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java#L42-L44

Added lines #L42 - L44 were not covered by tests
}
});

Check warning on line 46 in openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java#L46

Added line #L46 was not covered by tests
}

private void computeExpectationsForAssets(@NotNull final List<InjectExpectation> expectations) {
List<InjectExpectation> expectationAssets = expectations.stream().filter(e -> e.getAsset() != null).toList();
expectationAssets.forEach((expectation) -> {
if (isExpired(expectation)) {
String result = computeFailedMessage(expectation.getType());
this.injectExpectationService.computeExpectation(
expectation,
this.config.getId(),
"collector",
PRODUCT_NAME,
result,
false
);
}

});
List<InjectExpectation> expectationAssets =

Check warning on line 50 in openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java#L50

Added line #L50 was not covered by tests
expectations.stream().filter(e -> e.getAsset() != null).toList();
expectationAssets.forEach(

Check warning on line 52 in openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java#L52

Added line #L52 was not covered by tests
(expectation) -> {
if (isExpired(expectation)) {
String result = computeFailedMessage(expectation.getType());
this.injectExpectationService.computeExpectation(
expectation, this.config.getId(), "collector", PRODUCT_NAME, result, false);

Check warning on line 57 in openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java#L55-L57

Added lines #L55 - L57 were not covered by tests
}
});

Check warning on line 59 in openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java#L59

Added line #L59 was not covered by tests
}

private void computeExpectationsForAssetGroups(@NotNull final List<InjectExpectation> expectations) {
List<InjectExpectation> expectationAssetGroups = expectations.stream().filter(e -> e.getAssetGroup() != null)
.toList();
expectationAssetGroups.forEach((expectationAssetGroup -> {
List<InjectExpectation> expectationAssets = this.injectExpectationService.expectationsForAssets(
expectationAssetGroup.getInject(), expectationAssetGroup.getAssetGroup(), expectationAssetGroup.getType()
);
// Every expectation assets are filled
if (expectationAssets.stream().noneMatch(e -> e.getResults().isEmpty())) {
this.injectExpectationService.computeExpectationGroup(
expectationAssetGroup,
expectationAssets,
this.config.getId(),
"collector",
PRODUCT_NAME
);
}
}));
private void computeExpectationsForAssetGroups(
@NotNull final List<InjectExpectation> expectations) {
List<InjectExpectation> expectationAssetGroups =

Check warning on line 64 in openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java#L64

Added line #L64 was not covered by tests
expectations.stream().filter(e -> e.getAssetGroup() != null).toList();
expectationAssetGroups.forEach(

Check warning on line 66 in openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java#L66

Added line #L66 was not covered by tests
(expectationAssetGroup -> {
List<InjectExpectation> expectationAssets =
this.injectExpectationService.expectationsForAssets(
expectationAssetGroup.getInject(),
expectationAssetGroup.getAssetGroup(),
expectationAssetGroup.getType());

Check warning on line 72 in openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java#L68-L72

Added lines #L68 - L72 were not covered by tests
// Every expectation assets are filled
if (expectationAssets.stream().noneMatch(e -> e.getResults().isEmpty())) {
this.injectExpectationService.computeExpectationGroup(

Check warning on line 75 in openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java#L75

Added line #L75 was not covered by tests
expectationAssetGroup,
expectationAssets,
this.config.getId(),

Check warning on line 78 in openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java#L78

Added line #L78 was not covered by tests
"collector",
PRODUCT_NAME);
}
}));

Check warning on line 82 in openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/service/ExpectationsExpirationManagerService.java#L82

Added line #L82 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
package io.openbas.collectors.expectations_expiration_manager.utils;

import static io.openbas.database.model.InjectExpectation.EXPECTATION_TYPE.DETECTION;
import static io.openbas.database.model.InjectExpectation.EXPECTATION_TYPE.PREVENTION;

import io.openbas.database.model.InjectExpectation;
import io.openbas.database.model.InjectExpectation.EXPECTATION_TYPE;
import jakarta.validation.constraints.NotNull;

import java.time.Instant;
import java.time.temporal.ChronoUnit;

import static io.openbas.database.model.InjectExpectation.EXPECTATION_TYPE.DETECTION;
import static io.openbas.database.model.InjectExpectation.EXPECTATION_TYPE.PREVENTION;

public class ExpectationUtils {

private ExpectationUtils() {

}
private ExpectationUtils() {}

public static boolean isExpired(@NotNull final InjectExpectation expectation) {
Instant expirationTime = Instant.now().minus(expectation.getExpirationTime() / 60, ChronoUnit.MINUTES);
Instant expirationTime =
Instant.now().minus(expectation.getExpirationTime() / 60, ChronoUnit.MINUTES);

Check warning on line 18 in openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/utils/ExpectationUtils.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/collectors/expectations_expiration_manager/utils/ExpectationUtils.java#L18

Added line #L18 was not covered by tests
return expectation.getCreatedAt().isBefore(expirationTime);
}

public static String computeFailedMessage(@NotNull final EXPECTATION_TYPE expectationType) {
return DETECTION.equals(expectationType)
? "Not Detected"
: PREVENTION.equals(expectationType)
? "Not Prevented"
: "FAILED";
: PREVENTION.equals(expectationType) ? "Not Prevented" : "FAILED";
}

}
32 changes: 18 additions & 14 deletions openbas-api/src/main/java/io/openbas/config/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
public class AppConfig {

// Validations
public final static String EMPTY_MESSAGE = "This list cannot be empty.";
public final static String MANDATORY_MESSAGE = "This value should not be blank.";
public final static String NOW_FUTURE_MESSAGE = "This date must be now or in the future.";
public final static String EMAIL_FORMAT = "This field must be a valid email.";
public final static String PHONE_FORMAT = "This field must start with '+' character and country identifier.";
public static final String EMPTY_MESSAGE = "This list cannot be empty.";
public static final String MANDATORY_MESSAGE = "This value should not be blank.";
public static final String NOW_FUTURE_MESSAGE = "This date must be now or in the future.";
public static final String EMAIL_FORMAT = "This field must be a valid email.";
public static final String PHONE_FORMAT =
"This field must start with '+' character and country identifier.";

@Resource
private OpenBASConfig openBASConfig;
@Resource private OpenBASConfig openBASConfig;

@Bean
ObjectMapper openBASJsonMapper() {
Expand All @@ -37,12 +37,16 @@ ObjectMapper openBASJsonMapper() {
@Bean
public OpenAPI openBASOpenAPI() {
return new OpenAPI()
.info(new Info().title("OpenBAS API")
.description("Software under open source licence designed to plan and conduct exercises")
.version(this.openBASConfig.getVersion())
.license(new License().name("Apache 2.0").url("https://filigran.io//")))
.externalDocs(new ExternalDocumentation()
.description("OpenBAS documentation")
.url("https://docs.openbas.io/"));
.info(
new Info()
.title("OpenBAS API")
.description(
"Software under open source licence designed to plan and conduct exercises")
.version(this.openBASConfig.getVersion())
.license(new License().name("Apache 2.0").url("https://filigran.io//")))
.externalDocs(
new ExternalDocumentation()
.description("OpenBAS documentation")
.url("https://docs.openbas.io/"));
}
}
Loading