Validate data fails with Play2.2.0 and Ebean - mysql

I am developing an app with Play 2.2.0 and Ebean.
I have a problem when creating directly entities by code, not using a form. Let's say this is my entity :
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import models.SuperEntity;
import com.avaje.ebean.validation.Length;
#Entity
#Table(name = "foos")
public class Foo extends SuperEntity {
#Column(unique = true)
#NotNull
#Length(min = 2, max = 2)
public String code;
}
And this is my unit test :
#Test
public void testCreate() {
running(fakeApplication(), new Runnable() {
#Override
public void run() {
Foo foo = new Foo();
foo.code = "foo1";
foo.save();
}
});
}
The problem is that the validation annotations seems to be totally ignored. If I set the code to null, I have the database error not a Play error.
[error] Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'name' cannot be null
[error] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[error] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
[error] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
[error] at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
[error] at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
[error] at com.mysql.jdbc.Util.getInstance(Util.java:386)
[error] at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)
[error] at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4190)
[error] at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122)
[error] at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
[error] at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
[error] at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2818)
[error] at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2157)
[error] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2460)
[error] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2377)
[error] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2361)
[error] at com.jolbox.bonecp.PreparedStatementHandle.executeUpdate(PreparedStatementHandle.java:205)
[error] at com.avaje.ebeaninternal.server.type.DataBind.executeUpdate(DataBind.java:55)
[error] at com.avaje.ebeaninternal.server.persist.dml.InsertHandler.execute(InsertHandler.java:134)
[error] at com.avaje.ebeaninternal.server.persist.dml.DmlBeanPersister.execute(DmlBeanPersister.java:86)
The #Length annotation is ignored as well, I can put the length that I want in the database. The column in the database should be varchar(2) and it is varchar(255).
How can I fix this ? Thanks in advance.
EDIT : I have the same errors with annotations like #Min, #Max on Long or Integer properties...

Ebean does not validate data on save. The validation annotations such as #NotNull and #Column(unique = true) are used during DDL generation to specify table constraints. Regarding the #Length annotation, it is possible that this is not supported by Ebean, though I can't find the documentation to be sure. Documentation is definitely a problem with Ebean. There is a discussion on the Play git repo about the future of Ebean with Play. It includes information about new/active development on Ebean.

javax.validation is not implemented in Ebean. See com.avaje.ebean.validation package, but I'd suggest you not to use it.

Related

In Chisel3, what imports do I need for the Printable examples?

