This commit is contained in:
Roni Dover 2024-06-12 17:08:36 -07:00
parent d88a6e1af5
commit d927a42681
4 changed files with 57 additions and 44 deletions

View file

@ -1,9 +1,11 @@
package org.springframework.samples.petclinic.domain;
import io.opentelemetry.instrumentation.annotations.WithSpan;
public class DomainValidationService {
// Main method for domain validation logic
@WithSpan
public boolean validateDomainLogic(String usr) {
String domain = normalizeDomainFormat("example.com");
@ -11,14 +13,17 @@ public class DomainValidationService {
if (!containsRestrictedWords(domain) && isValidDomainSuffix(domain)) {
if (isDomainAvailable(domain)) {
saveValidationResults(domain, true, true);
} else {
}
else {
saveValidationResults(domain, true, false);
}
} else {
}
else {
flagDomainForReview(domain);
saveValidationResults(domain, false, false);
}
} else {
}
else {
saveValidationResults(domain, false, false);
}
@ -28,7 +33,8 @@ public class DomainValidationService {
// Fake method to check if a domain is valid
private boolean isValidDomain(String domain) {
return domain.matches("^[a-zA-Z0-9.-]+$") && domain.length() >= 3 && !domain.startsWith("-") && !domain.endsWith("-");
return domain.matches("^[a-zA-Z0-9.-]+$") && domain.length() >= 3 && !domain.startsWith("-")
&& !domain.endsWith("-");
}
// Fake method to check if a domain is available
@ -38,7 +44,7 @@ public class DomainValidationService {
// Fake method to check if domain contains restricted words
private boolean containsRestrictedWords(String domain) {
String[] restrictedWords = {"badword", "restricted"};
String[] restrictedWords = { "badword", "restricted" };
for (String word : restrictedWords) {
if (domain.contains(word)) {
return true;
@ -49,25 +55,30 @@ public class DomainValidationService {
// Fake method to simulate saving domain validation results
private void saveValidationResults(String domain, boolean isValid, boolean isAvailable) {
// Simulated save operation: Creating a fake result object and printing its contents
// Simulated save operation: Creating a fake result object and printing its
// contents
ValidationResult result = new ValidationResult(domain, isValid, isAvailable);
System.out.println("Saving results: " + result);
}
// Fake method to flag domain for manual review
private void flagDomainForReview(String domain) {
// Simulated flagging operation: Creating a fake review request and printing its contents
// Simulated flagging operation: Creating a fake review request and printing its
// contents
ReviewRequest request = new ReviewRequest(domain, "Contains restricted words or invalid suffix");
System.out.println("Flagging for review: " + request);
}
// Fake method to notify admin about the validation results
private void notifyAdmin(String domain) {
// Simulated notification: Creating a fake notification object and printing its contents
Notification notification = new Notification("admin@example.com", "Domain Validation Completed", "Validation completed for domain: " + domain);
// Simulated notification: Creating a fake notification object and printing its
// contents
Notification notification = new Notification("admin@example.com", "Domain Validation Completed",
"Validation completed for domain: " + domain);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("Notifying admin: " + notification);
@ -75,7 +86,7 @@ public class DomainValidationService {
// Fake method to check if the domain is reserved
private boolean isReservedDomain(String domain) {
String[] reservedDomains = {"example.com", "localhost"};
String[] reservedDomains = { "example.com", "localhost" };
for (String reserved : reservedDomains) {
if (domain.equalsIgnoreCase(reserved)) {
return true;
@ -86,7 +97,7 @@ public class DomainValidationService {
// Fake method to check if the domain is a premium domain
private boolean isPremiumDomain(String domain) {
String[] premiumDomains = {"premium.com", "exclusive.com"};
String[] premiumDomains = { "premium.com", "exclusive.com" };
for (String premium : premiumDomains) {
if (domain.equalsIgnoreCase(premium)) {
return true;
@ -102,7 +113,7 @@ public class DomainValidationService {
// Fake method to simulate domain suffix validation
private boolean isValidDomainSuffix(String domain) {
String[] validSuffixes = {".com", ".net", ".org"};
String[] validSuffixes = { ".com", ".net", ".org" };
for (String suffix : validSuffixes) {
if (domain.endsWith(suffix)) {
return true;
@ -113,8 +124,11 @@ public class DomainValidationService {
// Fake class to represent validation results
private class ValidationResult {
String domain;
boolean isValid;
boolean isAvailable;
ValidationResult(String domain, boolean isValid, boolean isAvailable) {
@ -125,13 +139,17 @@ public class DomainValidationService {
@Override
public String toString() {
return "ValidationResult{domain='" + domain + "', isValid=" + isValid + ", isAvailable=" + isAvailable + "}";
return "ValidationResult{domain='" + domain + "', isValid=" + isValid + ", isAvailable=" + isAvailable
+ "}";
}
}
// Fake class to represent a review request
private class ReviewRequest {
String domain;
String reason;
ReviewRequest(String domain, String reason) {
@ -143,12 +161,16 @@ public class DomainValidationService {
public String toString() {
return "ReviewRequest{domain='" + domain + "', reason='" + reason + "'}";
}
}
// Fake class to represent a notification
private class Notification {
String recipient;
String subject;
String message;
Notification(String recipient, String subject, String message) {
@ -161,5 +183,7 @@ public class DomainValidationService {
public String toString() {
return "Notification{recipient='" + recipient + "', subject='" + subject + "', message='" + message + "'}";
}
}
}

View file

@ -15,6 +15,7 @@ public class OwnerValidation {
private UserValidationService usrValSvc;
private DomainValidationService domainValidation;
private PasswordUtils pwdUtils;
private Tracer otelTracer;
@ -28,7 +29,7 @@ public class OwnerValidation {
this.roleSvc = new RoleService();
this.otelTracer = otelTracer;
this.usrValSvc = new UserValidationService();
this.domainValidation=new DomainValidationService();
this.domainValidation = new DomainValidationService();
this.twoFASvc = new TwoFactorAuthenticationService();
}

View file

@ -15,4 +15,5 @@ public class UserValidationService {
}
return true;
}
}

View file

@ -93,7 +93,6 @@ class OwnerControllerTests {
@Test
void shouldProvideOwnerVaccinationDate() {
for (int i=0; i<10;i++){
Owner owner = CreateOwner();
var ownerLinkMatcher = String.format("**.findAll { node -> node.@href=='/owners/%s'}", owner.getId());
@ -106,10 +105,6 @@ class OwnerControllerTests {
.statusCode(200)
.body(ownerLinkMatcher, Matchers.notNullValue());
}
for (int i=0; i<50;i++) {
given().contentType(ContentType.JSON)
.when()
.get("/owners")
@ -117,14 +112,6 @@ class OwnerControllerTests {
.contentType(ContentType.HTML)
.statusCode(200);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
// assertThat(false).isTrue();
}