Skip to content
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
.idea
.gradle
.classpath
.project
.settings
bin/
9 changes: 9 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ repositories {
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'

compileOnly 'org.projectlombok:lombok:1.18.30'
annotationProcessor 'org.projectlombok:lombok:1.18.30'

testCompileOnly 'org.projectlombok:lombok:1.18.30'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.30'

implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.9'
implementation group: 'org.slf4j', name: 'slf4j-simple', version: '2.0.9'
}

test {
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/task_4/AvengersBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package task_4;

import lombok.extern.slf4j.Slf4j;
import task_4.exceptions.SecurityBreachException;
import task_4.exceptions.UnauthorizedAccessAttemptException;

@Slf4j
public class AvengersBase {

public void enterBase(Hero hero) {

if (hero == null) {
throw new IllegalArgumentException("Null args");
}

log.info("Start to check hero's credentials");
if (hero.getName().equals("Loki")) {
log.warn("Loki here!");
throw new SecurityBreachException("Loki try to enter!");
}

log.info("{} entered successful", hero.getName());

}

public String getSecretDocuments(Hero hero, String documentName) {

if (hero == null || documentName == null || documentName.isBlank()) {
throw new IllegalArgumentException("Null or blank args");
}

log.info("Start to check access to the document");

if (hero.getName().equals("Loki")) {
log.warn("Loki's access attempt to {}", documentName);
throw new UnauthorizedAccessAttemptException("Loki try to enter!");
}

log.info("{} got access to {}", hero.getName(), documentName);

return documentName;
}
}
13 changes: 13 additions & 0 deletions src/main/java/task_4/Hero.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package task_4;

public class Hero {
private final String name;

public Hero(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
65 changes: 65 additions & 0 deletions src/main/java/task_4/Task4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package task_4;

import lombok.extern.slf4j.Slf4j;
import task_4.exceptions.SecurityBreachException;
import task_4.exceptions.UnauthorizedAccessAttemptException;

@Slf4j
public class Task4 {

public static void main(String[] args) {

Hero hero1 = new Hero("Loki");
Hero hero2 = new Hero("Grut");
Hero heroNull = null;

String doc = "Secret document";
String nullDoc = null;
String blankDoc = " ";

AvengersBase avengersBase = new AvengersBase();

try {
avengersBase.enterBase(heroNull);
} catch (IllegalArgumentException e) {
log.error(e.getMessage() + " during invoke enterBase()");
}

try {
avengersBase.enterBase(hero1);
} catch (SecurityBreachException e) {
log.error(e.getMessage() + " during invoke enterBase()");
}

avengersBase.enterBase(hero2);


try {
avengersBase.getSecretDocuments(heroNull, doc);
} catch (IllegalArgumentException e) {
log.error(e.getMessage() + " during invoke getSecretDocuments()");
}

try {
avengersBase.getSecretDocuments(hero1, nullDoc);
} catch (IllegalArgumentException e) {
log.error(e.getMessage() + " during invoke getSecretDocuments()");
}

try {
avengersBase.getSecretDocuments(hero1, blankDoc);
} catch (IllegalArgumentException e) {
log.error(e.getMessage() + " during invoke getSecretDocuments()");
}

try {
avengersBase.getSecretDocuments(hero1, doc);
} catch (UnauthorizedAccessAttemptException e) {
log.error(e.getMessage() + " during invoke getSecretDocuments()");
}

avengersBase.getSecretDocuments(hero2, doc);

}

}
9 changes: 9 additions & 0 deletions src/main/java/task_4/exceptions/SecurityBreachException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package task_4.exceptions;

public class SecurityBreachException extends RuntimeException {

public SecurityBreachException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package task_4.exceptions;

public class UnauthorizedAccessAttemptException extends RuntimeException {

public UnauthorizedAccessAttemptException(String message) {
super(message);
}

}