11package org .labkey .test .pages .admin ;
22
3+ import org .junit .Assert ;
34import org .labkey .test .Locator ;
5+ import org .labkey .test .Locators ;
46import org .labkey .test .WebDriverWrapper ;
57import org .labkey .test .WebTestHelper ;
68import org .labkey .test .components .html .Input ;
911import org .labkey .test .pages .LabKeyPage ;
1012import org .labkey .test .util .LogMethod ;
1113import org .labkey .test .util .LoggedParam ;
14+ import org .labkey .test .util .core .admin .CspConfigHelper .AllowedHost ;
15+ import org .openqa .selenium .NotFoundException ;
1216import org .openqa .selenium .WebDriver ;
1317import org .openqa .selenium .WebElement ;
1418
1519import java .util .ArrayList ;
16- import java .util .Collections ;
17- import java .util .HashMap ;
1820import java .util .List ;
19- import java .util .Map ;
2021import java .util .stream .Stream ;
2122
2223/**
23- * Wraps `AdminController.ExternalSourceAction `
24+ * Wraps `AdminController.ExternalSourcesAction `
2425 */
2526public class ExternalSourcesPage extends LabKeyPage <ExternalSourcesPage .ElementCache >
2627{
@@ -38,7 +39,7 @@ public static ExternalSourcesPage beginAt(WebDriverWrapper webDriverWrapper)
3839 @ LogMethod
3940 public ExternalSourcesPage ensureHost (Directive directive , String host )
4041 {
41- if (!getExistingHosts ().getOrDefault ( directive , Collections . emptyList ()). contains ( host ))
42+ if (!getExistingHosts ().contains ( new AllowedHost ( directive , host ) ))
4243 {
4344 return addHost (directive , host );
4445 }
@@ -57,34 +58,73 @@ public ExternalSourcesPage addHost(@LoggedParam Directive directive, @LoggedPara
5758
5859 clickAndWait (elementCache ().addButton );
5960 clearCache ();
61+ assertNoLabKeyErrors ();
6062 return this ;
6163 }
6264
63- public Map <Directive , List <String >> getExistingHosts ()
65+ @ LogMethod (quiet = true )
66+ public List <String > addHostExpectingError (@ LoggedParam Directive directive , @ LoggedParam String host )
6467 {
65- Map <Directive , List <String >> existingHosts = new HashMap <>();
68+ elementCache ().directiveSelect .selectOption (directive );
69+ elementCache ().hostInput .set (host );
6670
67- for (Map .Entry <Directive , List <Input >> entry : getExistingHostInputs ().entrySet ())
68- {
69- existingHosts .put (entry .getKey (), entry .getValue ().stream ().map (Input ::getValue ).toList ());
70- }
71+ clickAndWait (elementCache ().addButton );
72+ clearCache ();
7173
72- return existingHosts ;
74+ List <WebElement > errorEls = Locators .labkeyError .findElements (elementCache ());
75+ Assert .assertFalse ("No errors found" , errorEls .isEmpty ());
76+ return getTexts (errorEls );
7377 }
7478
75- public Map <Directive , List <Input >> getExistingHostInputs ()
79+ public ExternalSourcesPage editHost (Directive directive , String host , String newHost )
80+ {
81+ getExistingSourceRow (directive , host ).getHostInput ().set (newHost );
82+ return this ;
83+ }
84+
85+ public ExternalSourcesPage deleteHost (Directive directive , String host )
86+ {
87+ getExistingSourceRow (directive , host ).clickDelete ();
88+ return this ;
89+ }
90+
91+ private ExistingSourceRow getExistingSourceRow (Directive directive , String host )
92+ {
93+ return elementCache ().getExistingSourceRows ().stream ()
94+ .filter (row -> row .getDirective ().equals (directive ) && row .getHost ().equalsIgnoreCase (host ))
95+ .findFirst ().orElseThrow (() -> new NotFoundException ("Host " + host + " does not exist for directive " + directive ));
96+ }
97+
98+ @ LogMethod (quiet = true )
99+ public ExternalSourcesPage saveChanges ()
100+ {
101+ clickAndWait (elementCache ().saveButton );
102+ clearCache ();
103+
104+ assertNoLabKeyErrors ();
105+ return this ;
106+ }
107+
108+ @ LogMethod (quiet = true )
109+ public List <String > saveChangesExpectingError ()
76110 {
77- List <WebElement > directiveColumn = elementCache ().existingValuesTable .getColumnAsElement (1 );
78- List <WebElement > hostsColumn = elementCache ().existingValuesTable .getColumnAsElement (2 );
111+ clickAndWait (elementCache ().saveButton );
112+ clearCache ();
113+
114+ List <WebElement > errorEls = Locators .labkeyError .findElements (elementCache ());
115+ Assert .assertFalse ("No errors found" , errorEls .isEmpty ());
116+ return getTexts (errorEls );
117+ }
79118
80- Map <Directive , List <Input >> existingHosts = new HashMap <>();
119+ public List <AllowedHost > getExistingHosts ()
120+ {
121+ List <AllowedHost > existingHosts = new ArrayList <>();
81122
82- for (int i = 0 ; i < hostsColumn . size (); i ++ )
123+ for (ExistingSourceRow row : elementCache (). getExistingSourceRows () )
83124 {
84- WebElement directiveInput = Locator .tag ("input" ).findElement (directiveColumn .get (i ));
85- Directive directive = Directive .valueOf (directiveInput .getDomAttribute ("data-directive" ));
86- Input hostInput = Input .Input (Locator .tag ("input" ), getDriver ()).find (hostsColumn .get (i ));
87- existingHosts .computeIfAbsent (directive , d -> new ArrayList <>()).add (hostInput );
125+ Directive directive = row .getDirective ();
126+ String host = row .getHost ();
127+ existingHosts .add (new AllowedHost (directive , host ));
88128 }
89129
90130 return existingHosts ;
@@ -105,6 +145,59 @@ protected class ElementCache extends LabKeyPage<ElementCache>.ElementCache
105145
106146 final WebElement existingValuesForm = Locator .name ("existingValues" ).findWhenNeeded (this );
107147 final Table existingValuesTable = new Table (getDriver (), Locator .byClass ("labkey-data-region-legacy" ).findWhenNeeded (existingValuesForm ), 0 );
148+ private List <ExistingSourceRow > existingSourceRows ;
149+
150+ protected List <ExistingSourceRow > getExistingSourceRows ()
151+ {
152+ if (existingSourceRows == null )
153+ {
154+ existingSourceRows = existingValuesTable .getRows ().stream ().map (ExistingSourceRow ::new ).toList ();
155+ }
156+ return existingSourceRows ;
157+ }
158+
159+ final WebElement saveButton = Locator .lkButton ("Save" ).findWhenNeeded (existingValuesForm );
160+ }
161+
162+ protected class ExistingSourceRow
163+ {
164+ private final WebElement directiveInput ;
165+ private final Input hostInput ;
166+ private final WebElement deleteButton ;
167+
168+ private Directive directive ;
169+
170+ ExistingSourceRow (WebElement row )
171+ {
172+ directiveInput = Locator .tag ("input" ).withAttributeContaining ("name" , "directive" ).findWhenNeeded (row );
173+ hostInput = Input .Input (Locator .tag ("input" ).withAttributeContaining ("name" , "host" ), getDriver ()).findWhenNeeded (row );
174+ deleteButton = Locator .lkButton ("Delete" ).findWhenNeeded (row );
175+ }
176+
177+ Directive getDirective ()
178+ {
179+ if (directive == null )
180+ {
181+ directive = Directive .valueOf (directiveInput .getDomAttribute ("data-directive" ));
182+ }
183+ return directive ;
184+ }
185+
186+ public Input getHostInput ()
187+ {
188+ return hostInput ;
189+ }
190+
191+ public String getHost ()
192+ {
193+ return hostInput .get ();
194+ }
195+
196+ public void clickDelete ()
197+ {
198+ clickAndWait (deleteButton );
199+ clearCache ();
200+ }
108201 }
109202
110203 public enum Directive implements OptionSelect .SelectOption
0 commit comments