I have seen several answers and documentation on how to use operators, but cannot get this to work. I want to know where I can find use case on TruncateDate OPERATOR. I have looked at the following and they are not helpful:
http://www.eclipse.org/eclipselink/api/2.5/org/eclipse/persistence/expressions/Expression.html#truncateDate(java.lang.String)
https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#Functions
What I need is just a simple query that works with the TruncateDate operator. Here is my query:
#NamedQueries({
#NamedQuery(name="SprMessage.findPendingMessageIds",
query="SELECT s.messageId "
+ "FROM SprMessage s "
+ "WHERE s.inOrOut = 'INBOUND'"
+ "AND s.messageStatus = 'RECEIVED'"
+ "OR (s.messageStatus = 'RETRY' AND s.lastUpdateDate < OPERATOR('TruncateDate', :todayIn)) "
+ "ORDER BY s.createdDate ASC")
})
If I replace OPERATOR('TruncateDate', :todayIn) with :todayIn it works. Is this even possible with a parameter? My issue is I do not want to check seconds and minutes, etc. Just down to the day.
Thank you in advance for any help on the matter.
Additional information on my JPA version:
compile (group: 'org.eclipse.persistence', name: 'eclipselink', version: '2.5.1')
compile (group: 'org.eclipse.persistence', name: 'javax.persistence', version: '2.1.0', ext: 'jar')
I could do this in code with JODA time but I would really like to see this work in JPA for the sake of IT.
Due to a comment about database system, this is being run against an Oracle 11g database, as well as a Derby database for the JUnits which are failing.
Here is the error I am getting running:
Local Exception Stack:
Exception [EclipseLink-6047] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.QueryException
Exception Description: Invalid operator [platform operator - TruncateDate] in expression.
Query: ReportQuery(name="SprMessage.findPendingMessageIds" referenceClass=SprMessage jpql="SELECT s.messageId FROM SprMessage s WHERE s.inOrOut = 'INBOUND'AND s.messageStatus = 'RECEIVED'OR (s.messageStatus = 'RETRY' AND s.lastUpdateDate < OPERATOR('TruncateDate', :todayIn, 'dd')) ORDER BY s.createdDate ASC")
at org.eclipse.persistence.exceptions.QueryException.invalidOperator(QueryException.java:664)
at org.eclipse.persistence.internal.expressions.FunctionExpression.initializePlatformOperator(FunctionExpression.java:330)
at org.eclipse.persistence.internal.expressions.FunctionExpression.getPlatformOperator(FunctionExpression.java:307)
at org.eclipse.persistence.internal.expressions.FunctionExpression.printSQL(FunctionExpression.java:544)
at org.eclipse.persistence.expressions.ExpressionOperator.printDuo(ExpressionOperator.java:2239)
at org.eclipse.persistence.internal.expressions.CompoundExpression.printSQL(CompoundExpression.java:286)
at org.eclipse.persistence.internal.expressions.RelationExpression.printSQL(RelationExpression.java:872)
at org.eclipse.persistence.expressions.ExpressionOperator.printDuo(ExpressionOperator.java:2239)
at org.eclipse.persistence.internal.expressions.CompoundExpression.printSQL(CompoundExpression.java:286)
at org.eclipse.persistence.expressions.ExpressionOperator.printDuo(ExpressionOperator.java:2239)
at org.eclipse.persistence.internal.expressions.CompoundExpression.printSQL(CompoundExpression.java:286)
at org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter.translateExpression(ExpressionSQLPrinter.java:306)
at org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter.printExpression(ExpressionSQLPrinter.java:129)
at org.eclipse.persistence.internal.expressions.SQLSelectStatement.printSQL(SQLSelectStatement.java:1683)
at org.eclipse.persistence.platform.database.DerbyPlatform.printSQLSelectStatement(DerbyPlatform.java:373)
at org.eclipse.persistence.internal.expressions.SQLSelectStatement.buildCall(SQLSelectStatement.java:782)
at org.eclipse.persistence.internal.expressions.SQLSelectStatement.buildCall(SQLSelectStatement.java:792)
at org.eclipse.persistence.descriptors.ClassDescriptor.buildCallFromStatement(ClassDescriptor.java:813)
at org.eclipse.persistence.internal.queries.StatementQueryMechanism.setCallFromStatement(StatementQueryMechanism.java:390)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.prepareReportQuerySelectAllRows(ExpressionQueryMechanism.java:1696)
at org.eclipse.persistence.queries.ReportQuery.prepareSelectAllRows(ReportQuery.java:1203)
at org.eclipse.persistence.queries.ReadAllQuery.prepare(ReadAllQuery.java:744)
at org.eclipse.persistence.queries.ReportQuery.prepare(ReportQuery.java:1071)
at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:661)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrepare(ObjectLevelReadQuery.java:901)
at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:613)
at org.eclipse.persistence.internal.jpa.QueryImpl.getDatabaseQueryInternal(QueryImpl.java:341)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createNamedQuery(EntityManagerImpl.java:1124)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createNamedQuery(EntityManagerImpl.java:1144)
at com.paychex.corp.app2app.iatosfdc.WarTransactionsHandler.getPendingMessageIds(WarTransactionsHandler.java:102)
at com.paychex.corp.app2app.iatosfdc.WarTransactionsHandlerTest.testGetPendingMessageBatchGetsMessagesById(WarTransactionsHandlerTest.java:102)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Related
I am receiving the following exception when trying to use java mail;
java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
at javax.mail.Session.initLogger(Session.java:226)
at javax.mail.Session.<init>(Session.java:210)
at javax.mail.Session.getInstance(Session.java:247)
at com.secondstory.mailsender.MailSender.createSmtpSession(MailSender.java:67)
at com.secondstory.mailsender.MailSender.sendSimpleMessage(MailSender.java:38)
at com.secondstory.mailsender.MailSender.generateLostPasswordEmail(MailSender.java:79)
at com.secondstory.mailsender.MailSenderTest.shouldReturnTrueWithCredentialsSet(MailSenderTest.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 30 more
After searching stack overflow extensively (maybe not extensively enough - lets see!) I have found that we need two jars inside our maven pom for this too work. The two dependancies I have are as follows;
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
Something has changed but I am not entirely sure what it is - as this worked previously. The code i have written where this element fails is as follows;
private static Session createSmtpSession() {
final Properties props = new Properties();
props.setProperty("mail.host", "host");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.starttls.enable", "true");
props.setProperty("mail.transport.protocol", "smtp");
//props.setProperty("mail.debug", "true");
return Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"username", "password");
}
});
}
Am I missing some config or should this work with the current setup that I have?
Thanks
Try adding this into Maven POM:
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.2</version>
</dependency>
The problem is that you are trying to use javax.mail-api.jar. That is the wrong library. JavaMail is a Java EE specification, the interfaces of that specification are published in that javax.mail-api-1.6.1.jar, which only works when compiling. It doesn't provide an implementation of the specification, so it doesn't work at runtime.
You need to use an implementation of the JavaMail specification at runtime. You can find the reference implementation on https://javaee.github.io/javamail/ (but there are others, for example Java EE application servers usually include one).
For javax.mail-api.jar, https://javaee.github.io/javamail/ says:
The JavaMail API definitions only, suitable for compiling against; use
only with a Maven “provided” dependency scope
Specifically in your case you need either javax.mail.jar or mailapi.jar + the jars of the specific protocols you want to use. For example mailapi.jar + smtp.jar if you only need smtp(s) support.
With Maven, you can use
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.1</version>
</dependency>
I am trying to implement the DSL API example from the Apache Beam documentation. I am using the newest versions of the apache beam libraries (2.4.0)
The code I am running is the same as in the docs:
#Rule
public final transient TestPipeline p = TestPipeline.create();
#Test
public void dslTest() {
RowType appType = RowSqlType
.builder()
.withIntegerField("appId")
.withVarcharField("description")
.withTimestampField("rowtime")
.build();
// Create a concrete row with that type.
Row row = Row.withRowType(appType)
.addValues(1, "Some cool app", new Date())
.build();
// Create a source PCollection containing only that row
PCollection<Row> testApps = PBegin
.in(p)
.apply(Create
.of(row)
.withCoder(appType.getRowCoder()));
PCollection<Row> filteredNames = testApps.apply(
BeamSql.query(
"SELECT appId, description, rowtime "
+ "FROM PCOLLECTION "
+ "WHERE appId=1"));
}
This always fails with the following error:
java.lang.AssertionError
at org.apache.beam.sdks.java.extensions.sql.repackaged.org.apache.calcite.plan.volcano.VolcanoPlanner.changeTraits(VolcanoPlanner.java:546)
at org.apache.beam.sdks.java.extensions.sql.repackaged.org.apache.calcite.tools.Programs$RuleSetProgram.run(Programs.java:365)
at org.apache.beam.sdks.java.extensions.sql.repackaged.org.apache.calcite.prepare.PlannerImpl.transform(PlannerImpl.java:336)
at org.apache.beam.sdk.extensions.sql.impl.planner.BeamQueryPlanner.convertToBeamRel(BeamQueryPlanner.java:165)
at org.apache.beam.sdk.extensions.sql.impl.planner.BeamQueryPlanner.validateAndConvert(BeamQueryPlanner.java:156)
at org.apache.beam.sdk.extensions.sql.impl.planner.BeamQueryPlanner.convertToBeamRel(BeamQueryPlanner.java:144)
at org.apache.beam.sdk.extensions.sql.QueryTransform.expand(QueryTransform.java:73)
at org.apache.beam.sdk.extensions.sql.QueryTransform.expand(QueryTransform.java:47)
at org.apache.beam.sdk.Pipeline.applyInternal(Pipeline.java:537)
at org.apache.beam.sdk.Pipeline.applyTransform(Pipeline.java:472)
at org.apache.beam.sdk.values.PCollection.apply(PCollection.java:286)
at PipelineTest.dslTest(PipelineTest.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.apache.beam.sdk.testing.TestPipeline$1.evaluate(TestPipeline.java:324)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
What is the correct way to run this test, or is this a bug and the dsl apis are not working?
It looks like a known issue, there's an assertion inside a planner which Beam fails to satisfy, see this jira. The fix for it is not ready at the moment.
Current workaround is to disable assertions, depending on your build system:
if you're using gradle, then in build.gradle you will have something like this:
test {
jvmArgs "-da"
}
if you're using maven, then in pom.xml you will have something like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-da</argLine>
I've written a test Method. In this project I use Morphia for MongoDB. But when I start the Method I get the follow Error:
java.lang.NoSuchMethodError: org.apache.log4j.Logger.log(Ljava/lang/String;Lorg/apache/log4j/Level;Ljava/lang/Object;Ljava/lang/Throwable;)V
at org.slf4j.impl.Log4jLoggerAdapter.info(Log4jLoggerAdapter.java:166)
at com.mongodb.diagnostics.logging.SLF4JLogger.info(SLF4JLogger.java:71)
at com.mongodb.connection.SingleServerCluster.<init>(SingleServerCluster.java:45)
at com.mongodb.connection.DefaultClusterFactory.create(DefaultClusterFactory.java:85)
at com.mongodb.Mongo.createCluster(Mongo.java:670)
at com.mongodb.Mongo.createCluster(Mongo.java:656)
at com.mongodb.Mongo.<init>(Mongo.java:278)
at com.mongodb.Mongo.<init>(Mongo.java:274)
at com.mongodb.MongoClient.<init>(MongoClient.java:174)
at com.mongodb.MongoClient.<init>(MongoClient.java:151)
at com.mongodb.MongoClient.<init>(MongoClient.java:141)
at de.meinTellerchen.utils.mongoDB.connection.MongoDBCon.<init>(MongoDBCon.java:65)
at de.meinTellerchen.ingredient.service.IngredientRestService.dataBaseConnection(IngredientRestService.java:34)
at de.meinTellerchen.ingredient.service.IngredientRestService.<init>(IngredientRestService.java:22)
at de.meinTellerchen.ingredient.service.IngredientRestServiceTest.test001_WriteIngredient(IngredientRestServiceTest.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
The Method in this Test creates an Object and saves it in a MongoDB with Morphia.
#Test
public void test001_WriteIngredient() {
Ingredient ingredient = generateIngredient();
IngredientRestService ingredientRestService = new IngredientRestService();
assertNotNull(ingredient);
Response response = ingredientRestService.writeIngredient(ingredient);
assertNotNull(response);
}
I don't know why it doesn't work. I don't use Logger.
The issue is with the version of log4j and slf4j.
Add below mentioned jars in your classpath :
log4j
slf4j-simple
jcl-over-slf4j
slf4j-api
slf4j-log4j12 with appropriate versions
or use maven to handle your dependencies.
Example of Compatible versions of jars:
1) log4j-1.2.15.jar
2) slf4j-api-1.7.7.jar
3) commons-logging-1.0.4.jar
4) logback-classic-1.0.0.jar
5) logback-core-1.0.0.jar
The dependent jar of slf4j is missing in your classpath.
Please open the mongodriver jar available in your system and see the meta-inf/MANIFEST.MF file and add the required jars mentioned in Import-Package:section.
Example:
Mongodb java driver 3.2.1 requires following dependent jars.
Import-Package: javax.xml.bind,javax.crypto,javax.crypto.spec,javax.ma
nagement,javax.net,javax.net.ssl,javax.security.sasl,javax.security.a
uth.callback,org.ietf.jgss,io.netty.bootstrap;resolution:=optional;ve
rsion="[4.0,5)",io.netty.buffer;resolution:=optional;version="[4.0,5)
",io.netty.channel;resolution:=optional;version="[4.0,5)",io.netty.ch
annel.nio;resolution:=optional;version="[4.0,5)",io.netty.channel.soc
ket;resolution:=optional;version="[4.0,5)",io.netty.channel.socket.ni
o;resolution:=optional;version="[4.0,5)",io.netty.handler.ssl;resolut
ion:=optional;version="[4.0,5)",io.netty.handler.timeout;resolution:=
optional;version="[4.0,5)",io.netty.util.concurrent;resolution:=optio
nal;version="[4.0,5)",org.slf4j;resolution:=optional;version="[1.7,2)
"
I am trying to use simple java (minus any framework like Spring or containers like Tomcat etc) to connect to DB2 on mainframe. Here are the steps I took.
Added the db2 drivers to my local maven repository.
mvn install:install-file -Dfile=db2jcc_license_cisuz.jar -DgroupId=com.ibm.db2 -DartifactId=db2jcc -Dversion=1.0 -Dpackaging=jar
mvn install:install-file -Dfile=db2jcc4.jar -DgroupId=com.ibm.db2 -DartifactId=db2jcc4 -Dversion=1.0 -Dpackaging=jar
Created a simple java project using maven archetype. Added the following to the pom.xml
<!-- Connect to DB2 on Mainframe. -->
<dependency>
<groupId>com.ibm.db2</groupId>
<artifactId>db2jcc </artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.ibm.db2</groupId>
<artifactId>db2jcc4 </artifactId>
<version>1.0</version>
</dependency>
Now created a simple Junit and added the following
Driver driver = (Driver) Class.forName("com.ibm.db2.jcc.DB2Driver")
.newInstance();
DriverManager.registerDriver(driver);
Connection connection = DriverManager.getConnection(
"jdbc:db2://server:port/dbname", "username",
"password");
This does not work. It tells me
com.ibm.db2.jcc.am.SqlInvalidAuthorizationSpecException: [jcc][t4][2010][11246][4.13.80] Connection authorization failure occurred. Reason: Local security service non-retryable error. ERRORCODE=-4214, SQLSTATE=28000
I tried changing the driver type as well.
Class.forName("COM.ibm.db2os390.sqlj.jdbc.DB2SQLJDriver");
But did not work. Still same problem.
I am working off a Windows machine and I have made no configuration for connecting to DB2 at all.
However, from the same Windows machine, using IBM Datastudio, and using the same credentials, I am able to connect to the database and run queries.
The stacktrace
com.ibm.db2.jcc.am.SqlInvalidAuthorizationSpecException: [jcc][t4][2010][11246][3.62.80] Connection authorization failure occurred. Reason: Local security service non-retryable error. ERRORCODE=-4214, SQLSTATE=28000
at com.ibm.db2.jcc.am.fd.a(fd.java:674)
at com.ibm.db2.jcc.am.fd.a(fd.java:60)
at com.ibm.db2.jcc.am.fd.a(fd.java:120)
at com.ibm.db2.jcc.t4.b.q(b.java:2072)
at com.ibm.db2.jcc.t4.b.c(b.java:1669)
at com.ibm.db2.jcc.t4.ab.r(ab.java:809)
at com.ibm.db2.jcc.t4.ab.k(ab.java:363)
at com.ibm.db2.jcc.t4.ab.c(ab.java:136)
at com.ibm.db2.jcc.t4.b.Vc(b.java:1276)
at com.ibm.db2.jcc.t4.b.b(b.java:1195)
at com.ibm.db2.jcc.t4.b.a(b.java:5511)
at com.ibm.db2.jcc.t4.b.c(b.java:760)
at com.ibm.db2.jcc.t4.b.b(b.java:703)
at com.ibm.db2.jcc.t4.b.a(b.java:389)
at com.ibm.db2.jcc.t4.b.<init>(b.java:318)
at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:214)
at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:460)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at javahelloworld.poc.DB2ConnectTest.test(DB2ConnectTest.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Please help.
Managed to get this to work. A few things that were wrong with my original attempt.
Both db2jcc_license_cisuz-1.0.jar and db2jcc4-V97FP5.jar are required in the class path. Putting them in your local maven repository and adding them in the pom.xml is fine. However, both of them have to be there. This is how my pom.xml looks now. You could add them both in the classpath by any other method.
<dependency>
<groupId>com.ibm.db2</groupId>
<artifactId>db2jcc_license</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.ibm.db2</groupId>
<artifactId>db2jcc4</artifactId>
<version>1.0</version>
</dependency>
This driver works fine.
Class.forName("com.ibm.db2.jcc.DB2Driver");
Connection connection1 = DriverManager
.getConnection(
Finally, something that took embarassingly long amount of time for me to figure out. Since we are talking about connection to DB2 on z/OS it might have some additional restrictions of password (those which might not apply to your regular dev environment of windows / linux), that are easy to forget.
Take care of these things and you should be all set. Thanks.
I am trying to Unit test one of my sub-flows using MUnit, I need to mock out a custom Java component but I am unable to do so.
My sub-flow reads as below
<sub-flow name="ProcessCSVFlow" tracking:enable-default-events="true">
<transformer ref="enrichWithHeaderAndEndOfFileTransformer" doc:name="headerAndEOFEnricher" />
<set-variable variableName="outputfilename" value="#['Mercury'+server.dateTime.year+server.dateTime.month+server.dateTime.dayOfMonth+server.dateTime.hours +server.dateTime.minutes+server.dateTime.seconds+'.csv']" doc:name="outputfilename"/>
<sftp:outbound-endpoint exchange-pattern="one-way" connector-ref="DestinationSFTP" host="${destination.host}" port="22" responseTimeout="10000" doc:name="DestinationSFTP"
outputPattern="#[outputfilename]" path="${destination.path}" user="${destination.username}" password="${destination.password}"/>
<!-- <component class="nz.co.mightyriver.creditreport.component.ArchiveOutputComponent" doc:name="Archive"/> -->
<gzip-compress-transformer/>
<sftp:outbound-endpoint exchange-pattern="one-way" connector-ref="InputSFTP" host="${source.host}" port="22" responseTimeout="10000" doc:name="SourceArchiveSFTP"
outputPattern="#[outputfilename].gzip" path="Archive" user="${source.username}" password="${source.password}"/>
<component doc:name="Delete Read File">
<singleton-object class="nz.co.mightyriver.creditreport.component.DeleteProcessedFileComponent">
<property key="host" value="${source.host}"/>
<property key="username" value="${source.username}"/>
<property key="password" value="${source.password}"/>
<property key="workingDirectory" value="${source.path}"/>
</singleton-object>
</component>
<parse-template location="successmessagetemplate.txt" doc:name="Success Template"/>
<smtp:outbound-endpoint host="${smtp.host}" port="${smtp.port}" user="${smtp.from.address}" password="${smtp.from.password}"
to="${smtp.to.address}" from="${smtp.from.address}" subject="${mail.success.subject}" responseTimeout="10000"
doc:name="SuccessEmail" connector-ref="Gmail"/>
<logger message="Process completed successfully" level="INFO" doc:name="Logger"/>
</sub-flow>
This is my failing unit test
#Test
public void givenAValidPayload_whenFlowIsInvoked_itShouldSendPayloadToDestinationSFTPOnlyOnce() throws Exception {
destinationSFTP.thenReturnSameEvent();
sourceArchiveSFTP.thenReturnSameEvent();
deleteProcessedFileComponent.thenReturnSameEvent();
successEmail.thenReturnSameEvent();
MuleEvent testEvent = PropertyEnricher.enrich(testEvent(IOUtils.toInputStream("hello,dummy,payload"))).get();
runFlow(PROCESS_CSV_FLOW, testEvent);
verifyCallOfMessageProcessor("outbound-endpoint")
.ofNamespace("sftp")
.withAttributes(attribute("name").ofNamespace("doc").withValue("DestinationSFTP"))
.times(1);
}
My attempt at mocking out the component
deleteProcessedFileComponent = whenMessageProcessor("component")
.withAttributes(attribute("name").ofNamespace("doc").withValue("Delete Read File"));
I have tried a few variations and none have worked, I am guessing a component is not a MessageProcessor
The exception I get is as follows
org.mule.api.lifecycle.InitialisationException: Component has not been initialized properly, no flow constuct.
at org.mule.component.AbstractComponent.initialise(AbstractComponent.java:218)
at org.mule.processor.chain.AbstractMessageProcessorChain.initialise(AbstractMessageProcessorChain.java:80)
at org.mule.processor.chain.AbstractMessageProcessorChain$$FastClassByCGLIB$$38c17d88.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
at org.mule.munit.common.mp.WrapperMunitMessageProcessorInterceptor.invokeSuper(WrapperMunitMessageProcessorInterceptor.java:71)
at org.mule.munit.common.mp.WrapperMunitMessageProcessorInterceptor.invokeSuper(WrapperMunitMessageProcessorInterceptor.java:63)
at org.mule.munit.common.mp.WrapperMunitMessageProcessorInterceptor.intercept(WrapperMunitMessageProcessorInterceptor.java:49)
at org.mule.processor.chain.SubflowInterceptingChainLifecycleWrapper$$EnhancerByMUNIT$$c8ca2508.initialise(<generated>)
at org.mule.munit.runner.functional.FunctionalMunitSuite.initialiseSubFlow(FunctionalMunitSuite.java:269)
at org.mule.munit.runner.functional.FunctionalMunitSuite.runFlow(FunctionalMunitSuite.java:259)
at nz.co.mightyriver.ProcessCsvTest.givenAValidPayload_whenFlowIsInvoked_itShouldSendPayloadToDestinationSFTPOnlyOnce(ProcessCsvTest.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
So there is no problem with your mock it actually work perfectly.
The problem is with Mule and a little bit with MUnit.
Short answer, there is no fix for that right now.
There is a workaround though!
Create a new xml (as to keep it separate from your production code)
Add flow (not a sub-flow) to that new xml.
In the flow you just added do a flow-ref to your sub-flow.
From you test, instead of doing runFlow(PROCESS_CSV_FLOW), do runFlow("the_flow_you_created").
The longer answer is:
The sub-flows are not real flows, they don't even share a parent class. Thus they behave a little bit different. MUnit does some stuff to make that fact transparent to the user.
As it turns out we are not making enough, I'll create a jira in the MUnit project to tackle that issue. But hopefully you should be able to keep testing.