Mockito: To test that a returned String is under a given length - spring-boot

In Junit I want to check that length of a returned String is under a given length .
I am doing the following and it is passing . But I want to know that is there any better alternate solution it test it.
Assertions.assertTrue(roomEntity.getRoomType().length()<=10);
Thanks

Your attempt is good. You could use assertThat(), that may be a bit more readable.
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.Assert.assertThat;
...
assertThat(roomEntity.getRoomType().length(), lessThanOrEqualTo(10));
Or refactor the length into its own variable.
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.Assert.assertThat;
...
int roomTypeLength = roomEntity.getRoomType().length();
assertThat(roomTypeLength, lessThanOrEqualTo(10));

Here is the way I am following for this.
You can use one of these libraries. Both works the same.
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.16.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.easytesting/fest-assert -->
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert</artifactId>
<version>1.4</version>
<scope>test</scope>
</dependency>
Now to the code segment.
Assertions.assertThat(roomEntity.getRoomType().length()).isLessThanOrEqualTo(10);
There are some other useful ways are also included in this such as isLessThan, isBetween, hasSize etc.

Related

TableInputFormat is not a member of package org.apache.hadoop.hbase.mapreduce

I import the TableInputFormat in my code as:
import org.apache.hadoop.hbase.mapreduce.TableInputFormat
but it shows errors:
object TableInputFormat is not a member of package org.apache.hadoop.hbase.mapreduce
but package org.apache.hadoop.hbase.mapreduce do has the class TableInputFormat (http://hbase.apache.org/apidocs/org/apache/hadoop/hbase/mapreduce/TableInputFormat.html)
And I have added the libraryDependencies including :
"org.apache.spark" % "spark-core_2.11" % "2.4.0""org.apache.hbase" % "hbase-server" % "2.1.1""org.apache.hbase" % "hbase-common" % "2.1.1""org.apache.hbase" % "hbase-hadoop-compat" % "2.1.1""org.apache.hadoop" % "hadoop-common" % "2.8.5"
TableInputFormat is in the org.apache.hadoop.hbase.mapreduce
package, which is part of the hbase-server artifact, so it needs to add that as a dependency. But I have added that dependency, why will it run wrong?
I also encounter the same problem, but after I add "hbase-mapreduce" to the pom.xml and it works well. Here my pom.xml is:
<!-- start of HBase-->
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase</artifactId>
<version>${hbase.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>${hbase.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>${hbase.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>${hbase.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-mapreduce -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-mapreduce</artifactId>
<version>${hbase.version}</version>
</dependency>
<!-- end of hbase -->

How to fix warning in Kotlin: Assigning single elements to varargs in named form is deprecated

After upgrading Kotlin from 1.1.4 to latest 1.2.22, I got a lot of build warnings:
[WARNING] ... (422, 29) Assigning single elements to varargs in named form is deprecated
The related code is like this:
422 #RequestMapping(value = "/privacy", method = arrayOf(RequestMethod.GET))
423 fun mainLinkPrivacy(request: HttpServletRequest, model: MutableMap<String, Any>): String {
424
425 var lang = request.getParameter("lang")
426
427 if(lang == null || lang.isEmpty())
428 lang = "EN"
429
430 model.put("lang",lang)
431
432 return "/mobile/main/mainlink_privacy"
433 }
And here is the import part:
import org.apache.ibatis.annotations.Param
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.core.env.Environment
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam
import javax.servlet.http.HttpServletRequest
And here is my Kotlin version:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<kotlin.version>1.2.21</kotlin.version>
<dokka.version>0.9.15</dokka.version>
<spring.framework.version>4.3.1.RELEASE</spring.framework.version>
</properties>
I think the problem exist in method = arrayOf(), but how to fix this?
I read the Kotlin release note about this Deprecation: single named argument for vararg, it says:
"For consistency with array literals in annotations, passing a single item for a vararg parameter in the named form (foo(items = i)) has been deprecated. Please use the spread operator with the corresponding array factory functions:
foo(items = *intArrayOf(1))
There is an optimization that removes redundant arrays creation in such cases, which prevents performance degradation. The single-argument form produces warnings in Kotlin 1.2 and is to be dropped in Kotlin 1.3."
But I still don't find hints about how to fix it.
I think that not deprecated form is value = ["/privacy"], method = [RequestMethod.GET].
More documentation on how to use annotations in kotlin can be found here

XML Path expression

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>telnet</groupId>
<artifactId>telnet.service</artifactId>
<version>1.10.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>xmlthing</groupId>
<artifactId>xmlthing.service</artifactId>
<version>1.9.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>parser</groupId>
<artifactId>parser.service</artifactId>
<version>1.4.0-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
How to be more substantive than this?
I have problem extracting value from the version attribute where groupId=telnet. How can I get xmlpath value of 1.10.0-SNAPSHOT where groupId=telnet?
Sorry not for mention it before:
It should be in linux/unix format (xmllint, grep, sed ...) anything :)
Thanks a lot!
Brgds,
S.
You can start with selecting all version elements nested in dependency elements:
//pom:dependency/pom:version
and then qualify the dependency appropriately:
//pom:dependency[pom:groupId = 'telnet']/pom:version
Of course, you need to specify the namespaces for XPath as well.
Quick PowerShell test:
PS> $x | Select-Xml '//pom:dependency[pom:groupId = ''telnet'']/pom:version' -Namespace #{pom = 'http://maven.apache.org/POM/4.0.0'} | % node
#text
-----
1.10.0-SNAPSHOT
I find the solution using xpath:
xpath -q -e "//dependency[groupId='telnet']/version/text()" pom.xml

Can I use lambda expressions in Jaspersoft Studio 6.2.0?

I use Jaspersoft Studio 6.2.0, and compile the report in a maven project with dependencies:
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.2.0</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-fonts</artifactId>
<version>6.0.0</version>
</dependency>
The report is fed with a
JRBeanArrayDataSource beanDS = new JRBeanArrayDataSource(new Incident[]{incident}, false);
I have tried to use a labmda expression in Print When Expression of a band. It seems not to recognise it. The expression is:
$F{actionList} == null || $F{actionList}.stream().allMatch(a -> ActionStatus.COMPLETED.equals(a.getStatus()))
ActionStatus is an Enum. And I get syntax errors like:
net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
1. a cannot be resolved to a variable
(((java.util.List)field_actionList.getValue()) == null || ((java.util.List)field_actionList.getValue()).stream().allMatch(a -> ActionStatus.COMPLETED.equals(a.getStatus()))) //$JR_EXPR_ID=248$
^
2. Syntax error on token "-", -- expected
(((java.util.List)field_actionList.getValue()) == null || ((java.util.List)field_actionList.getValue()).stream().allMatch(a -> ActionStatus.COMPLETED.equals(a.getStatus()))) //$JR_EXPR_ID=248$
I also tried using the method from the object directly like:
this.hasAllActionsCompleted()
that does the same thing (this is in the Incident object that is set for the report's DS) :
/**
* Checks if the incident has all actions completed
* #return true if all actions are completed or none defined, false otherwise
*/
public boolean hasAllActionsCompleted() {
return actionList == null || actionList.stream().allMatch(a -> ActionStatus.COMPLETED.equals(a.getStatus()));
}
and I get:
net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
1. The method hasAllActionsCompleted() is undefined for the type Incident_1461053626798_784639
this.hasAllActionsCompleted() //$JR_EXPR_ID=248$
Is there another way to do this? Or should I just set the lambda expression result in a parameter for the report (this is an obvious walkaround, but I was hoping for a direct approach)?
And the answer is change the method to a getter, and use it as a field.
In my case I changed hasAllActionsCompleted to isAllActionsCompleted and added a new field allActionsCompleted as a Boolean. Worked as a charm. Thank you.

Maven/Gradle way to calculate the total size of a dependency with all its transitive dependencies included

I would like to be able to perform an analysis on each of my project POMs to determine how many bytes each direct dependency introduces to the resulting package based on the sum of all of its transitive dependencies.
For example, if dependency A brings in B, C, and D, I would like to be able to see a summary showing A -> total size = (A + B + C + D).
Is there an existing Maven or Gradle way to determine this information?
Here's a task for your build.gradle:
task depsize {
doLast {
final formatStr = "%,10.2f"
final conf = configurations.default
final size = conf.collect { it.length() / (1024 * 1024) }.sum()
final out = new StringBuffer()
out << 'Total dependencies size:'.padRight(45)
out << "${String.format(formatStr, size)} Mb\n\n"
conf.sort { -it.length() }
.each {
out << "${it.name}".padRight(45)
out << "${String.format(formatStr, (it.length() / 1024))} kb\n"
}
println(out)
}
}
The task prints out sum of all dependencies and prints them out with size in kb, sorted by size desc.
Update: latest version of task can be found on github gist
I keep the a small pom.xml template on my workstation to identify heavy-weight dependencies.
Assuming you want to see the weight of org.eclipse.jetty:jetty-client with all of its transitives create this in a new folder.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>not-used</groupId>
<artifactId>fat</artifactId>
<version>standalone</version>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Then cd to the folder and run mvn package and check the size of the generated fat jar. On Unix-like systems you can use du -h target/fat-standalone.jar for that.
In order to test another maven artifact just change groupId:artifactId in the above template.
I do not know any way to show the totals but you may get a report for your project which can show per dependency size information. Please check this maven plugin : http://maven.apache.org/plugins/maven-project-info-reports-plugin/dependencies-mojo.html
If you have a configuration which includes all the necessary dependencies that you wish to calculate the size for you can simply put the following snippet in your build.gradle file:
def size = 0
configurations.myConfiguration.files.each { file ->
size += file.size()
}
println "Dependencies size: $size bytes"
This should print out when you run any gradle task after the build file is compiled.

Resources