I am trying to follow the example here:
https://github.com/freechipsproject/chisel3/wiki/Printing-in-Chisel#custom-printing
As in the example, I overrode def toPrintable: Printable with a concatenation of p"..." + strings.
In my scala file I
import chisel3._
But I get Scala compile warnings that it doesn't know what Printable is nor does it know what to do with the p interpolator, which makes me think I don't have the right imports.
Is there something I need to import other than chisel3._?
Here is a bit more information on what I am doing and what error I am getting.
I am modifying this file:
https://github.com/chipsalliance/rocket-chip/blob/e6a6c67f30d668e702ddbef93789e9b4f709b237/src/main/scala/tilelink/Bundles.scala
Here is what I have added:
...
import Chisel._
import chisel3.{Printable}
...
final class TLBundleA(params: TLBundleParameters)
extends TLBundleBase(params) with TLAddrChannel
{
...
override def toPrintable: Printable = {
p"A:\t" +
p"opcode[${opcode}]\t" +
p"param[${param}]\t" +
p"size[${size}]\t" +
p"source[${source}]\t" +
p"address[${address}]\t" +
p"user[${user}]\t" +
p"mask[${mask}]\t" +
p"data[${data}]\t" +
p"corrupt[${corrupt}]\n"
}
}
It seems that (unlike import chisel3._), import.chisel3.{Printable} works, but I am not doing something right with the p"...". I get this series of error:
[error] rocket-chip/src/main/scala/tilelink/Bundles.scala:189:5: value p is not a member of StringContext
[error] p"A:\t" +
[error] ^
[error] rocket-chip/src/main/scala/tilelink/Bundles.scala:190:5: value p is not a member of StringContext
[error] p"opcode[${opcode}]\t" +
[error] ^
...
[error] rocket-chip/src/main/scala/tilelink/Bundles.scala:198:5: value p is not a member of StringContext
[error] p"corrupt[${corrupt}]\n"
[error] ^
[error] 10 errors found
[error] (Compile / compileIncremental) Compilation failed
EDIT TO UPDATE:
After looking at the Chisel3 source, I added
import chisel3.{Printable, PrintableHelper}
and now I get a new error which looks more like I am just messing up my string:
[error] (run-main-0) java.lang.IllegalArgumentException: requirement failed
[error] java.lang.IllegalArgumentException: requirement failed
[error] at scala.Predef$.require(Predef.scala:268)
[error] at chisel3.printf$.escaped$1(Printf.scala:28)
[error] at chisel3.printf$.$anonfun$format$3(Printf.scala:32)
[error] ocketchiat chisel3.printf$.$anonfun$format$3$adapted(Printf.scala:32)
[error] at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:238)
[error] at scala.collection.IndexedSeqOptimized.foreach(IndexedSeqOptimized.scala:36)
[error] at scala.collection.IndexedSeqOptimized.foreach$(IndexedSeqOptimized.scala:33)
[error] at scala.collection.immutable.StringOps.foreach(StringOps.scala:33)
[error] at scala.collection.TraversableLike.map(TraversableLike.scala:238)
[error] at scala.collection.TraversableLike.map$(TraversableLike.scala:231)
[error] at scala.collection.immutable.StringOps.map(StringOps.scala:33)
[error] at chisel3.printf$.format(Printf.scala:32)
[error] at chisel3.internal.firrtl.Emitter.emit(Emitter.scala:75)
[error] at chisel3.internal.firrtl.Emitter.$anonfun$moduleDefn$4(Emitter.scala:140)
EDIT TO UPDATE:
I added more verbosity to the requires in the printf code and I think it doesn't like tabs (\t):
[error] (run-main-0) java.lang.IllegalArgumentException: requirement failed: char to Int 9 must be >= 32
[error] java.lang.IllegalArgumentException: requirement failed: char to Int 9 must be >= 32
EDIT TO UPDATE:
I modified chisel3 to be OK with tabs, and now the code compiles and runs, but this is one of the resulting verilog strings which is obviously not right. I tried the string2Printable import you suggested but still get the following:
freechips.rocketchip.system.DefaultConfig.v: $fwrite(32'h80000002,"PLIC_TL_IFC A:\tPrintables(ArrayBuffer(PString(opcode[), Decimal(UInt<3>(IO in unelaborated TLMonitor)), PString(]\t)))Printables(ArrayBuffer(PString(param[), Decimal(UInt<3>(IO in unelaborated TLMonitor)), PString(]\t)))Printables(ArrayBuffer(PString(size[), Decimal(UInt<2>(IO in unelaborated TLMonitor)), PString(]\t)))Printables(ArrayBuffer(PString(source[), Decimal(UInt<9>(IO in unelaborated TLMonitor)), PString(]\t)))Printables(ArrayBuffer(PString(address[), Decimal(UInt<28>(IO in unelaborated TLMonitor)), PString(]\t)))Printables(ArrayBuffer(PString(user[), PString(None), PString(]\t)))Printables(ArrayBuffer(PString(mask[), Decimal(UInt<8>(IO in unelaborated TLMonitor)), PString(]\t)))Printables(ArrayBuffer(PString(data[), Decimal(UInt<64>(IO in unelaborated TLMonitor)), PString(]\t)))Printables(ArrayBuffer(PString(corrupt[), Decimal(Bool(IO in unelaborated TLMonitor)), PString(]\n)))"); // #[Monitor.scala 577:40:freechips.rocketchip.system.DefaultConfig.fir#64082.10]
freechips.rocketchip.system.DefaultConfig.v: $fwrite(32'h80000002,"PLIC_TL_IFC TLBundleD(opcode -> %d, param -> %d, size -> %d, source -> %d, sink -> %d, denied -> %d, data -> %d, corrupt -> %d)",io_in_d_bits_opcode,2'h0,io_in_d_bits_size,io_in_d_bits_source,1'h0,1'h0,io_in_d_bits_data,1'h0); // #[Monitor.scala 578:40:freechips.rocketchip.system.DefaultConfig.fir#64090.10]
For reference, here is the callsite where I am actually calling this:
when (bundle.a.fire()) { printf(p"$prefix ${bundle.a.bits}")}
when (bundle.d.fire()) { printf(p"$prefix ${bundle.d.bits}")}
Note the bundle.d.bits looks "okay" because I haven't given it a specific toPrintable function, but bundle.a.bits is a mess.
EDIT: RESOLVED
The problem is I had changed my toPrintable in the course of debugging to be of the form
override def toPrintable: Printable = {
"A:\t" +
p"opcode[$opcode]\t" +
...
p"corrupt[$corrupt]\n"
}
}
The first un-p string was making the rest of them just be normal strings, ignoring the p. I put the first p"A:\t" back and now it interpolates correctly.
The import chisel3._ should be enough. Try looking at the examples in the Chisel3 repo such as LFSR16.scala. If that doesn't help can you show the error message.

Error: Attempted to instantiate a Module without wrapping it in Module()

Top module is as follows;
class PE (DataWidth: Int, NumLinks: Int, NumEntries: Int, FifoDepth: Int) extends Module {
val io = IO(new Bundle {
...
})
...
}
I think that this is ordinary style for the chisel3.
I do the following run of sbt to lint the code;
sbt 'test:runMain noc.PEMain'
Then I get bellow error messages;
[warn] Multiple main classes detected. Run 'show discoveredMainClasses' to see the list
[info] Running noc.NoCMain
[info] [0.002] Elaborating design...
[error] (run-main-0) chisel3.internal.ChiselException: Error: attempted to instantiate a Module without wrapping it in Module().
[error] chisel3.internal.ChiselException: Error: attempted to instantiate a Module without wrapping it in Module().
[error] at chisel3.internal.throwException$.apply(Error.scala:13)
[error] at chisel3.core.BaseModule.<init>(Module.scala:90)
[error] at chisel3.core.UserModule.<init>(UserModule.scala:18)
[error] at chisel3.core.ImplicitModule.<init>(UserModule.scala:102)
[error] at chisel3.core.LegacyModule.<init>(UserModule.scala:127)
[error] at noc.NumGen.<init>(NoC.scala:328)
[error] at noc.FanIn_Link.<init>(NoC.scala:376)
[error] at noc.PE$$anonfun$12.apply(NoC.scala:490)
[error] at noc.PE$$anonfun$12.apply(NoC.scala:490)
[error] at chisel3.core.Module$.do_apply(Module.scala:49)
[error] at noc.PE.<init>(NoC.scala:490)
[error] at noc.NoCMain$$anonfun$1.apply(NoCMain.scala:27)
[error] at noc.NoCMain$$anonfun$1.apply(NoCMain.scala:27)
...
[error] at chisel3.internal.Builder$$anonfun$build$1.apply(Builder.scala:297)
[error] at chisel3.internal.Builder$$anonfun$build$1.apply(Builder.scala:295)
[error] at scala.util.DynamicVariable.withValue(DynamicVariable.scala:58)
[error] at chisel3.internal.Builder$.build(Builder.scala:295)
[error] at chisel3.Driver$.elaborate(Driver.scala:93)
[error] at chisel3.Driver$.execute(Driver.scala:140)
[error] at chisel3.iotesters.setupTreadleBackend$.apply(TreadleBackend.scala:139)
...
[error] at logger.Logger$$anonfun$makeScope$1.apply(Logger.scala:138)
[error] at scala.util.DynamicVariable.withValue(DynamicVariable.scala:58)
[error] at logger.Logger$.makeScope(Logger.scala:136)
...
[error] at scala.util.DynamicVariable.withValue(DynamicVariable.scala:58)
[error] at chisel3.iotesters.Driver$.execute(Driver.scala:38)
[error] at chisel3.iotesters.Driver$.execute(Driver.scala:100)
[error] at noc.NoCMain$.delayedEndpoint$noc$NoCMain$1(NoCMain.scala:27)
[error] at noc.NoCMain$delayedInit$body.apply(NoCMain.scala:26)
[error] at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
...
[error] at scala.App$class.main(App.scala:76)
[error] at noc.NoCMain$.main(NoCMain.scala:26)
[error] at noc.NoCMain.main(NoCMain.scala)
[error] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
[error] at java.lang.Thread.run(Thread.java:745)
And lastly this error:
[error] (Test / runMain) Nonzero exit code: 1
I find warning of:
[warn] Multiple main classes detected. Run 'show discoveredMainClasses' to see the list
Command of sbt 'show discoveredMainClasses' shows;
[info] Loading settings from plugins.sbt ...
[info] Loading project definition from /Users/hoge/Desktop/NoC/project
[info] Loading settings from build.sbt ...
[info] Set current project to en-noc (in build file:/Users/hoge/Desktop/NoC/)
[info] *
[success] Total time: 1 s, completed 2019/10/22 2:08:49
What does this error message mean and how can I fix it?
sbt 'testOnly noc.PETester'
introduced;
[info] at chisel3.core.LegacyModule.<init>(UserModule.scala:127)
This is caused at
val io = IO(new Bundle {
val No = Output(Vec(NumLinks, UInt((log2Ceil(NumLinks)).W)))
})
It seem that the main problem is:
attempted to instantiate a Module without wrapping it in Module()
This may rise due to you making a new instance of a class extended from Module but you are probably not wrapping it into one.
For example you in your code you are doing something like:
val test = new module_class
where as you should be doing
val test = Module(new module_class)

camel read directory and choose a destination by completion task result

I need to read a directory with csv files (one or more files). I use Camel with springboot and I need to move any file processed completely(without errors) to OUT dir, but if the last "to" route fails (an Exceptions is thrown) I need to move the file to REFUSED dir.
When I try my code, camel goes to infinitive loop, continue to process the same file forever..
24/08/2017 16:27:57.070 ERROR [Camel (camel-1) thread #0 - file://src/main/resources/data] - org.apache.camel.processor.DefaultErrorHandler: Failed delivery for (MessageId: ID-CAD1652-39380-1503584865077-0-33 on ExchangeId: ID-CAD1652-39380-1503584865077-0-34). Exhausted after delivery attempt: 1 caught: com.cadit.exceptions.FileNotEvaluableException: Error: file tipo sconosciuto
Message History
---------------------------------------------------------------------------------------------------------------------------------------
RouteId ProcessorId Processor Elapsed (ms)
[route2 ] [route2 ] [file://src/main/resources/data?idempotent=false&move=OUT%2FVB%2F ] [ 10]
[route2 ] [unmarshal1 ] [unmarshal[org.apache.camel.model.dataformat.CsvDataFormat#28f6cf0f] ] [ 1]
[route2 ] [to1 ] [bean:myCsvHandler?method=doHandleCsvDataVB ] [ 8]
Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------
com.cadit.exceptions.FileNotEvaluableException: Error: file tipo sconosciuto
at com.cadit.handlers.MyCsvHandler.doHandleCsvDataVB(MyCsvHandler.java:172)
at com.cadit.handlers.MyCsvHandler$$FastClassBySpringCGLIB$$f4b8f70b.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:721)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656)
at com.cadit.handlers.MyCsvHandler$$EnhancerBySpringCGLIB$$d81d9e7f.doHandleCsvDataVB(<generated>)
at sun.reflect.GeneratedMethodAccessor97.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:458)
at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:289)
at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:262)
at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:178)
at org.apache.camel.component.bean.BeanProducer.process(BeanProducer.java:41)
at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:145)
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:542)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:120)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197)
at org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:460)
at org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:227)
at org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:191)
at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:175)
at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:102)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
This is the main code:
#Component
public class CamelContextConf extends RouteBuilder{
static final Logger logger = Logger.getLogger(CamelContextConf.class);
#Override
public void configure() throws Exception {
restConfiguration().component("servlet").dataFormatProperty("prettyPrint", "true") ;
CsvDataFormat csv = new CsvDataFormat();
csv.setDelimiter(";");
csv.setSkipHeaderRecord(true);
from("direct:csvprocessor")
.streamCaching()
.from("file:src/main/resources/data?move=OUT/VB/")
.unmarshal(csv)
.to("bean:myCsvHandler?method=doHandleCsvDataVB")
.onCompletion().onFailureOnly().to("file:src/main/reources/data/REFUSED").end()
.setBody(constant("OK"))
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(200))
.setHeader(Exchange.CONTENT_TYPE, constant("text/html"));
logger.info("** Route config ok");
}
then in myCsvHandler -> method doHandleCsvDataVB I explicitly throw an exception for testing
a failure :
#Component
public class MyCsvHandler {
#Inject
AFVINCCrudRepository _entityManagerVINC;
#Inject
AFFileCrudRepository _entityManagerAfFile;
#Transactional(propagation = Propagation.REQUIRED , transactionManager="DbTransactionManagerVB")
public void doHandleCsvDataVB(List<List<String>> csvData) throws FileNotEvaluableException
{
//System.out.println("stampo..");
if (csvData.size() > 0){
AfFileEntity afbean = new AfFileEntity();
afbean.setNomeFile("test");
afbean.setDataImport(new java.sql.Timestamp(System.currentTimeMillis()));
afbean.setTipoFile("M");
afbean.setAfStatoFlusso("I");
_entityManagerAfFile.save(afbean);
long pkfile = afbean.getId();
System.out.println("pkfile: " + pkfile);
int i = 1; VincEntity vincBean = new VincEntity();
System.out.println(csvData.size());
for (List<String> rows : csvData){
..
_entityManagerVINC.save(..);
}
throw new FileNotEvaluableException("Il file non è nè una ...");
}
}
}
}
The save methods loop and continue to save data on db..
What's wrong?
Thanks so much.
In your code, you cannot have from inside another from.
from("direct:csvprocessor")
.streamCaching()
.from("file:src/main/resources/data?move=OUT/VB/")
Your second from("file:...") should instead use an enricher. http://camel.apache.org/content-enricher.html
Not sure, but in your doHandleCsvDataVB() method I don't see a try() catch() block. You are going straight from a for loop to throwing the exception.
I did the below way to move the file from one folder to another folder.
#Autowired
FileProcessor fileProcessor;
#Component
public class FileRouteBuilder extends RouteBuilder {
#Override
public void configure() throws Exception {
from("file://{{dir.in}}?readLock=changed&include=.*.csv"&preMove={{dir.progress}}&move={{dir.out}}&moveFailed={{dir.error}}")
.process(fileProcessor).setId("Poller");
}
}
dir.in -Source directory for file polling
dir.progress -File will be moved from in to progress for processing
dir.out -File will be moved here when processed successfully
dir.error - File moved here when any error occur during process
readLock=changed - this will lock the file while reading
include - this will read only csv files in source dir i.e in folder
you may or may not have processor as per your needs.
if you don't want processor then use below code
from("file://{{dir.in}}?readLock=changed&include=.*.csv"&preMove={{dir.progress}}&move={{dir.out}}&moveFailed={{dir.error}}")
.setId("Poller");
Please note that, if you are not using preMove then default .camel folder will be created in the 'in' folder and one copy of file moved to .camel folder. file will always exist in the directory until and unless we remove manually.
So i suggest you to use the preMove in the route.

Using Spring Boot Data to read MySQL data

I'm trying to create a framework to read data from MySQL using Spring Data. The end goal is to be able to write automated tests that could read data from two different MySQL dbs and compare the data (for example, to make sure data is replicating correctly). I'm currently having loads of trouble in actually getting the Spring code to work (I've never used Spring before, I've tried modifying some various tutorial code I've found on the web, but so far haven't gotten it working.)
Here's what I've got.
MySQL
table: credentials
columns: id (int), password_hash (string)
Has 4 entries in it.
Project layout:
src/main
groovy
domain
Credentials
repository
CredentialsRepository
resources
application.properties
src/test/groovy/
CredentialsTest
build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.3.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'gs-accessing-data-jpa'
version = '0.1.0'
}
repositories {
mavenCentral()
maven { url "https://repository.jboss.org/nexus/content/repositories/releases" }
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.7'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '1.4.3.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.4.3.RELEASE'
testCompile 'junit:junit:4.11'
}
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/my_db
spring.datasource.username=my_user
spring.datasource.password=my_password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
Credentials.groovy
package domain
import org.springframework.data.annotation.Id
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Table
#Entity
#Table(name = 'credentials')
class Credentials {
#Id #GeneratedValue(strategy = GenerationType.AUTO) #Column(name='id')
int id
#Column(name='password_hash')
String passwordHash
protected Credentials() {}
#Override
String toString() {
"Credential: [id=${id}, passwordHash=${passwordHash}]"
}
}
CredentialsRepository.groovy
package repository
import domain.Credentials
import org.springframework.data.repository.CrudRepository
interface CredentialsRepository extends CrudRepository<Credentials, Integer> {
}
CredentialsTest.groovy
import domain.Credentials
import repository.CredentialsRepository
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import static org.junit.Assert.assertEquals
#RunWith(SpringJUnit4ClassRunner.class) //#EnableJpaRepositories(['domain.Credentials'])
#SpringBootTest(classes = MysqlJpaDemoApplication.class)
class CredentialsTest {
#Autowired
CredentialsRepository credentialsRepository
#Test
void testLoadCredentials() {
List<Credentials> credentialsList = credentialsRepository.findAll() as ArrayList<Credentials>
assertEquals(20, credentialsList.size())
}
}
Running the testLoadCredentials test gives the following stacktrace:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CredentialsTest': Unsatisfied dependency expressed through field 'credentialsRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'repository.CredentialsRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1225)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:386)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
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:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'repository.CredentialsRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1474)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1102)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1064)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
... 33 more
2016-12-28 14:14:32.638 INFO 39748 --- [ Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext#742ff096: startup date [Wed Dec 28 14:14:32 PST 2016]; root of context hierarchy
Process finished with exit code 255
Seems that the application class MysqlJpaDemoApplication that you're using to configure your tests is in the default (top-level) package. As Spring runs some checks when using #ComponentScan, it will prevent your application to scan the whole classpath.
You should move MysqlJpaDemoApplication to a new package, ie,com.example. Your source folder would look like this:
src/main
/groovy/com/example
|-- /domain
| |-- Credentials.groovy
|-- /repository
| |-- CredentialsRepository.groovy
|-- MysqlJpaDemoApplicatin.groovy
Looks like this is what got it to work:
com.example/
domain/
Credentials
repository/
CredentialsRepository
SpringConfig
Credentials.groovy
#Entity
#Table(name = 'credentials')
class Credentials {
#Id #GeneratedValue(strategy = GenerationType.AUTO)
int id
#Column(name='password_hash')
String passwordHash
protected Credentials() {}
#Override
String toString() {
"Credential: [id=${id}, passwordHash=${passwordHash}]"
}
}
CredentialsRepository
interface CredentialsRepository extends CrudRepository<Credentials, Integer> {}
SpringConfig.groovy
#Configuration
#EnableAutoConfiguration
#ComponentScan('com.example')
class SpringConfig {}
CredentialsTest.groovy
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes=SpringConfig.class)
#SpringBootTest(classes = SpringConfig.class)
class CredentialsTest {
#Autowired
CredentialsRepository credentialsRepository
#Test
void testLoadCredentials() {
List<Credentials> credentialsList = credentialsRepository.findAll() as ArrayList<Credentials>
assertEquals(4, credentialsList.size())
}
}

PlayFramework 2.5 - Testing not using in-memory databse

I have been following tutorials on PlayFramework, but it seems that they are all outdated and for older version of Play.
I want to try JUnit test using mysql databse not h2 in-memory database.
I am using ebean ORC (which clearly has different api than it used to have inside play 2.2, and the api is not really well documented).
Anyway I want to try JUnit test on mysql databse, but I am always getting a configuration error.
This is how the JUnit test class looks:
public class ModelsTest extends WithApplication {
public Application app;
#Before
public void setUp() throws FileNotFoundException, IOException {
java.util.Properties externalProps=new java.util.Properties();
externalProps.load(new FileInputStream("resources/test-ebean.properties"));
ServerConfig config = new ServerConfig();
config.setName("test");
config.setDefaultServer(true);
config.loadFromProperties(externalProps);
EbeanServer server = EbeanServerFactory.create(config);
app = Helpers.fakeApplication();
Helpers.start(app);
}
#Test
public void createAndRetrieveUser() {
new User("bob#bob.bob", "admin", "admin").save();
User bob = User.find.where().eq("email", "bob#bob.bob").findUnique();
assertNotNull(bob);
assertEquals("admin", bob.login);
}
#After
public void stopApp() {
Helpers.stop(app);
}
test-ebean.properties file :
ebean.ddl.generate=true
ebean.ddl.run=true
datasource.default=db
datasource.db.username="root"
datasource.db.password="root"
datasource.db.databaseUrl="jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8"
datasource.db.databaseDriver=com.mysql.jdbc.Driver
When running test I am getting this error:
[error] Test ModelsTest.createAndRetrieveUser failed:` play.api.Configuration$$anon$1: Configuration error[null], took 4.969 sec
[error] at play.api.Configuration$.configError(Configuration.scala:154)
[error] at play.api.Configuration.reportError(Configuration.scala:806)
[error] at play.Configuration.reportError(Configuration.java:366)
[error] at play.db.ebean.DefaultEbeanConfig$EbeanConfigParser.parse(DefaultEbeanConfig.java:81)
[error] at play.db.ebean.DefaultEbeanConfig$EbeanConfigParser.get(DefaultEbeanConfig.java:60)
[error] at play.db.ebean.DefaultEbeanConfig$EbeanConfigParser.get(DefaultEbeanConfig.java:44)
[error] at com.google.inject.internal.ProviderInternalFactory.provision(ProviderInternalFactory.java:81)
[error] at com.google.inject.internal.BoundProviderFactory.provision(BoundProviderFactory.java:72)
[error] at com.google.inject.internal.ProviderInternalFactory.circularGet(ProviderInternalFactory.java:61)
[error] at com.google.inject.internal.BoundProviderFactory.get(BoundProviderFactory.java:62)
[error] at com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:46)
[error] at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1103)
[error] at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
[error] at com.google.inject.internal.SingletonScope$1.get(SingletonScope.java:145)
[error] at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:41)
[error] at com.google.inject.internal.SingleParameterInjector.inject(SingleParameterInjector.java:38)
[error] at com.google.inject.internal.SingleParameterInjector.getAll(SingleParameterInjector.java:62)
[error] at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:104)
[error] at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:85)
[error] at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:267)
[error] at com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:46)
[error] at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1103)
[error] at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
[error] at com.google.inject.internal.SingletonScope$1.get(SingletonScope.java:145)
[error] at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:41)
[error] at com.google.inject.internal.FactoryProxy.get(FactoryProxy.java:56)
[error] at com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:46)
[error] at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1103)
[error] at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
[error] at com.google.inject.internal.SingletonScope$1.get(SingletonScope.java:145)
[error] at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:41)
[error] at com.google.inject.internal.InternalInjectorCreator$1.call(InternalInjectorCreator.java:205)
[error] at com.google.inject.internal.InternalInjectorCreator$1.call(InternalInjectorCreator.java:199)
[error] at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1092)
[error] at com.google.inject.internal.InternalInjectorCreator.loadEagerSingletons(InternalInjectorCreator.java:199)
[error] at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:180)
[error] at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:110)
[error] at com.google.inject.Guice.createInjector(Guice.java:96)
[error] at com.google.inject.Guice.createInjector(Guice.java:84)
[error] at play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:181)
[error] at play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:123)
[error] at play.api.test.FakeApplication.<init>(Fakes.scala:209)
[error] at play.test.FakeApplication.<init>(FakeApplication.java:51)
[error] at play.test.Helpers.fakeApplication(Helpers.java:124)
[error] at play.test.WithApplication.provideFakeApplication(WithApplication.java:46)
[error] at play.test.WithApplication.provideApplication(WithApplication.java:33)
[error] at play.test.WithApplication.startPlay(WithApplication.java:51)
[error] ...
[error] Caused by: java.lang.NullPointerException
[error] at play.db.ebean.DefaultEbeanConfig$EbeanConfigParser.parse(DefaultEbeanConfig.java:79)
[error] ... 78 more
[error] Test ModelsTest.createAndRetrieveUser failed: java.lang.NullPointerException: null, took 4.979 sec
[error] at play.test.Helpers.stop(Helpers.java:376)
[error] at ModelsTest.stopApp(ModelsTest.java:58)
[error] ...
[error] Failed: Total 1, Failed 1, Errors 0, Passed 0
[error] Failed tests:
[error] ModelsTest
[error] (test:testOnly) sbt.TestsFailedException: Tests unsuccessful
I just started learning play (but actually most of tutorials are outdated) and I have spent more time trying to configure it to run than actually coding. I guess I should look up another framework.
You don't need to set up a whole database just for testing (you are free to, of course). Play relies strongly on In-Memory databases (e.g. during development) and you can utilize this also in your tests:
#Test
public void findById() {
running(fakeApplication(inMemoryDatabase("test")), () -> {
User bob = User.findById(21l);
assertEquals("bob#bob.bob", bob.email);
assertEquals("admin", bob.login);
});
}
On the other hand if you really want to test the database access code you can go as far as creating a Database test object:
Database database = Databases.createFrom(
"com.mysql.jdbc.Driver",
"jdbc:mysql://localhost/test"
);
Which again can be in-memory:
Database database = Databases.inMemory(
"mydatabase",
ImmutableMap.of(
"MODE", "MYSQL"
),
ImmutableMap.of(
"logStatements", true
)
);
Just don't forget to release the resources after the test:
#After
public void shutdownDatabase() {
database.shutdown();
}