REST-Driver ClientDriverRule error on instantiation - java

I decided to try the rest-driver library for my unit tests.
I have the following code:
public class RemoteSessionAccountantTest {
#Rule
public ClientDriverRule driver = new ClientDriverRule();
// ...
#Test
public void singleUserSession() throws IOException, JSONException {
driver.addExpectation(
onRequestTo("http://myTestUrl/anyUsername").withMethod(Method.GET),
giveResponse("[{\"contractCode\":\"1234\"}]", "application/json"));
// ...
}
// ...
}
But I'm not even reaching the test case because instantiating the ClientDriverRule results in an error:
com.github.restdriver.clientdriver.exception.ClientDriverSetupException:
Error starting jetty on port 0
...
Caused by: java.lang.IllegalStateException: Local port was not set
Now I've tried setting a specific port, but it didn't help.
I have jetty v9.3.0 (also tried versions 8.x.x, 9.1.x, 9.2.x with no luck)
rest-client-driver version 1.1.42
The problem seems to be coming from jetty, but I cannot figure out what exactly.

Related

Testcontainers MS SQL Server Module stays in a loop and never enters the test

I'm writting a JUnit4 test case with the following:
#Rule
public MSSQLServerContainer mssqlserver = new MSSQLServerContainer().acceptLicense();
#Before
public void setUp() throws Exception
{
url = mssqlserver.getJdbcUrl();
}
#Test
public void someTestMethod() {
...
But it hangs a long time and then this exception is thrown:
java.lang.IllegalStateException: Container is started, but cannot be accessed by (JDBC URL: jdbc:sqlserver://localhost:51772), please check container logs
What's wrong?
I'm using these dependencies:
testImplementation "org.testcontainers:testcontainers:1.16.3"
testImplementation "org.testcontainers:mssqlserver:1.16.3"
It looks like there's a fix on the way [1]. I had to use the workaround of
.withUrlParam("trustServerCertificate", "true")
mentioned there with testcontainers 1.16.3.
[1] https://github.com/testcontainers/testcontainers-java/issues/5032

Hazelcast on deployed servers throws java.lang.ClassNotFoundException and locally not

I wanted to upgrade to Java 11 and tomcat 9 my spring boot application that uses Hazelcast 3.12.9 as cashing mechanism. When I deployed locally everything looks to work fine and the caching successfully works. But when the application runs on the cluster, I receive from all 3 nodes that are available the following error:
com.hazelcast.nio.serialization.HazelcastSerializationException: java.lang.ClassNotFoundException: com.some.service.some.server.domain.ClassA
at com.hazelcast.internal.serialization.impl.JavaDefaultSerializers$JavaSerializer.read(JavaDefaultSerializers.java:88)
at com.hazelcast.internal.serialization.impl.JavaDefaultSerializers$JavaSerializer.read(JavaDefaultSerializers.java:77)
at com.hazelcast.internal.serialization.impl.StreamSerializerAdapter.read(StreamSerializerAdapter.java:48)
at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toObject(AbstractSerializationService.java:187)
at com.hazelcast.map.impl.proxy.MapProxySupport.toObject(MapProxySupport.java:1237)
at com.hazelcast.map.impl.proxy.MapProxyImpl.get(MapProxyImpl.java:120)
at com.hazelcast.spring.cache.HazelcastCache.lookup(HazelcastCache.java:162)
at com.hazelcast.spring.cache.HazelcastCache.get(HazelcastCache.java:67)
at com.some.service.some.server.domain.ClassACache.get(GlassACache.java:28)
at com.some.service.some.server.domain.ClassAFacade.getClassA(ClassAFacade.java:203)
at com.some.service.some.server.domain.ClassAFacade.getGlassA(ClassAFacade.java:185)
at com.some.service.some.server.domain.ClassALogic.lambda$getClassAInParallel$1(ClassALogic.java:196)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1655)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ReduceOps$ReduceTask.doLeaf(ReduceOps.java:952)
at java.base/java.util.stream.ReduceOps$ReduceTask.doLeaf(ReduceOps.java:926)
at java.base/java.util.stream.AbstractTask.compute(AbstractTask.java:327)
at java.base/java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:746)
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)
Caused by: java.lang.ClassNotFoundException: com.some.service.some.server.domain.ClassA
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at com.hazelcast.nio.ClassLoaderUtil.tryLoadClass(ClassLoaderUtil.java:288)
Hazelcast customizer :
#Configuration
public class ClassAHazelcastConfig {
private static final MaxSizePolicy HAZELCAST_DEFAULT_MAX_SIZE_POLICY = MaxSizePolicy.PER_NODE;
private static final EvictionPolicy HAZELCAST_DEFAULT_EVICTION_POLICY = EvictionPolicy.LRU;
#Bean
HazelcastConfigurationCustomizer customizer(CachePropertiesHolder cacheProperties) {
return config -> {
config.addMapConfig(new MapConfig()
.setName(CLASS_A_CACHE)
.setMaxSizeConfig(new MaxSizeConfig(cacheProperties.getMaxsize(), HAZELCAST_DEFAULT_MAX_SIZE_POLICY))
.setEvictionPolicy(HAZELCAST_DEFAULT_EVICTION_POLICY)
.setTimeToLiveSeconds(cacheProperties.getTtl()));
config.getSerializationConfig().addSerializerConfig(
new SerializerConfig()
.setImplementation(new OptionalStreamSerializer())
.setTypeClass(Optional.class)
);
};
}
}
#Configuration
#EnableConfigurationProperties(CachePropertiesHolder.class)
public class CacheConfig implements CachingConfigurer, EnvironmentAware, ApplicationContextAware {
public static final String CLASS_A_CACHE = "CACHE_A";
private Environment environment;
private ApplicationContext applicationContext;
#Override
#Bean(name="cacheManager")
public CacheManager cacheManager() {
boolean cachingEnabled = Boolean.parseBoolean(environment.getProperty("cache.enabled"));
if (cachingEnabled) {
HazelcastInstance instance = (HazelcastInstance) applicationContext.getBean("hazelcastInstance");
return new HazelcastCacheManager(instance);
}
return new NoOpCacheManager();
}
#Override
public CacheResolver cacheResolver() {
return new SimpleCacheResolver(Objects.requireNonNull(cacheManager()));
}
#Bean
#Override
public KeyGenerator keyGenerator() {
return new SimpleKeyGenerator();
}
#Bean
#Override
public CacheErrorHandler errorHandler() {
return new SimpleCacheErrorHandler();
}
#Override
public void setEnvironment(#NotNull Environment environment) {
this.environment = environment;
}
#Override
public void setApplicationContext(#NotNull ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
Everything works properly fine with Java 8 and tomcat 8.
Update:
After some days of investigation, I see that the only place that these exceptions are thrown into a parallel stream that is used.
return forkJoinPool.submit(() ->
items.parallelStream()
.map(item -> {
try {
return biFunction.apply(item);
} catch (Exception e) {
LOG.error("Error", e);
return Optional.<Item>empty();
}
})
The weird thing is that with Java 8 and tomcat 8 I did not have that issue.
Eventually, it is completely unrelated to Hazelcast. Mostly it is Java 11 difference from Java 8.
In the part that the exception was thrown a ForkJoinPool was used which from Java 11, it is not guaranteed when exactly will be created and it seems to not have the same classloader as the spring application. (Classes with default access result in NoClassDefFound error at runtime in spring boot project for java 11)
I made a wrong assumption because the exception was coming from Hazelcast and I also saw that there were other issues related.
Hazelcast offers two deployment models: embedded mode and client-server mode. Please check the relevant documentation section for more information.
In the former case, you have a single JVM and all of your classes are on your classpath.
In the latter case, you have at least 2 JVMs, one for each client and one for the member. It seems you forgot to set up the classpath of the member to reference ClassA.
How to set the classpath depends on how you launch your Hazelcast members. The method that works in most cases is to use the CLASSPATH environment variable.

Java 11 junit jupiter assertThrows

I try to migrate from Java 8 to 11 and get an error in my test class that I don't understand.
My failing (groovy) test is:
#SpringJUnitConfig
class TestSpringBeanScopeChecker {
#Autowired
ApplicationContext ctx
#Test
void testSingletonFail() {
Assertions.assertThrows(IllegalStateException.class) {
SpringBeanScopeChecker.check(ctx, DummyPrototype.class, BeanDefinition.SCOPE_SINGLETON)
}
}
}
The SpringBeanScopeChecker:
public class SpringBeanScopeChecker {
private SpringBeanScopeChecker() {}
public static void check(ApplicationContext ctx, Class<?> type, String scope)
throws IllegalStateException {
AbstractApplicationContext actx = (ctx instanceof AbstractApplicationContext) ?
((AbstractApplicationContext) ctx) :
new StaticApplicationContext(ctx);
ConfigurableListableBeanFactory factory = actx.getBeanFactory();
for (String key : ctx.getBeanNamesForType(type)) {
BeanDefinition definition = factory.getMergedBeanDefinition(key);
if (!scope.equals(definition.getScope())) {
throw new IllegalStateException(
"Every spring bean "
+ "must be request scoped in the bean configuration. The current scope is: "
+ definition.getScope());
}
}
}
}
So for the test I'm expecting a IllegalArgumentException. And this is working fine with Java8.
When I switch to Java11 and execute the test I get this error:
[ERROR] testSingletonFail Time elapsed: 0.009 s <<< FAILURE!
org.opentest4j.AssertionFailedError: Unexpected exception type thrown
==> expected: <java.lang.IllegalStateException> but was: <java.lang.AbstractMethodError>
at TestSpringBeanScopeChecker.testSingletonFail(TestSpringBeanScopeChecker.groovy:22)
Caused by: java.lang.AbstractMethodError: Receiver class
TestSpringBeanScopeChecker does not define or inherit an
implementation of the resolved method 'abstract java.lang.Object
getProperty(java.lang.String)' of interface groovy.lang.GroovyObject.
at TestSpringBeanScopeChecker.testSingletonFail(TestSpringBeanScopeChecker.groovy:22)
In case someone else has the same problem I write down the solution for this.
The problem was misconfiguration of the groovy-eclipse-compiler and groovy-eclipse-batch.
My groovy version is managed by spring-boot and I didn't update the groovy-eclipse-batch according to the groovy.version from the spring-boot pom.
According to this issue on github:
You have to compile with groovy-eclipse-batch and groovy runtime in the same version. groovy-eclipse-batch and groovy runtime should be matched up. E.g. batch 2.5.10-0x and runtime 2.5.10 or batch 3.0.1-0x and runtime 3.0.1.

Kafka Storm Integration using Kafka Spout

I am using KafkaSpout. Please find the test program below.
I am using Storm 0.8.1. Multischeme class is there in Storm 0.8.2. I will be using that. I just want to know how were the earlier versions working just by instantiating the StringScheme() class? Where can I download earlier versions of Kafka Spout? But I doubt that would be a correct alternative than to work on Storm 0.8.2. ??? (Confused)
When I run the code (given below) on storm cluster (i.e. when I push my topology) I get the following error (This happens when the Scheme part is commented else of course I will get compiler error as the class is not there in 0.8.1):
java.lang.NoClassDefFoundError: backtype/storm/spout/MultiScheme
at storm.kafka.TestTopology.main(TestTopology.java:37)
Caused by: java.lang.ClassNotFoundException: backtype.storm.spout.MultiScheme
In the code given below you may find the spoutConfig.scheme=new StringScheme(); part commented. I was getting compiler error if I don't comment that line which is but natural as there are no constructors in there. Also when I instantiate MultiScheme I get error as I dont have that class in 0.8.1.
public class TestTopology {
public static class PrinterBolt extends BaseBasicBolt {
public void declareOutputFields(OutputFieldsDeclarer declarer) {
}
public void execute(Tuple tuple, BasicOutputCollector collector) {
System.out.println(tuple.toString());
}
}
public static void main(String [] args) throws Exception {
List<HostPort> hosts = new ArrayList<HostPort>();
hosts.add(new HostPort("127.0.0.1",9092));
LocalCluster cluster = new LocalCluster();
TopologyBuilder builder = new TopologyBuilder();
SpoutConfig spoutConfig = new SpoutConfig(new KafkaConfig.StaticHosts(hosts, 1), "test", "/zkRootStorm", "STORM-ID");
spoutConfig.zkServers=ImmutableList.of("localhost");
spoutConfig.zkPort=2181;
//spoutConfig.scheme=new StringScheme();
spoutConfig.scheme = new SchemeAsMultiScheme(new StringScheme());
builder.setSpout("spout",new KafkaSpout(spoutConfig));
builder.setBolt("printer", new PrinterBolt())
.shuffleGrouping("spout");
Config config = new Config();
cluster.submitTopology("kafka-test", config, builder.createTopology());
Thread.sleep(600000);
}
I had the same problem. Finally resolved it, and I put the complete running example up on github.
You are welcome to check it out here >
https://github.com/buildlackey/cep
(click on the storm+kafka directory for a sample program that should get you up and running).
We had a similar issue.
Our solution:
Open pom.xml
Change scope from provided to <scope>compile</scope>
If you want to know more about dependency scopes check the maven docu:
Maven docu - dependency scopes

Java class not found errors while running

I have a class that is having some dependency problems referencing external library files. Every time I try to run this on the server I get errors saying class not found, such as this:
SEVERE: Class [ org/json/JSONException ] not found. Error while loading [ class com.myproj.logic.Driver ]
this is preventing the class from executing. I tried taking out the specific throws execption by just saying "throws exception" and got the following error:
WARNING: A system exception occurred during an invocation on EJB Driver method public void com..logic.Driver.initURL() throws java.lang.Exception
javax.ejb.EJBException: javax.ejb.CreateException: Initialization failed for Singleton Driver
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils
#Singleton
public class Driver {
#EJB RSSbean rssbean;
#PostConstruct
#Schedule(hour ="*/1", minute ="*/1", second ="*/1", persistent = false)
public void initURL() throws IOException, JSONException{
URL twitterSource = new URL("http://search.twitter.com/search.json?q=news");
ByteArrayOutputStream urlOutputStream = new ByteArrayOutputStream();
IOUtils.copy(twitterSource.openStream(), urlOutputStream);
String urlContents = urlOutputStream.toString();
JSONObject thisobject = new JSONObject(urlContents);
JSONArray names = thisobject.names();
JSONArray asArray = thisobject.toJSONArray(names);
JSONArray resultsArray = thisobject.getJSONArray("results");
JSONObject jsonObject = resultsArray.getJSONObject(0);
String twitterText = jsonObject.getString("text");
//System.out.println(twitterText);
System.out.println("Calling rssbean from Driver");
rssbean.updateDatabase("twitterText");
}
}
I have edited the Java classpath and added a user library for each of these as well as editing the Build Path of the project. The libraries are displayed in the list and I don't get compiler errors so at least Eclipse has recognized them. The problem comes during execution so I think something is wrong there.
Should I edit the classpath in Windows>Preferences>Java>Classpath> and add the Jars there? I have not had to do this for any other libraries before.
I found out you have to add any library files in the Glassfish/domain/lib directory before they're recognized by glassfish on the server.

Categories

Resources