mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-21 07:15:49 +00:00
format
This commit is contained in:
parent
d88a6e1af5
commit
d927a42681
4 changed files with 57 additions and 44 deletions
|
@ -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
|
||||||
|
@ -38,7 +44,7 @@ public class DomainValidationService {
|
||||||
|
|
||||||
// Fake method to check if domain contains restricted words
|
// Fake method to check if domain contains restricted words
|
||||||
private boolean containsRestrictedWords(String domain) {
|
private boolean containsRestrictedWords(String domain) {
|
||||||
String[] restrictedWords = {"badword", "restricted"};
|
String[] restrictedWords = { "badword", "restricted" };
|
||||||
for (String word : restrictedWords) {
|
for (String word : restrictedWords) {
|
||||||
if (domain.contains(word)) {
|
if (domain.contains(word)) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -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);
|
||||||
|
@ -75,7 +86,7 @@ public class DomainValidationService {
|
||||||
|
|
||||||
// Fake method to check if the domain is reserved
|
// Fake method to check if the domain is reserved
|
||||||
private boolean isReservedDomain(String domain) {
|
private boolean isReservedDomain(String domain) {
|
||||||
String[] reservedDomains = {"example.com", "localhost"};
|
String[] reservedDomains = { "example.com", "localhost" };
|
||||||
for (String reserved : reservedDomains) {
|
for (String reserved : reservedDomains) {
|
||||||
if (domain.equalsIgnoreCase(reserved)) {
|
if (domain.equalsIgnoreCase(reserved)) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -86,7 +97,7 @@ public class DomainValidationService {
|
||||||
|
|
||||||
// Fake method to check if the domain is a premium domain
|
// Fake method to check if the domain is a premium domain
|
||||||
private boolean isPremiumDomain(String domain) {
|
private boolean isPremiumDomain(String domain) {
|
||||||
String[] premiumDomains = {"premium.com", "exclusive.com"};
|
String[] premiumDomains = { "premium.com", "exclusive.com" };
|
||||||
for (String premium : premiumDomains) {
|
for (String premium : premiumDomains) {
|
||||||
if (domain.equalsIgnoreCase(premium)) {
|
if (domain.equalsIgnoreCase(premium)) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -102,7 +113,7 @@ public class DomainValidationService {
|
||||||
|
|
||||||
// Fake method to simulate domain suffix validation
|
// Fake method to simulate domain suffix validation
|
||||||
private boolean isValidDomainSuffix(String domain) {
|
private boolean isValidDomainSuffix(String domain) {
|
||||||
String[] validSuffixes = {".com", ".net", ".org"};
|
String[] validSuffixes = { ".com", ".net", ".org" };
|
||||||
for (String suffix : validSuffixes) {
|
for (String suffix : validSuffixes) {
|
||||||
if (domain.endsWith(suffix)) {
|
if (domain.endsWith(suffix)) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -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 + "'}";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
@ -28,7 +29,7 @@ public class OwnerValidation {
|
||||||
this.roleSvc = new RoleService();
|
this.roleSvc = new RoleService();
|
||||||
this.otelTracer = otelTracer;
|
this.otelTracer = otelTracer;
|
||||||
this.usrValSvc = new UserValidationService();
|
this.usrValSvc = new UserValidationService();
|
||||||
this.domainValidation=new DomainValidationService();
|
this.domainValidation = new DomainValidationService();
|
||||||
this.twoFASvc = new TwoFactorAuthenticationService();
|
this.twoFASvc = new TwoFactorAuthenticationService();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,4 +15,5 @@ public class UserValidationService {
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,37 +93,24 @@ 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());
|
||||||
|
|
||||||
given().contentType(ContentType.JSON)
|
given().contentType(ContentType.JSON)
|
||||||
.when()
|
.when()
|
||||||
.get("/owners")
|
.get("/owners")
|
||||||
.then()
|
.then()
|
||||||
.contentType(ContentType.HTML)
|
.contentType(ContentType.HTML)
|
||||||
.statusCode(200)
|
.statusCode(200)
|
||||||
.body(ownerLinkMatcher, Matchers.notNullValue());
|
.body(ownerLinkMatcher, Matchers.notNullValue());
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i=0; i<50;i++) {
|
|
||||||
|
|
||||||
given().contentType(ContentType.JSON)
|
|
||||||
.when()
|
|
||||||
.get("/owners")
|
|
||||||
.then()
|
|
||||||
.contentType(ContentType.HTML)
|
|
||||||
.statusCode(200);
|
|
||||||
|
|
||||||
try {
|
|
||||||
Thread.sleep(200);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
given().contentType(ContentType.JSON)
|
||||||
|
.when()
|
||||||
|
.get("/owners")
|
||||||
|
.then()
|
||||||
|
.contentType(ContentType.HTML)
|
||||||
|
.statusCode(200);
|
||||||
|
|
||||||
// assertThat(false).isTrue();
|
// assertThat(false).isTrue();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue