Best Easymock code snippet using org.easymock.Capture.newInstance
Source:ConnectorsResourceTest.java
...90 connectorsResource = new ConnectorsResource(herder);91 }92 @Test93 public void testListConnectors() throws Throwable {94 final Capture<Callback<Collection<String>>> cb = Capture.newInstance();95 herder.connectors(EasyMock.capture(cb));96 expectAndCallbackResult(cb, Arrays.asList(CONNECTOR2_NAME, CONNECTOR_NAME));97 PowerMock.replayAll();98 Collection<String> connectors = connectorsResource.listConnectors(FORWARD);99 // Ordering isn't guaranteed, compare sets100 assertEquals(new HashSet<>(Arrays.asList(CONNECTOR_NAME, CONNECTOR2_NAME)), new HashSet<>(connectors));101 PowerMock.verifyAll();102 }103 @Test104 public void testListConnectorsNotLeader() throws Throwable {105 final Capture<Callback<Collection<String>>> cb = Capture.newInstance();106 herder.connectors(EasyMock.capture(cb));107 expectAndCallbackNotLeaderException(cb);108 // Should forward request109 EasyMock.expect(RestServer.httpRequest(EasyMock.eq("http://leader:8083/connectors?forward=false"), EasyMock.eq("GET"),110 EasyMock.isNull(), EasyMock.anyObject(TypeReference.class)))111 .andReturn(new RestServer.HttpResponse<>(200, new HashMap<String, List<String>>(), Arrays.asList(CONNECTOR2_NAME, CONNECTOR_NAME)));112 PowerMock.replayAll();113 Collection<String> connectors = connectorsResource.listConnectors(FORWARD);114 // Ordering isn't guaranteed, compare sets115 assertEquals(new HashSet<>(Arrays.asList(CONNECTOR_NAME, CONNECTOR2_NAME)), new HashSet<>(connectors));116 PowerMock.verifyAll();117 }118 @Test(expected = ConnectException.class)119 public void testListConnectorsNotSynced() throws Throwable {120 final Capture<Callback<Collection<String>>> cb = Capture.newInstance();121 herder.connectors(EasyMock.capture(cb));122 expectAndCallbackException(cb, new ConnectException("not synced"));123 PowerMock.replayAll();124 // throws125 connectorsResource.listConnectors(FORWARD);126 }127 @Test128 public void testCreateConnector() throws Throwable {129 CreateConnectorRequest body = new CreateConnectorRequest(CONNECTOR_NAME, Collections.singletonMap(ConnectorConfig.NAME_CONFIG, CONNECTOR_NAME));130 final Capture<Callback<Herder.Created<ConnectorInfo>>> cb = Capture.newInstance();131 herder.putConnectorConfig(EasyMock.eq(CONNECTOR_NAME), EasyMock.eq(body.config()), EasyMock.eq(false), EasyMock.capture(cb));132 expectAndCallbackResult(cb, new Herder.Created<>(true, new ConnectorInfo(CONNECTOR_NAME, CONNECTOR_CONFIG, CONNECTOR_TASK_NAMES)));133 PowerMock.replayAll();134 connectorsResource.createConnector(FORWARD, body);135 PowerMock.verifyAll();136 }137 @Test138 public void testCreateConnectorNotLeader() throws Throwable {139 CreateConnectorRequest body = new CreateConnectorRequest(CONNECTOR_NAME, Collections.singletonMap(ConnectorConfig.NAME_CONFIG, CONNECTOR_NAME));140 final Capture<Callback<Herder.Created<ConnectorInfo>>> cb = Capture.newInstance();141 herder.putConnectorConfig(EasyMock.eq(CONNECTOR_NAME), EasyMock.eq(body.config()), EasyMock.eq(false), EasyMock.capture(cb));142 expectAndCallbackNotLeaderException(cb);143 // Should forward request144 EasyMock.expect(RestServer.httpRequest(EasyMock.eq("http://leader:8083/connectors?forward=false"), EasyMock.eq("POST"), EasyMock.eq(body), EasyMock.<TypeReference>anyObject()))145 .andReturn(new RestServer.HttpResponse<>(201, new HashMap<String, List<String>>(), new ConnectorInfo(CONNECTOR_NAME, CONNECTOR_CONFIG, CONNECTOR_TASK_NAMES)));146 PowerMock.replayAll();147 connectorsResource.createConnector(FORWARD, body);148 PowerMock.verifyAll();149 }150 @Test(expected = AlreadyExistsException.class)151 public void testCreateConnectorExists() throws Throwable {152 CreateConnectorRequest body = new CreateConnectorRequest(CONNECTOR_NAME, Collections.singletonMap(ConnectorConfig.NAME_CONFIG, CONNECTOR_NAME));153 final Capture<Callback<Herder.Created<ConnectorInfo>>> cb = Capture.newInstance();154 herder.putConnectorConfig(EasyMock.eq(CONNECTOR_NAME), EasyMock.eq(body.config()), EasyMock.eq(false), EasyMock.capture(cb));155 expectAndCallbackException(cb, new AlreadyExistsException("already exists"));156 PowerMock.replayAll();157 connectorsResource.createConnector(FORWARD, body);158 PowerMock.verifyAll();159 }160 @Test161 public void testDeleteConnector() throws Throwable {162 final Capture<Callback<Herder.Created<ConnectorInfo>>> cb = Capture.newInstance();163 herder.putConnectorConfig(EasyMock.eq(CONNECTOR_NAME), EasyMock.<Map<String, String>>isNull(), EasyMock.eq(true), EasyMock.capture(cb));164 expectAndCallbackResult(cb, null);165 PowerMock.replayAll();166 connectorsResource.destroyConnector(CONNECTOR_NAME, FORWARD);167 PowerMock.verifyAll();168 }169 @Test170 public void testDeleteConnectorNotLeader() throws Throwable {171 final Capture<Callback<Herder.Created<ConnectorInfo>>> cb = Capture.newInstance();172 herder.putConnectorConfig(EasyMock.eq(CONNECTOR_NAME), EasyMock.<Map<String, String>>isNull(), EasyMock.eq(true), EasyMock.capture(cb));173 expectAndCallbackNotLeaderException(cb);174 // Should forward request175 EasyMock.expect(RestServer.httpRequest("http://leader:8083/connectors/" + CONNECTOR_NAME + "?forward=false", "DELETE", null, null))176 .andReturn(new RestServer.HttpResponse<>(204, new HashMap<String, List<String>>(), null));177 PowerMock.replayAll();178 connectorsResource.destroyConnector(CONNECTOR_NAME, FORWARD);179 PowerMock.verifyAll();180 }181 // Not found exceptions should pass through to caller so they can be processed for 404s182 @Test(expected = NotFoundException.class)183 public void testDeleteConnectorNotFound() throws Throwable {184 final Capture<Callback<Herder.Created<ConnectorInfo>>> cb = Capture.newInstance();185 herder.putConnectorConfig(EasyMock.eq(CONNECTOR_NAME), EasyMock.<Map<String, String>>isNull(), EasyMock.eq(true), EasyMock.capture(cb));186 expectAndCallbackException(cb, new NotFoundException("not found"));187 PowerMock.replayAll();188 connectorsResource.destroyConnector(CONNECTOR_NAME, FORWARD);189 PowerMock.verifyAll();190 }191 @Test192 public void testGetConnector() throws Throwable {193 final Capture<Callback<ConnectorInfo>> cb = Capture.newInstance();194 herder.connectorInfo(EasyMock.eq(CONNECTOR_NAME), EasyMock.capture(cb));195 expectAndCallbackResult(cb, new ConnectorInfo(CONNECTOR_NAME, CONNECTOR_CONFIG, CONNECTOR_TASK_NAMES));196 PowerMock.replayAll();197 ConnectorInfo connInfo = connectorsResource.getConnector(CONNECTOR_NAME, FORWARD);198 assertEquals(new ConnectorInfo(CONNECTOR_NAME, CONNECTOR_CONFIG, CONNECTOR_TASK_NAMES), connInfo);199 PowerMock.verifyAll();200 }201 @Test202 public void testGetConnectorConfig() throws Throwable {203 final Capture<Callback<Map<String, String>>> cb = Capture.newInstance();204 herder.connectorConfig(EasyMock.eq(CONNECTOR_NAME), EasyMock.capture(cb));205 expectAndCallbackResult(cb, CONNECTOR_CONFIG);206 PowerMock.replayAll();207 Map<String, String> connConfig = connectorsResource.getConnectorConfig(CONNECTOR_NAME, FORWARD);208 assertEquals(CONNECTOR_CONFIG, connConfig);209 PowerMock.verifyAll();210 }211 @Test(expected = NotFoundException.class)212 public void testGetConnectorConfigConnectorNotFound() throws Throwable {213 final Capture<Callback<Map<String, String>>> cb = Capture.newInstance();214 herder.connectorConfig(EasyMock.eq(CONNECTOR_NAME), EasyMock.capture(cb));215 expectAndCallbackException(cb, new NotFoundException("not found"));216 PowerMock.replayAll();217 connectorsResource.getConnectorConfig(CONNECTOR_NAME, FORWARD);218 PowerMock.verifyAll();219 }220 @Test221 public void testPutConnectorConfig() throws Throwable {222 final Capture<Callback<Herder.Created<ConnectorInfo>>> cb = Capture.newInstance();223 herder.putConnectorConfig(EasyMock.eq(CONNECTOR_NAME), EasyMock.eq(CONNECTOR_CONFIG), EasyMock.eq(true), EasyMock.capture(cb));224 expectAndCallbackResult(cb, new Herder.Created<>(false, new ConnectorInfo(CONNECTOR_NAME, CONNECTOR_CONFIG, CONNECTOR_TASK_NAMES)));225 PowerMock.replayAll();226 connectorsResource.putConnectorConfig(CONNECTOR_NAME, FORWARD, CONNECTOR_CONFIG);227 PowerMock.verifyAll();228 }229 @Test(expected = BadRequestException.class)230 public void testPutConnectorConfigNameMismatch() throws Throwable {231 Map<String, String> connConfig = new HashMap<>(CONNECTOR_CONFIG);232 connConfig.put(ConnectorConfig.NAME_CONFIG, "mismatched-name");233 connectorsResource.putConnectorConfig(CONNECTOR_NAME, FORWARD, connConfig);234 }235 @Test236 public void testGetConnectorTaskConfigs() throws Throwable {237 final Capture<Callback<List<TaskInfo>>> cb = Capture.newInstance();238 herder.taskConfigs(EasyMock.eq(CONNECTOR_NAME), EasyMock.capture(cb));239 expectAndCallbackResult(cb, TASK_INFOS);240 PowerMock.replayAll();241 List<TaskInfo> taskInfos = connectorsResource.getTaskConfigs(CONNECTOR_NAME, FORWARD);242 assertEquals(TASK_INFOS, taskInfos);243 PowerMock.verifyAll();244 }245 @Test(expected = NotFoundException.class)246 public void testGetConnectorTaskConfigsConnectorNotFound() throws Throwable {247 final Capture<Callback<List<TaskInfo>>> cb = Capture.newInstance();248 herder.taskConfigs(EasyMock.eq(CONNECTOR_NAME), EasyMock.capture(cb));249 expectAndCallbackException(cb, new NotFoundException("connector not found"));250 PowerMock.replayAll();251 connectorsResource.getTaskConfigs(CONNECTOR_NAME, FORWARD);252 PowerMock.verifyAll();253 }254 @Test255 public void testPutConnectorTaskConfigs() throws Throwable {256 final Capture<Callback<Void>> cb = Capture.newInstance();257 herder.putTaskConfigs(EasyMock.eq(CONNECTOR_NAME), EasyMock.eq(TASK_CONFIGS), EasyMock.capture(cb));258 expectAndCallbackResult(cb, null);259 PowerMock.replayAll();260 connectorsResource.putTaskConfigs(CONNECTOR_NAME, FORWARD, TASK_CONFIGS);261 PowerMock.verifyAll();262 }263 @Test(expected = NotFoundException.class)264 public void testPutConnectorTaskConfigsConnectorNotFound() throws Throwable {265 final Capture<Callback<Void>> cb = Capture.newInstance();266 herder.putTaskConfigs(EasyMock.eq(CONNECTOR_NAME), EasyMock.eq(TASK_CONFIGS), EasyMock.capture(cb));267 expectAndCallbackException(cb, new NotFoundException("not found"));268 PowerMock.replayAll();269 connectorsResource.putTaskConfigs(CONNECTOR_NAME, FORWARD, TASK_CONFIGS);270 PowerMock.verifyAll();271 }272 @Test(expected = NotFoundException.class)273 public void testRestartConnectorNotFound() throws Throwable {274 final Capture<Callback<Void>> cb = Capture.newInstance();275 herder.restartConnector(EasyMock.eq(CONNECTOR_NAME), EasyMock.capture(cb));276 expectAndCallbackException(cb, new NotFoundException("not found"));277 PowerMock.replayAll();278 connectorsResource.restartConnector(CONNECTOR_NAME, FORWARD);279 PowerMock.verifyAll();280 }281 @Test282 public void testRestartConnectorLeaderRedirect() throws Throwable {283 final Capture<Callback<Void>> cb = Capture.newInstance();284 herder.restartConnector(EasyMock.eq(CONNECTOR_NAME), EasyMock.capture(cb));285 expectAndCallbackNotLeaderException(cb);286 EasyMock.expect(RestServer.httpRequest(EasyMock.eq("http://leader:8083/connectors/" + CONNECTOR_NAME + "/restart?forward=true"),287 EasyMock.eq("POST"), EasyMock.isNull(), EasyMock.<TypeReference>anyObject()))288 .andReturn(new RestServer.HttpResponse<>(202, new HashMap<String, List<String>>(), null));289 PowerMock.replayAll();290 connectorsResource.restartConnector(CONNECTOR_NAME, null);291 PowerMock.verifyAll();292 }293 @Test294 public void testRestartConnectorOwnerRedirect() throws Throwable {295 final Capture<Callback<Void>> cb = Capture.newInstance();296 herder.restartConnector(EasyMock.eq(CONNECTOR_NAME), EasyMock.capture(cb));297 String ownerUrl = "http://owner:8083";298 expectAndCallbackException(cb, new NotAssignedException("not owner test", ownerUrl));299 EasyMock.expect(RestServer.httpRequest(EasyMock.eq("http://owner:8083/connectors/" + CONNECTOR_NAME + "/restart?forward=false"),300 EasyMock.eq("POST"), EasyMock.isNull(), EasyMock.<TypeReference>anyObject()))301 .andReturn(new RestServer.HttpResponse<>(202, new HashMap<String, List<String>>(), null));302 PowerMock.replayAll();303 connectorsResource.restartConnector(CONNECTOR_NAME, true);304 PowerMock.verifyAll();305 }306 @Test(expected = NotFoundException.class)307 public void testRestartTaskNotFound() throws Throwable {308 ConnectorTaskId taskId = new ConnectorTaskId(CONNECTOR_NAME, 0);309 final Capture<Callback<Void>> cb = Capture.newInstance();310 herder.restartTask(EasyMock.eq(taskId), EasyMock.capture(cb));311 expectAndCallbackException(cb, new NotFoundException("not found"));312 PowerMock.replayAll();313 connectorsResource.restartTask(CONNECTOR_NAME, 0, FORWARD);314 PowerMock.verifyAll();315 }316 @Test317 public void testRestartTaskLeaderRedirect() throws Throwable {318 ConnectorTaskId taskId = new ConnectorTaskId(CONNECTOR_NAME, 0);319 final Capture<Callback<Void>> cb = Capture.newInstance();320 herder.restartTask(EasyMock.eq(taskId), EasyMock.capture(cb));321 expectAndCallbackNotLeaderException(cb);322 EasyMock.expect(RestServer.httpRequest(EasyMock.eq("http://leader:8083/connectors/" + CONNECTOR_NAME + "/tasks/0/restart?forward=true"),323 EasyMock.eq("POST"), EasyMock.isNull(), EasyMock.<TypeReference>anyObject()))324 .andReturn(new RestServer.HttpResponse<>(202, new HashMap<String, List<String>>(), null));325 PowerMock.replayAll();326 connectorsResource.restartTask(CONNECTOR_NAME, 0, null);327 PowerMock.verifyAll();328 }329 @Test330 public void testRestartTaskOwnerRedirect() throws Throwable {331 ConnectorTaskId taskId = new ConnectorTaskId(CONNECTOR_NAME, 0);332 final Capture<Callback<Void>> cb = Capture.newInstance();333 herder.restartTask(EasyMock.eq(taskId), EasyMock.capture(cb));334 String ownerUrl = "http://owner:8083";335 expectAndCallbackException(cb, new NotAssignedException("not owner test", ownerUrl));336 EasyMock.expect(RestServer.httpRequest(EasyMock.eq("http://owner:8083/connectors/" + CONNECTOR_NAME + "/tasks/0/restart?forward=false"),337 EasyMock.eq("POST"), EasyMock.isNull(), EasyMock.<TypeReference>anyObject()))338 .andReturn(new RestServer.HttpResponse<>(202, new HashMap<String, List<String>>(), null));339 PowerMock.replayAll();340 connectorsResource.restartTask(CONNECTOR_NAME, 0, true);341 PowerMock.verifyAll();342 }343 private <T> void expectAndCallbackResult(final Capture<Callback<T>> cb, final T value) {344 PowerMock.expectLastCall().andAnswer(new IAnswer<Void>() {345 @Override346 public Void answer() throws Throwable {...
newInstance
Using AI Code Generation
1Capture<String> capture = new Capture<String>();2EasyMock.expect(mock.add(EasyMock.anyString(), EasyMock.capture(capture))).andReturn(1);3EasyMock.replay(mock);4assertEquals("Hello World", capture.getValue());5Capture<String> capture = new Capture<String>();6EasyMock.expect(mock.add(EasyMock.anyString(), EasyMock.capture(capture))).andReturn(1);7EasyMock.replay(mock);8assertEquals("Hello World", capture.getValue());9Capture<String> capture = new Capture<String>();10EasyMock.expect(mock.add(EasyMock.anyString(), EasyMock.capture(capture))).andReturn(1);11EasyMock.replay(mock);12assertEquals("Hello World", capture.getValue());13Capture<String> capture = new Capture<String>();14EasyMock.expect(mock.add(EasyMock.anyString(), EasyMock.capture(capture))).andReturn(1);15EasyMock.replay(mock);16assertEquals("Hello World", capture.getValue());17Capture<String> capture = new Capture<String>();18EasyMock.expect(mock.add(EasyMock.anyString(), EasyMock.capture(capture))).andReturn(1);19EasyMock.replay(mock);20assertEquals("Hello World", capture.getValue());21Capture<String> capture = new Capture<String>();22EasyMock.expect(mock.add(EasyMock.anyString(), EasyMock.capture(capture))).andReturn(1);23EasyMock.replay(mock);24assertEquals("Hello World", capture.getValue());25Capture<String> capture = new Capture<String>();26EasyMock.expect(mock.add(EasyMock.anyString(), EasyMock.capture(capture))).andReturn(1);27EasyMock.replay(mock);28assertEquals("Hello World", capture.getValue());29Capture<String> capture = new Capture<String>();30EasyMock.expect(mock.add(EasyMock.anyString(), EasyMock.capture(capture))).andReturn(1);31EasyMock.replay(mock);32assertEquals("Hello World", capture.getValue());33Capture<String> capture = new Capture<String>();34EasyMock.expect(mock.add(EasyMock.any
newInstance
Using AI Code Generation
1Capture<Object> capture = Capture.newInstance();2expect(mock.method(capture)).andReturn("Hello");3Object capturedObject = capture.getValue();4assertTrue(capture.hasCaptured());5capture.reset();6capture.setValue("Hello");7String str = capture.toString();8Object capturedObject = capture.getValue();9assertTrue(capture.hasCaptured());10capture.reset();11capture.setValue("Hello");12String str = capture.toString();13Object capturedObject = capture.getValue();14assertTrue(capture.hasCaptured());15capture.reset();16capture.setValue("Hello");17String str = capture.toString();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!