use of org.apache.servicecomb.foundation.common.Holder in project incubator-servicecomb-java-chassis by apache.
the class TestSwaggerLoader method mockLocalResource.
private void mockLocalResource(Map<String, String> resourceMap) {
Holder<String> pathHolder = new Holder<>();
URL url = new MockUp<URL>() {
@Mock
String getPath() {
return pathHolder.value;
}
@Mock
String toExternalForm() {
return pathHolder.value;
}
}.getMockInstance();
ClassLoader classLoader = new MockUp<ClassLoader>() {
@Mock
URL getResource(String name) {
pathHolder.value = name;
return resourceMap.containsKey(name) ? url : null;
}
@Mock
InputStream getResourceAsStream(String name) {
String content = resourceMap.get(name);
return content == null ? null : new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
}
}.getMockInstance();
new Expectations(JvmUtils.class) {
{
JvmUtils.findClassLoader();
result = classLoader;
}
};
new MockUp<IOUtils>() {
@Mock
String toString(URL url, Charset encoding) {
return resourceMap.get(url.getPath());
}
};
}
use of org.apache.servicecomb.foundation.common.Holder in project incubator-servicecomb-java-chassis by apache.
the class TestMicroserviceWatchTask method testWatchFailed.
@Test
public void testWatchFailed(@Mocked ServiceRegistryConfig serviceRegistryConfig, @Mocked ServiceRegistryClient srClient, @Mocked Microservice microservice) {
initWatch(serviceRegistryConfig, srClient, microservice);
new MockUp<ServiceRegistryClient>(srClient) {
@Mock
void watch(String selfMicroserviceId, AsyncResultCallback<MicroserviceInstanceChangedEvent> callback, AsyncResultCallback<Void> onOpen, AsyncResultCallback<Void> onClose) {
callback.fail(new RuntimeExceptionWithoutStackTrace("test failed"));
}
};
Holder<Throwable> holder = new Holder<>();
eventBus.register(new Object() {
@Subscribe
public void onException(ExceptionEvent event) {
holder.value = event.getThrowable();
}
});
Assert.assertNull(holder.value);
microserviceWatchTask.run();
Assert.assertEquals("test failed", holder.value.getMessage());
}
use of org.apache.servicecomb.foundation.common.Holder in project incubator-servicecomb-java-chassis by apache.
the class TestMicroserviceWatchTask method testWatchInstanceChanged.
@Test
public void testWatchInstanceChanged(@Mocked ServiceRegistryConfig serviceRegistryConfig, @Mocked ServiceRegistryClient srClient, @Mocked Microservice microservice) {
initWatch(serviceRegistryConfig, srClient, microservice);
MicroserviceInstanceChangedEvent changedEvent = new MicroserviceInstanceChangedEvent();
MicroserviceKey key = new MicroserviceKey();
key.setAppId(microservice.getAppId());
key.setVersion(microservice.getVersion());
key.setServiceName(microservice.getServiceName());
changedEvent.setKey(key);
changedEvent.setInstance(microservice.getInstance());
new MockUp<ServiceRegistryClient>(srClient) {
@Mock
void watch(String selfMicroserviceId, AsyncResultCallback<MicroserviceInstanceChangedEvent> callback, AsyncResultCallback<Void> onOpen, AsyncResultCallback<Void> onClose) {
callback.success(changedEvent);
}
};
Holder<MicroserviceInstanceChangedEvent> holder = new Holder<>();
eventBus.register(new Object() {
@Subscribe
public void onException(MicroserviceInstanceChangedEvent event) {
holder.value = event;
}
});
changedEvent.setAction(WatchAction.CREATE);
microserviceWatchTask.run();
Assert.assertEquals(WatchAction.CREATE, holder.value.getAction());
changedEvent.setAction(WatchAction.DELETE);
microserviceWatchTask.run();
Assert.assertEquals(WatchAction.DELETE, holder.value.getAction());
changedEvent.setAction(WatchAction.UPDATE);
microserviceWatchTask.run();
Assert.assertEquals(WatchAction.UPDATE, holder.value.getAction());
}
use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.
the class EndpointTest method should_set_endpoint_to_invocation_when_map_arguments.
@Test
void should_set_endpoint_to_invocation_when_map_arguments() {
SwaggerEnvironment environment = new SwaggerEnvironment();
SwaggerConsumer consumer = environment.createConsumer(TestSchema.class, SwaggerGenerator.generate(TestSchema.class));
SwaggerConsumerOperation operation = consumer.findOperation("say");
Endpoint endpoint = new Endpoint(Mockito.mock(Transport.class), null);
Invocation invocation = Mockito.mock(Invocation.class);
Holder<Object> holder = new Holder<>();
Mockito.doAnswer(invocationOnMock -> {
holder.value = invocationOnMock.getArguments()[0];
return null;
}).when(invocation).setEndpoint(Mockito.any());
Map<String, Object> argsMap = of("endpoint", endpoint);
operation.getArgumentsMapper().invocationArgumentToSwaggerArguments(invocation, argsMap);
assertThat(holder.value).isSameAs(endpoint);
}
use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.
the class TestGeneric method testHolderUser_rt.
@Test
public void testHolderUser_rt() {
Holder<User> holder = new Holder<>(new User());
@SuppressWarnings("unchecked") Holder<User> result = consumers.getSCBRestTemplate().postForObject("/holderUser", holder, Holder.class);
assertEquals(result.value.jsonString(), expectUserStr);
}
Aggregations