This commit is contained in:
32
.gitea/workflows/sonar.yml
Normal file
32
.gitea/workflows/sonar.yml
Normal file
@@ -0,0 +1,32 @@
|
||||
name: SonarQube Scan
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- '**'
|
||||
pull_request:
|
||||
branches:
|
||||
- '**'
|
||||
|
||||
jobs:
|
||||
sonarqube:
|
||||
name: SonarQube Trigger
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Download SonarQube Scanner
|
||||
run: |
|
||||
curl -sSLo sonar-scanner.zip https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-5.0.1.3006-linux.zip
|
||||
unzip sonar-scanner.zip
|
||||
|
||||
- name: Run SonarQube Scan
|
||||
run: |
|
||||
./sonar-scanner-*/bin/sonar-scanner \
|
||||
-Dsonar.projectKey=tp1-iaavancee \
|
||||
-Dsonar.sources=. \
|
||||
-Dsonar.host.url=${{ secrets.SONARQUBE_HOST }} \
|
||||
-Dsonar.login=${{ secrets.SONARQUBE_TOKEN }}
|
||||
29
.gitignore
vendored
Normal file
29
.gitignore
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
### IntelliJ IDEA ###
|
||||
out/
|
||||
!**/src/main/**/out/
|
||||
!**/src/test/**/out/
|
||||
|
||||
### Eclipse ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
bin/
|
||||
!**/src/main/**/bin/
|
||||
!**/src/test/**/bin/
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
||||
31
src/Application.java
Normal file
31
src/Application.java
Normal file
@@ -0,0 +1,31 @@
|
||||
import entities.User;
|
||||
import utils.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static utils.NotificationType.*;
|
||||
|
||||
public class Application {
|
||||
private final NotificationManager notificationManager;
|
||||
|
||||
public Application(NotificationManager notificationManager) {
|
||||
this.notificationManager = notificationManager;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
NotificationType[] typesThomas = {Sms, Mail, Push};
|
||||
NotificationType[] typesAlexandre = {Sms, Mail};
|
||||
|
||||
SendNotificationStrategy secured = new SecureSendNotificationStrategy(123);
|
||||
DifferedSendNotificationStrategy differed = new DifferedSendNotificationStrategy(300);
|
||||
|
||||
var thomas = new User(0, "Thomas", Arrays.asList(typesThomas), secured);
|
||||
var alexandre = new User(1, "Alexandre", Arrays.asList(typesAlexandre), differed);
|
||||
|
||||
notificationManager.getObservable().addSubscribers(thomas);
|
||||
notificationManager.getObservable().addSubscribers(alexandre);
|
||||
|
||||
notificationManager.getObservable().sendNotifications("C# > Java (mais Rust c'est le goat)");
|
||||
|
||||
}
|
||||
}
|
||||
8
src/Main.java
Normal file
8
src/Main.java
Normal file
@@ -0,0 +1,8 @@
|
||||
import utils.NotificationManager;
|
||||
|
||||
void main(String[] args) {
|
||||
final var notificationManager = NotificationManager.getInstance();
|
||||
final var app = new Application(notificationManager);
|
||||
|
||||
app.run();
|
||||
}
|
||||
16
src/entities/MailNotification.java
Normal file
16
src/entities/MailNotification.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package entities;
|
||||
|
||||
import utils.SendNotificationStrategy;
|
||||
|
||||
public class MailNotification extends Notification {
|
||||
public MailNotification(String content, User user, SendNotificationStrategy strategy) {
|
||||
super(content, user, strategy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send() {
|
||||
if (isSendable())
|
||||
strategy.send(user.id() + " Mail: " + user.name() + " " + content);
|
||||
}
|
||||
|
||||
}
|
||||
24
src/entities/Notification.java
Normal file
24
src/entities/Notification.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package entities;
|
||||
|
||||
import utils.NotificationState;
|
||||
import utils.SendNotificationStrategy;
|
||||
|
||||
public abstract class Notification {
|
||||
protected String content;
|
||||
protected User user;
|
||||
protected NotificationState state;
|
||||
protected SendNotificationStrategy strategy;
|
||||
|
||||
public Notification(String content, User user, SendNotificationStrategy strategy) {
|
||||
this.content = content;
|
||||
this.user = user;
|
||||
this.state = NotificationState.PENDING;
|
||||
this.strategy = strategy;
|
||||
}
|
||||
|
||||
protected boolean isSendable() {
|
||||
return this.state != NotificationState.SENT && this.state != NotificationState.READ;
|
||||
}
|
||||
|
||||
public abstract void send();
|
||||
}
|
||||
15
src/entities/PushNotification.java
Normal file
15
src/entities/PushNotification.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package entities;
|
||||
|
||||
import utils.SendNotificationStrategy;
|
||||
|
||||
public class PushNotification extends Notification {
|
||||
public PushNotification(String content, User user, SendNotificationStrategy strategy) {
|
||||
super(content, user, strategy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send() {
|
||||
if (isSendable())
|
||||
strategy.send(user.id() + " Push: " + user.name() + " " + content);
|
||||
}
|
||||
}
|
||||
16
src/entities/SmsNotification.java
Normal file
16
src/entities/SmsNotification.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package entities;
|
||||
|
||||
import utils.SendNotificationStrategy;
|
||||
|
||||
public class SmsNotification extends Notification {
|
||||
|
||||
public SmsNotification(String content, User user, SendNotificationStrategy strategy) {
|
||||
super(content, user, strategy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send() {
|
||||
if (isSendable())
|
||||
strategy.send(user.id() + " Sms: " + user.name() + " " + content);
|
||||
}
|
||||
}
|
||||
9
src/entities/User.java
Normal file
9
src/entities/User.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package entities;
|
||||
|
||||
import utils.NotificationType;
|
||||
import utils.SendNotificationStrategy;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record User(int id, String name, List<NotificationType> notificationsTypes, SendNotificationStrategy chosenNotificationStrategy) {
|
||||
}
|
||||
21
src/utils/DifferedSendNotificationStrategy.java
Normal file
21
src/utils/DifferedSendNotificationStrategy.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package utils;
|
||||
|
||||
public class DifferedSendNotificationStrategy implements SendNotificationStrategy {
|
||||
private int delay;
|
||||
|
||||
public DifferedSendNotificationStrategy(int delay) {
|
||||
this.delay = delay;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NotificationState send(String message) {
|
||||
try {
|
||||
Thread.sleep(delay);
|
||||
} catch (InterruptedException e) {
|
||||
System.err.println("Cannot wait for differed strategy");
|
||||
return NotificationState.KO;
|
||||
}
|
||||
System.out.println(message);
|
||||
return NotificationState.SENT;
|
||||
}
|
||||
}
|
||||
9
src/utils/FastSendNotificationStrategy.java
Normal file
9
src/utils/FastSendNotificationStrategy.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package utils;
|
||||
|
||||
public class FastSendNotificationStrategy implements SendNotificationStrategy {
|
||||
@Override
|
||||
public NotificationState send(String message) {
|
||||
System.out.println(message);
|
||||
return NotificationState.SENT;
|
||||
}
|
||||
}
|
||||
5
src/utils/NotificationFacade.java
Normal file
5
src/utils/NotificationFacade.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package utils;
|
||||
|
||||
public class NotificationFacade {
|
||||
|
||||
}
|
||||
13
src/utils/NotificationFactory.java
Normal file
13
src/utils/NotificationFactory.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package utils;
|
||||
|
||||
import entities.*;
|
||||
|
||||
public class NotificationFactory {
|
||||
public Notification createNotification(NotificationType type, String content, User user, SendNotificationStrategy strategy) {
|
||||
return switch (type) {
|
||||
case Sms -> new SmsNotification(content, user, strategy);
|
||||
case Mail -> new MailNotification(content, user, strategy);
|
||||
case Push -> new PushNotification(content, user, strategy);
|
||||
};
|
||||
}
|
||||
}
|
||||
22
src/utils/NotificationManager.java
Normal file
22
src/utils/NotificationManager.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package utils;
|
||||
|
||||
|
||||
public class NotificationManager {
|
||||
private static NotificationManager INSTANCE;
|
||||
private final NotificationObservable observable;
|
||||
|
||||
private NotificationManager() {
|
||||
this.observable = new NotificationObservable();
|
||||
}
|
||||
|
||||
public static NotificationManager getInstance() {
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new NotificationManager();
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public NotificationObservable getObservable() {
|
||||
return this.observable;
|
||||
}
|
||||
}
|
||||
29
src/utils/NotificationObservable.java
Normal file
29
src/utils/NotificationObservable.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package utils;
|
||||
|
||||
import entities.User;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class NotificationObservable {
|
||||
private final List<User> subscribers;
|
||||
|
||||
public NotificationObservable() {
|
||||
this.subscribers = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addSubscribers(User subscriber) {
|
||||
this.subscribers.add(subscriber);
|
||||
}
|
||||
|
||||
public void sendNotifications(String content) {
|
||||
var factory = new NotificationFactory();
|
||||
|
||||
for (final var subscriber : this.subscribers) {
|
||||
for (final var notificationType : subscriber.notificationsTypes()) {
|
||||
var notification = factory.createNotification(notificationType, content, subscriber, subscriber.chosenNotificationStrategy());
|
||||
notification.send();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
8
src/utils/NotificationState.java
Normal file
8
src/utils/NotificationState.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package utils;
|
||||
|
||||
public enum NotificationState {
|
||||
PENDING,
|
||||
SENT,
|
||||
KO,
|
||||
READ,
|
||||
}
|
||||
7
src/utils/NotificationType.java
Normal file
7
src/utils/NotificationType.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package utils;
|
||||
|
||||
public enum NotificationType {
|
||||
Sms,
|
||||
Mail,
|
||||
Push
|
||||
}
|
||||
17
src/utils/PriorityDecorator.java
Normal file
17
src/utils/PriorityDecorator.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package utils;
|
||||
|
||||
public class PriorityDecorator {
|
||||
private boolean isPriority;
|
||||
|
||||
public PriorityDecorator(boolean isPriority) {
|
||||
this.isPriority = isPriority;
|
||||
}
|
||||
|
||||
public boolean isPriority() {
|
||||
return isPriority;
|
||||
}
|
||||
|
||||
public void setPriority(boolean priority) {
|
||||
isPriority = priority;
|
||||
}
|
||||
}
|
||||
17
src/utils/ReceiptDecorator.java
Normal file
17
src/utils/ReceiptDecorator.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package utils;
|
||||
|
||||
public class ReceiptDecorator {
|
||||
boolean received;
|
||||
|
||||
public ReceiptDecorator() {
|
||||
this.received = false;
|
||||
}
|
||||
|
||||
public boolean isReceived() {
|
||||
return received;
|
||||
}
|
||||
|
||||
public void setReceived(boolean received) {
|
||||
this.received = received;
|
||||
}
|
||||
}
|
||||
15
src/utils/SecureSendNotificationStrategy.java
Normal file
15
src/utils/SecureSendNotificationStrategy.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package utils;
|
||||
|
||||
public class SecureSendNotificationStrategy implements SendNotificationStrategy {
|
||||
private int key;
|
||||
|
||||
public SecureSendNotificationStrategy(int key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NotificationState send(String message) {
|
||||
System.out.println(key+"Flcjsdjknesdlkgjsilkqzfjazio"+message+"kjhsdiuhisehifsdhfiuhsuidhuifhjsid");
|
||||
return NotificationState.SENT;
|
||||
}
|
||||
}
|
||||
5
src/utils/SendNotificationStrategy.java
Normal file
5
src/utils/SendNotificationStrategy.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package utils;
|
||||
|
||||
public interface SendNotificationStrategy {
|
||||
NotificationState send(String message);
|
||||
}
|
||||
19
src/utils/TimeStampDecorator.java
Normal file
19
src/utils/TimeStampDecorator.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package utils;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class TimeStampDecorator {
|
||||
private Date time;
|
||||
|
||||
public TimeStampDecorator(Date time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public Date getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(Date time) {
|
||||
this.time = time;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user