3737import org .labkey .remoteapi .query .SelectRowsResponse ;
3838import org .labkey .remoteapi .query .Sort ;
3939import org .labkey .serverapi .reader .TabLoader ;
40+ import org .labkey .test .TestProperties ;
4041import org .labkey .test .WebTestHelper ;
4142import org .labkey .test .params .FieldDefinition ;
43+ import org .labkey .test .util .DomainUtils .DomainKind ;
4244import org .labkey .test .util .data .ColumnNameMapper ;
4345import org .labkey .test .util .data .TestDataUtils ;
4446import org .labkey .test .util .query .QueryApiHelper ;
@@ -566,7 +568,7 @@ public static String randomInvalidDomainName(@Nullable String namePart, int numS
566568 return domainName ;
567569 }
568570
569- public static String randomDomainName (@ Nullable String namePart , @ Nullable DomainUtils . DomainKind domainKind )
571+ public static String randomDomainName (@ Nullable String namePart , @ Nullable DomainKind domainKind )
570572 {
571573 return randomDomainName (namePart , null , null , domainKind );
572574 }
@@ -579,10 +581,10 @@ public static String randomDomainName(@Nullable String namePart, @Nullable Domai
579581 * @param numEndChars Number of random characters at end of name
580582 * @return name containing the given name part and appended random characters that should be a valid domain name
581583 */
582- public static String randomDomainName (@ Nullable String namePart , @ Nullable Integer numStartChars , @ Nullable Integer numEndChars , @ Nullable DomainUtils . DomainKind domainKind )
584+ public static String randomDomainName (@ Nullable String namePart , @ Nullable Integer numStartChars , @ Nullable Integer numEndChars , @ Nullable DomainKind domainKind )
583585 {
584586 String _namePart = namePart == null ? "" : namePart ;
585- DomainUtils . DomainKind _domainKind = domainKind == null ? DomainUtils . DomainKind .SampleSet : domainKind ;
587+ DomainKind _domainKind = domainKind == null ? DomainKind .SampleSet : domainKind ;
586588 String charSet = ALPHANUMERIC_STRING + DOMAIN_SPECIAL_STRING ;
587589 int currentTries = 0 ;
588590 String domainName = randomName (_namePart , getNumChars (numStartChars , 5 ), getNumChars (numEndChars , 50 ), charSet , null );
@@ -615,14 +617,14 @@ public static String randomFieldName(String part, @Nullable String exclusion)
615617 return randomFieldName (part , exclusion , null );
616618 }
617619
618- public static String randomFieldName (String part , @ Nullable String exclusion , DomainUtils . DomainKind domainKind )
620+ public static String randomFieldName (String part , @ Nullable String exclusion , DomainKind domainKind )
619621 {
620622 return randomFieldName (part , null , null , exclusion , domainKind );
621623 }
622624
623- public static String randomFieldName (@ NotNull String part , @ Nullable Integer numStartChars , @ Nullable Integer numEndChars , @ Nullable String exclusion , @ Nullable DomainUtils . DomainKind domainKind )
625+ public static String randomFieldName (@ NotNull String part , @ Nullable Integer numStartChars , @ Nullable Integer numEndChars , @ Nullable String exclusion , @ Nullable DomainKind domainKind )
624626 {
625- DomainUtils . DomainKind _domainKind = domainKind == null ? DomainUtils . DomainKind .SampleSet : domainKind ;
627+ DomainKind _domainKind = domainKind == null ? DomainKind .SampleSet : domainKind ;
626628
627629 // use the characters that we know are encoded in fieldKeys plus characters that we know clients are using
628630 // Issue 53197: Field name with double byte character can cause client side exception in Firefox when trying to customize grid view.
@@ -642,7 +644,22 @@ public static String randomFieldName(@NotNull String part, @Nullable Integer num
642644 return randomFieldName ;
643645 }
644646
645- private static boolean isDomainAndFieldNameInvalid (DomainUtils .DomainKind domainKind , @ Nullable String domainName , @ Nullable String fieldName )
647+ private static boolean isDomainAndFieldNameInvalid (DomainKind domainKind , @ Nullable String domainName , @ Nullable String fieldName )
648+ {
649+ if (fieldName != null && fieldName .length () > 64 && fieldName .toLowerCase ().contains ("key" )) // Not guaranteed but likely a list key
650+ return true ; // Issue 53706: List key field name length is limited to 64 characters
651+
652+ if (TestProperties .isRemoteNameValidationEnabled ())
653+ {
654+ return isNameInvalidRemote (domainKind , domainName , fieldName );
655+ }
656+ else
657+ {
658+ return isNameInvalidLocal (domainKind , domainName , fieldName );
659+ }
660+ }
661+
662+ private static boolean isNameInvalidRemote (DomainKind domainKind , @ Nullable String domainName , @ Nullable String fieldName )
646663 {
647664 SimplePostCommand command = new SimplePostCommand ("property" , "validateDomainAndFieldNames" );
648665 JSONObject domainDesign = new JSONObject ();
@@ -679,6 +696,35 @@ private static boolean isDomainAndFieldNameInvalid(DomainUtils.DomainKind domain
679696 }
680697 }
681698
699+ public static boolean isNameInvalidLocal (DomainKind domainKind , @ Nullable String domainName , @ Nullable String fieldName )
700+ {
701+ if (domainName != null )
702+ {
703+ if (!Character .isLetterOrDigit (domainName .charAt (0 )))
704+ return true ; // domain needs to start with alphanumeric char
705+ if (Pattern .matches ("(.*\\ s--[^ ].*)|(.*\\ s-[^- ].*)" , domainName ))
706+ return true ; // domain name must not contain space followed by dash. (command like: Issue 49161)
707+
708+ int maxLength = switch (domainKind )
709+ {
710+ case Assay -> 200 - 13 ; // Make room for "{$domainName} Batch Fields" domain
711+ case SampleSet -> 100 ;
712+ default -> 200 ; // Sources, lists, and datasets allow 200 character names
713+ };
714+ if (domainName .length () > maxLength )
715+ return true ;
716+ }
717+ if (fieldName != null )
718+ {
719+ if (fieldName .length () > 200 )
720+ return true ;
721+ if (Pattern .matches (".*:[a-zA-Z]{3}.*" , fieldName )) // Avoid illegal patterns like ":Date"
722+ return true ;
723+ }
724+
725+ return false ;
726+ }
727+
682728 public static <T > T randomChoice (List <T > choices )
683729 {
684730 return choices .get (randomInt (0 , choices .size () - 1 ));
0 commit comments