|
20 | 20 | import static org.junit.Assert.assertEquals;
|
21 | 21 | import static org.junit.Assert.assertNotNull;
|
22 | 22 |
|
23 |
| -import com.google.gson.GsonBuilder; |
24 |
| -import com.google.gson.JsonObject; |
25 |
| -import com.google.gson.annotations.SerializedName; |
| 23 | +import com.google.common.collect.ImmutableList; |
| 24 | +import com.google.common.collect.ImmutableMap; |
26 | 25 |
|
27 | 26 | import org.junit.Before;
|
28 | 27 | import org.junit.Test;
|
|
34 | 33 | import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;
|
35 | 34 | import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;
|
36 | 35 | import org.openqa.grid.web.Hub;
|
37 |
| -import org.openqa.selenium.remote.DesiredCapabilities; |
| 36 | +import org.openqa.selenium.firefox.FirefoxOptions; |
38 | 37 | import org.openqa.testing.FakeHttpServletResponse;
|
39 | 38 | import org.seleniumhq.jetty9.server.handler.ContextHandler;
|
40 | 39 |
|
41 |
| -import java.util.ArrayList; |
42 |
| -import java.util.HashMap; |
43 |
| -import java.util.List; |
44 | 40 | import java.util.Map;
|
| 41 | +import java.util.TreeMap; |
45 | 42 |
|
46 | 43 | import javax.servlet.ServletContext;
|
47 | 44 | import javax.servlet.ServletException;
|
48 | 45 | import javax.servlet.http.HttpServletResponse;
|
49 | 46 |
|
50 | 47 | public class RegistrationServletTest extends BaseServletTest {
|
51 |
| - private class BaseRequest { |
52 |
| - final String name = "proxy-foo"; |
53 |
| - final String description = "a fictitious proxy"; |
54 |
| - @SerializedName( "class" ) |
55 |
| - final String clazz = BaseRequest.class.getCanonicalName(); |
56 |
| - String id; |
57 |
| - } |
58 | 48 |
|
59 |
| - private final class RequestV2 extends BaseRequest { |
60 |
| - final Map<String, Object> configuration = new HashMap<>(); |
61 |
| - final List<DesiredCapabilities> capabilities = new ArrayList<>(); |
62 |
| - } |
| 49 | + private Map<String, Object> requestWithoutConfig; |
| 50 | + private Map<String, Object> grid2Request; |
| 51 | + private Map<String, Object> grid3Request; |
63 | 52 |
|
64 |
| - private final class InvalidV2Request extends BaseRequest { |
65 |
| - final List<DesiredCapabilities> capabilities = new ArrayList<>(); |
66 |
| - } |
67 |
| - |
68 |
| - private final class RequestV3Beta extends BaseRequest { |
69 |
| - final GridNodeConfiguration configuration = new GridNodeConfiguration(); |
70 |
| - final List<DesiredCapabilities> capabilities = new ArrayList<>(); |
| 53 | + @Before |
| 54 | + public void fillBaseRequest() { |
| 55 | + // This base request contains most of the fields we normally see serialised, but lacks the |
| 56 | + // "configuration" and "capabilities" fields. |
| 57 | + Map<String, Object> baseRequest = new TreeMap<>(); |
| 58 | + baseRequest.put("name", "proxy-foo"); |
| 59 | + baseRequest.put("description", "a fictitious proxy"); |
| 60 | + baseRequest.put("class", "com.example.grid.BaseRequest"); |
| 61 | + |
| 62 | + requestWithoutConfig = new TreeMap<>(baseRequest); |
| 63 | + requestWithoutConfig.put("capabilities", ImmutableList.of()); |
| 64 | + |
| 65 | + grid2Request = new TreeMap<>(baseRequest); |
| 66 | + grid2Request.put("capabilities", ImmutableList.of()); |
| 67 | + grid2Request.put("configuration", ImmutableMap.of()); |
| 68 | + |
| 69 | + grid3Request = new TreeMap<>(baseRequest); |
| 70 | + grid3Request.put("capabilities", ImmutableList.of()); |
| 71 | + grid3Request.put("configuration", new GridNodeConfiguration()); |
71 | 72 | }
|
72 | 73 |
|
73 | 74 | @Before
|
@@ -106,71 +107,71 @@ private void waitForServletToAddProxy() throws Exception {
|
106 | 107 | */
|
107 | 108 | @Test(expected = GridConfigurationException.class)
|
108 | 109 | public void testInvalidV2Registration() throws Exception {
|
109 |
| - final InvalidV2Request request = new InvalidV2Request(); |
110 |
| - request.capabilities.add(DesiredCapabilities.firefox()); |
111 |
| - request.id = "http://dummynode:1111"; |
112 |
| - final JsonObject json = new GsonBuilder().serializeNulls().create() |
113 |
| - .toJsonTree(request, InvalidV2Request.class).getAsJsonObject(); |
114 |
| - sendCommand("POST", "/", json); |
115 |
| - } |
| 110 | + requestWithoutConfig.put("capabilities", ImmutableList.of(new FirefoxOptions())); |
| 111 | + requestWithoutConfig.put("id", "http://dummynode:1111"); |
116 | 112 |
|
| 113 | + sendCommand("POST", "/", requestWithoutConfig); |
| 114 | + } |
117 | 115 |
|
118 | 116 | /**
|
119 | 117 | * Tests that the registration request servlet can process a V2 RegistrationRequest which
|
120 | 118 | * contains servlets as a comma separated String.
|
121 | 119 | */
|
122 | 120 | @Test
|
123 | 121 | public void testLegacyV2Registration() throws Exception {
|
124 |
| - final RequestV2 request = new RequestV2(); |
125 |
| - request.configuration.put("servlets", "foo,bar,baz"); |
126 |
| - request.configuration.put("registerCycle", 30001); |
127 |
| - request.configuration.put("proxy", null); |
128 |
| - request.capabilities.add(DesiredCapabilities.firefox()); |
129 |
| - request.id = "http://dummynode:1234"; |
130 |
| - final JsonObject json = new GsonBuilder().serializeNulls().create() |
131 |
| - .toJsonTree(request, RequestV2.class).getAsJsonObject(); |
132 |
| - final FakeHttpServletResponse response = sendCommand("POST", "/", json); |
| 122 | + Map<String, Object> config = new TreeMap<>(); |
| 123 | + config.put("servlets", "foo,bar,baz"); |
| 124 | + config.put("registerCycle", 30001); |
| 125 | + config.put("proxy", null); |
| 126 | + grid2Request.put("configuration", config); |
| 127 | + |
| 128 | + grid2Request.put("capabilities", ImmutableList.of(new FirefoxOptions())); |
| 129 | + String id = "http://dummynode:1234"; |
| 130 | + grid2Request.put("id", id); |
| 131 | + |
| 132 | + final FakeHttpServletResponse response = sendCommand("POST", "/", grid2Request); |
133 | 133 | waitForServletToAddProxy();
|
134 | 134 |
|
135 | 135 | assertEquals(HttpServletResponse.SC_OK, response.getStatus());
|
136 | 136 | assertEquals(((RegistrationServlet) servlet).getRegistry().getAllProxies().size(), 1);
|
137 | 137 |
|
138 | 138 | final RemoteProxy proxy = ((RegistrationServlet) servlet).getRegistry().getAllProxies()
|
139 |
| - .getProxyById(request.id); |
| 139 | + .getProxyById(id); |
140 | 140 | assertNotNull(proxy);
|
141 | 141 | assertEquals(3, proxy.getConfig().servlets.size());
|
142 | 142 | assertEquals(1, proxy.getConfig().capabilities.size());
|
143 | 143 | assertEquals(30001, proxy.getConfig().registerCycle.intValue());
|
144 |
| - assertEquals(request.id, proxy.getConfig().id); |
| 144 | + assertEquals(id, proxy.getConfig().id); |
145 | 145 | }
|
146 | 146 |
|
147 | 147 |
|
148 | 148 | /**
|
149 | 149 | * Tests that the registration request servlet can process a V2 RegistrationRequest from
|
150 |
| - * a 3.0.0-beta node. |
| 150 | + * a 3.x node. |
151 | 151 | */
|
152 | 152 | @Test
|
153 | 153 | public void testLegacyV3BetaRegistration() throws Exception {
|
154 |
| - final RequestV3Beta request = new RequestV3Beta(); |
155 |
| - request.configuration.capabilities.clear(); |
156 |
| - request.configuration.proxy = null; |
157 |
| - request.capabilities.add(DesiredCapabilities.firefox()); |
158 |
| - request.id = "http://dummynode:2345"; |
159 |
| - final JsonObject json = new GsonBuilder().serializeNulls().create() |
160 |
| - .toJsonTree(request, RequestV3Beta.class).getAsJsonObject(); |
161 |
| - final FakeHttpServletResponse response = sendCommand("POST", "/", json); |
| 154 | + GridNodeConfiguration config = new GridNodeConfiguration(); |
| 155 | + config.capabilities.clear(); |
| 156 | + config.proxy = null; |
| 157 | + grid3Request.put("configuration", config); |
| 158 | + |
| 159 | + grid3Request.put("capabilities", ImmutableList.of(new FirefoxOptions())); |
| 160 | + String id = "http://dummynode:2345"; |
| 161 | + grid3Request.put("id", id); |
| 162 | + final FakeHttpServletResponse response = sendCommand("POST", "/", grid3Request); |
162 | 163 | waitForServletToAddProxy();
|
163 | 164 |
|
164 | 165 | assertEquals(HttpServletResponse.SC_OK, response.getStatus());
|
165 | 166 | assertEquals(((RegistrationServlet) servlet).getRegistry().getAllProxies().size(), 1);
|
166 | 167 |
|
167 | 168 | final RemoteProxy proxy = ((RegistrationServlet) servlet).getRegistry().getAllProxies()
|
168 |
| - .getProxyById(request.id); |
| 169 | + .getProxyById(id); |
169 | 170 | assertNotNull(proxy);
|
170 | 171 | assertEquals(0, proxy.getConfig().servlets.size());
|
171 | 172 | assertEquals(1, proxy.getConfig().capabilities.size());
|
172 |
| - assertEquals(request.configuration.registerCycle.intValue(), proxy.getConfig().registerCycle.intValue()); |
173 |
| - assertEquals(request.id, proxy.getConfig().id); |
| 173 | + assertEquals(config.registerCycle.intValue(), proxy.getConfig().registerCycle.intValue()); |
| 174 | + assertEquals(id, proxy.getConfig().id); |
174 | 175 | }
|
175 | 176 |
|
176 | 177 |
|
|
0 commit comments