Java Code Examples for org.easymock.EasyMock#niceMock()

The following examples show how to use org.easymock.EasyMock#niceMock() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KsqlResourceTest.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturn5xxOnSystemError() throws Exception {
  final String ksqlString = "CREATE STREAM test_explain AS SELECT * FROM test_stream;";
  // Set up a mock engine to mirror the returns of the real engine
  KsqlEngine mockEngine = EasyMock.niceMock(KsqlEngine.class);
  EasyMock.expect(mockEngine.getMetaStore()).andReturn(ksqlEngine.getMetaStore()).anyTimes();
  EasyMock.expect(mockEngine.getTopicClient()).andReturn(ksqlEngine.getTopicClient()).anyTimes();
  EasyMock.expect(
      mockEngine.getStatements(ksqlString)).andThrow(
          new RuntimeException("internal error"));
  EasyMock.replay(mockEngine);

  KsqlResource testResource = TestKsqlResourceUtil.get(mockEngine);
  Response response = handleKsqlStatements(
      testResource, new KsqlRequest(ksqlString, Collections.emptyMap()));
  assertThat(response.getStatus(), equalTo(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
  assertThat(response.getEntity(), instanceOf(KsqlErrorMessage.class));
  KsqlErrorMessage result = (KsqlErrorMessage)response.getEntity();
  assertThat(result.getErrorCode(), equalTo(Errors.ERROR_CODE_SERVER_ERROR));
  assertThat(result.getMessage(), containsString("internal error"));

  EasyMock.verify(mockEngine);
}
 
Example 2
Source File: JsonRpcBasicServerTest.java    From jsonrpc4j with MIT License 6 votes vote down vote up
/**
 * The {@link com.googlecode.jsonrpc4j.JsonRpcBasicServer} is able to have an instance of
 * {@link com.googlecode.jsonrpc4j.InvocationListener} configured for it.  Prior to a
 * method being invoked, the lister is notified and after the method is invoked, the
 * listener is notified.  This test checks that these two events are hit correctly in
 * the case that an exception is raised when the method is invoked.
 */

@Test
@SuppressWarnings("unchecked")
public void callMethodThrowingWithInvocationListener() throws Exception {
	final InvocationListener invocationListener = EasyMock.niceMock(InvocationListener.class);
	Method m = ServiceInterface.class.getMethod("throwsMethod", String.class);
	invocationListener.willInvoke(eq(m), anyObject(List.class));
	invocationListener.didInvoke(eq(m), anyObject(List.class), EasyMock.isNull(), EasyMock.<Throwable>notNull(), EasyMock.geq(0L));
	
	jsonRpcServer.setInvocationListener(invocationListener);
	
	EasyMock.expect(mockService.throwsMethod(param1)).andThrow(new CustomTestException(param1));
	EasyMock.replay(mockService, invocationListener);
	
	jsonRpcServer.handleRequest(messageWithListParamsStream(1, "throwsMethod", param1), byteArrayOutputStream);
	
	EasyMock.verify(invocationListener, mockService);
	
	JsonNode json = decodeAnswer(byteArrayOutputStream);
	assertNull(json.get(JsonRpcBasicServer.RESULT));
	assertNotNull(json.get(JsonRpcBasicServer.ERROR));
}
 
Example 3
Source File: JsonRpcBasicServerTest.java    From jsonrpc4j with MIT License 6 votes vote down vote up
/**
 * The {@link com.googlecode.jsonrpc4j.JsonRpcBasicServer} is able to have an instance of
 * {@link com.googlecode.jsonrpc4j.InvocationListener} configured for it.  Prior to a
 * method being invoked, the lister is notified and after the method is invoked, the
 * listener is notified.  This test checks that these two events are hit correctly
 * when a method is invoked.
 */

@SuppressWarnings("unchecked")
@Test
public void callMethodWithInvocationListener() throws Exception {
	final InvocationListener invocationListener = EasyMock.niceMock(InvocationListener.class);
	Method m = ServiceInterface.class.getMethod("throwsMethod", String.class);
	invocationListener.willInvoke(eq(m), anyObject(List.class));
	invocationListener.didInvoke(eq(m), anyObject(List.class), EasyMock.notNull(), EasyMock.<Throwable>isNull(), EasyMock.geq(0L));
	
	jsonRpcServer.setInvocationListener(invocationListener);
	
	EasyMock.expect(mockService.throwsMethod(param1)).andReturn(param1);
	EasyMock.replay(mockService, invocationListener);
	
	jsonRpcServer.handleRequest(messageWithListParamsStream(1, "throwsMethod", param1), byteArrayOutputStream);
	
	EasyMock.verify(invocationListener, mockService);
	
	JsonNode json = decodeAnswer(byteArrayOutputStream);
	assertEquals(param1, json.get(JsonRpcBasicServer.RESULT).textValue());
	assertNull(json.get(JsonRpcBasicServer.ERROR));
}
 
Example 4
Source File: JsonRpcBasicServerTest.java    From jsonrpc4j with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void callConvertedParameterTransformerShouldTransformTheParameters() throws Exception {
	final ConvertedParameterTransformer convertedParameterTransformer = EasyMock.niceMock(ConvertedParameterTransformer.class);
	
	String[] parameters = {param1, param2};
	String[] expectedConvertedParameters = {param2, param1};
	
	EasyMock.expect(mockService.overloadedMethod(param2, param1)).andReturn("converted");
	jsonRpcServer.setConvertedParameterTransformer(convertedParameterTransformer);
	
	EasyMock.expect(convertedParameterTransformer.transformConvertedParameters(anyObject(), anyObject(Object[].class))).andReturn(expectedConvertedParameters);
	EasyMock.replay(mockService, convertedParameterTransformer);
	
	jsonRpcServer.handleRequest(messageWithListParamsStream(1, "overloadedMethod", (Object[]) parameters), byteArrayOutputStream);
	
	JsonNode json = decodeAnswer(byteArrayOutputStream);
	assertEquals("converted", json.get(JsonRpcBasicServer.RESULT).textValue());
	assertNull(json.get(JsonRpcBasicServer.ERROR));
}
 
Example 5
Source File: KsqlResourceTest.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturn5xxOnStatementSystemError() throws Exception {
  final String ksqlString = "CREATE STREAM test_explain AS SELECT * FROM test_stream;";
  // Set up a mock engine to mirror the returns of the real engine
  KsqlEngine mockEngine = EasyMock.niceMock(KsqlEngine.class);
  EasyMock.expect(mockEngine.getMetaStore()).andReturn(ksqlEngine.getMetaStore()).anyTimes();
  EasyMock.expect(mockEngine.getTopicClient()).andReturn(ksqlEngine.getTopicClient()).anyTimes();
  EasyMock.replay(mockEngine);
  KsqlResource testResource = TestKsqlResourceUtil.get(mockEngine);

  EasyMock.reset(mockEngine);
  EasyMock.expect(
      mockEngine.getStatements(ksqlString)).andReturn(ksqlEngine.getStatements(ksqlString));
  EasyMock.expect(mockEngine.getQueryExecutionPlan(EasyMock.anyObject()))
      .andThrow(new RuntimeException("internal error"));
  EasyMock.replay(mockEngine);

  Response response = handleKsqlStatements(
      testResource, new KsqlRequest(ksqlString, Collections.emptyMap()));
  assertThat(response.getStatus(), equalTo(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
  assertThat(response.getEntity(), instanceOf(KsqlErrorMessage.class));
  KsqlErrorMessage result = (KsqlStatementErrorMessage)response.getEntity();
  assertThat(result.getErrorCode(), equalTo(Errors.ERROR_CODE_SERVER_ERROR));
  assertThat(result.getMessage(), containsString("internal error"));

  EasyMock.verify(mockEngine);
}
 
Example 6
Source File: StandaloneExecutorTest.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldExecutePersistentQueries() throws Exception {
  final PersistentQueryMetadata query = EasyMock.niceMock(PersistentQueryMetadata.class);
  EasyMock.expect(engine.createQueries(anyString())).andReturn(Collections.singletonList(query));
  query.start();
  EasyMock.expectLastCall();
  EasyMock.replay(query, engine);

  executor.start();

  EasyMock.verify(query);
}
 
Example 7
Source File: JsonRpcBasicServerTest.java    From jsonrpc4j with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void callConvertedParameterTransformerShouldBeCalledIfSet() throws Exception {
	final ConvertedParameterTransformer convertedParameterTransformer = EasyMock.niceMock(ConvertedParameterTransformer.class);
	
	EasyMock.expect(mockService.testMethod(param1)).andReturn(param1);
	jsonRpcServer.setConvertedParameterTransformer(convertedParameterTransformer);
	
	EasyMock.expect(convertedParameterTransformer.transformConvertedParameters(anyObject(), anyObject(Object[].class))).andReturn(new Object[]{param1});
	EasyMock.replay(convertedParameterTransformer);
	
	jsonRpcServer.handleRequest(messageWithListParamsStream(1, "testMethod", param1), byteArrayOutputStream);
	
	EasyMock.verify(convertedParameterTransformer);
}
 
Example 8
Source File: KsqlEngineMetricsTest.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
  MetricCollectors.initialize();
  ksqlEngine = EasyMock.niceMock(KsqlEngine.class);
  engineMetrics = new KsqlEngineMetrics(METRIC_GROUP, ksqlEngine);
}
 
Example 9
Source File: DhcpRelayManagerTest.java    From onos with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() {
    manager = new DhcpRelayManager();
    manager.cfgService = createNiceMock(NetworkConfigRegistry.class);

    expect(manager.cfgService.getConfig(APP_ID, DefaultDhcpRelayConfig.class))
            .andReturn(CONFIG)
            .anyTimes();

    expect(manager.cfgService.getConfig(APP_ID, IndirectDhcpRelayConfig.class))
            .andReturn(CONFIG_INDIRECT)
            .anyTimes();

    manager.coreService = createNiceMock(CoreService.class);
    expect(manager.coreService.registerApplication(anyString()))
            .andReturn(APP_ID).anyTimes();

    manager.hostService = createNiceMock(HostService.class);

    expect(manager.hostService.getHostsByIp(OUTER_RELAY_IP_V6))
            .andReturn(ImmutableSet.of(OUTER_RELAY_HOST)).anyTimes();
    expect(manager.hostService.getHostsByIp(SERVER_IP))
            .andReturn(ImmutableSet.of(SERVER_HOST)).anyTimes();
    expect(manager.hostService.getHostsByIp(SERVER_IP_V6))
            .andReturn(ImmutableSet.of(SERVER_HOST)).anyTimes();
    expect(manager.hostService.getHostsByIp(GATEWAY_IP))
            .andReturn(ImmutableSet.of(SERVER_HOST)).anyTimes();
    expect(manager.hostService.getHostsByIp(GATEWAY_IP_V6))
            .andReturn(ImmutableSet.of(SERVER_HOST)).anyTimes();
    expect(manager.hostService.getHostsByIp(CLIENT_LL_IP_V6))
            .andReturn(ImmutableSet.of(EXISTS_HOST)).anyTimes();

    expect(manager.hostService.getHost(OUTER_RELAY_HOST_ID)).andReturn(OUTER_RELAY_HOST).anyTimes();

    packetService = new MockPacketService();
    manager.packetService = packetService;
    manager.compCfgService = createNiceMock(ComponentConfigService.class);
    deviceService = createNiceMock(DeviceService.class);

    Device device = createNiceMock(Device.class);
    expect(device.is(Pipeliner.class)).andReturn(true).anyTimes();

    expect(deviceService.getDevice(DEV_1_ID)).andReturn(device).anyTimes();
    expect(deviceService.getDevice(DEV_2_ID)).andReturn(device).anyTimes();
    replay(deviceService, device);

    mockRouteStore = new MockRouteStore();
    mockDhcpRelayStore = new MockDhcpRelayStore();
    mockDhcpRelayCountersStore = new MockDhcpRelayCountersStore();

    manager.dhcpRelayStore = mockDhcpRelayStore;

    manager.deviceService = deviceService;

    manager.interfaceService = new MockInterfaceService();
    flowObjectiveService = EasyMock.niceMock(FlowObjectiveService.class);
    mockHostProviderService = createNiceMock(HostProviderService.class);
    v4Handler = new Dhcp4HandlerImpl();
    v4Handler.providerService = mockHostProviderService;
    v4Handler.dhcpRelayStore = mockDhcpRelayStore;
    v4Handler.hostService = manager.hostService;
    v4Handler.interfaceService = manager.interfaceService;
    v4Handler.packetService = manager.packetService;
    v4Handler.routeStore = mockRouteStore;
    v4Handler.coreService = createNiceMock(CoreService.class);
    v4Handler.flowObjectiveService = flowObjectiveService;
    v4Handler.appId = TestApplicationId.create(Dhcp4HandlerImpl.DHCP_V4_RELAY_APP);
    v4Handler.deviceService = deviceService;
    manager.v4Handler = v4Handler;

    v6Handler = new Dhcp6HandlerImpl();
    v6Handler.dhcpRelayStore = mockDhcpRelayStore;
    v6Handler.dhcpRelayCountersStore = mockDhcpRelayCountersStore;
    v6Handler.hostService = manager.hostService;
    v6Handler.interfaceService = manager.interfaceService;
    v6Handler.packetService = manager.packetService;
    v6Handler.routeStore = mockRouteStore;
    v6Handler.providerService = mockHostProviderService;
    v6Handler.coreService = createNiceMock(CoreService.class);
    v6Handler.flowObjectiveService = flowObjectiveService;
    v6Handler.appId = TestApplicationId.create(Dhcp6HandlerImpl.DHCP_V6_RELAY_APP);
    v6Handler.deviceService = deviceService;
    manager.v6Handler = v6Handler;

    // properties
    Dictionary<String, Object> dictionary = createNiceMock(Dictionary.class);
    expect(dictionary.get("arpEnabled")).andReturn(true).anyTimes();
    expect(dictionary.get("dhcpPollInterval")).andReturn(120).anyTimes();
    ComponentContext context = createNiceMock(ComponentContext.class);
    expect(context.getProperties()).andReturn(dictionary).anyTimes();

    replay(manager.cfgService, manager.coreService, manager.hostService,
           manager.compCfgService, dictionary, context);
    manager.activate(context);
}