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; package org.springframework.samples.petclinic.domain;
import io.opentelemetry.instrumentation.annotations.WithSpan;
public class DomainValidationService { public class DomainValidationService {
// Main method for domain validation logic // Main method for domain validation logic
@WithSpan
public boolean validateDomainLogic(String usr) { public boolean validateDomainLogic(String usr) {
String domain = normalizeDomainFormat("example.com"); String domain = normalizeDomainFormat("example.com");
@ -11,14 +13,17 @@ public class DomainValidationService {
if (!containsRestrictedWords(domain) && isValidDomainSuffix(domain)) { if (!containsRestrictedWords(domain) && isValidDomainSuffix(domain)) {
if (isDomainAvailable(domain)) { if (isDomainAvailable(domain)) {
saveValidationResults(domain, true, true); saveValidationResults(domain, true, true);
} else { }
else {
saveValidationResults(domain, true, false); saveValidationResults(domain, true, false);
} }
} else { }
else {
flagDomainForReview(domain); flagDomainForReview(domain);
saveValidationResults(domain, false, false); saveValidationResults(domain, false, false);
} }
} else { }
else {
saveValidationResults(domain, false, false); saveValidationResults(domain, false, false);
} }
@ -28,7 +33,8 @@ public class DomainValidationService {
// Fake method to check if a domain is valid // Fake method to check if a domain is valid
private boolean isValidDomain(String domain) { 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 // Fake method to check if a domain is available
@ -49,25 +55,30 @@ public class DomainValidationService {
// Fake method to simulate saving domain validation results // Fake method to simulate saving domain validation results
private void saveValidationResults(String domain, boolean isValid, boolean isAvailable) { 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); ValidationResult result = new ValidationResult(domain, isValid, isAvailable);
System.out.println("Saving results: " + result); System.out.println("Saving results: " + result);
} }
// Fake method to flag domain for manual review // Fake method to flag domain for manual review
private void flagDomainForReview(String domain) { 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"); ReviewRequest request = new ReviewRequest(domain, "Contains restricted words or invalid suffix");
System.out.println("Flagging for review: " + request); System.out.println("Flagging for review: " + request);
} }
// Fake method to notify admin about the validation results // Fake method to notify admin about the validation results
private void notifyAdmin(String domain) { private void notifyAdmin(String domain) {
// Simulated notification: Creating a fake notification object and printing its contents // Simulated notification: Creating a fake notification object and printing its
Notification notification = new Notification("admin@example.com", "Domain Validation Completed", "Validation completed for domain: " + domain); // contents
Notification notification = new Notification("admin@example.com", "Domain Validation Completed",
"Validation completed for domain: " + domain);
try { try {
Thread.sleep(2000); Thread.sleep(2000);
} catch (InterruptedException e) { }
catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
System.out.println("Notifying admin: " + notification); System.out.println("Notifying admin: " + notification);
@ -113,8 +124,11 @@ public class DomainValidationService {
// Fake class to represent validation results // Fake class to represent validation results
private class ValidationResult { private class ValidationResult {
String domain; String domain;
boolean isValid; boolean isValid;
boolean isAvailable; boolean isAvailable;
ValidationResult(String domain, boolean isValid, boolean isAvailable) { ValidationResult(String domain, boolean isValid, boolean isAvailable) {
@ -125,13 +139,17 @@ public class DomainValidationService {
@Override @Override
public String toString() { 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 // Fake class to represent a review request
private class ReviewRequest { private class ReviewRequest {
String domain; String domain;
String reason; String reason;
ReviewRequest(String domain, String reason) { ReviewRequest(String domain, String reason) {
@ -143,12 +161,16 @@ public class DomainValidationService {
public String toString() { public String toString() {
return "ReviewRequest{domain='" + domain + "', reason='" + reason + "'}"; return "ReviewRequest{domain='" + domain + "', reason='" + reason + "'}";
} }
} }
// Fake class to represent a notification // Fake class to represent a notification
private class Notification { private class Notification {
String recipient; String recipient;
String subject; String subject;
String message; String message;
Notification(String recipient, String subject, String message) { Notification(String recipient, String subject, String message) {
@ -161,5 +183,7 @@ public class DomainValidationService {
public String toString() { public String toString() {
return "Notification{recipient='" + recipient + "', subject='" + subject + "', message='" + message + "'}"; return "Notification{recipient='" + recipient + "', subject='" + subject + "', message='" + message + "'}";
} }
} }
} }

View file

@ -15,6 +15,7 @@ public class OwnerValidation {
private UserValidationService usrValSvc; private UserValidationService usrValSvc;
private DomainValidationService domainValidation; private DomainValidationService domainValidation;
private PasswordUtils pwdUtils; private PasswordUtils pwdUtils;
private Tracer otelTracer; private Tracer otelTracer;

View file

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

View file

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