Yichun Zhao JAVA Notes

My Java Online Notes

Tuesday, 30 May 2017

Spring Boot Test


An integration test, @SpringBootTest, load the whole of Spring application context. According to observations from the output. It initiates a database connection and a real web server listening on the ports (I see a Tomcat server starts on the port 8080). However,  via @SpringBootTest attributes, the web environment can be re-configured. By default, the web environment should be mock. On such a setup, a mocked servlet container is initiated, rather than a real application server. 

Two major annotations construct a typical Spring Boot (I am using 1.5 release right now) integration test. It looks like this:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class MyTest {

    // ...
    
}
  • @RunWith(SpringRunner.class) tells JUnit to run using Spring’s testing support. SpringRunner is the new name for,SpringJUnit4ClassRunner it’s just a bit easier on the eye. 
  • @SpringBootTest is saying “bootstrap with Spring Boot’s support” (e.g. load application.properties and give me all the Spring Boot goodness)
  • The attributewebEnvironment allows specific “web environments” to be configured for the test. You can start tests with a MOCK servlet environment or with a real HTTP server running on either a RANDOM_PORT or a.DEFINED_PORT  
  • If we want to load a specific configuration, we can use the attributeclasses of @SpringBootTest. In this example, we’ve omitted meansclasses that the test will first attempt to load @Configuration from any inner-classes, and if that fails, it will search for your primary @SpringBootApplication class.
There is another way to test without a server on but having the whole of Spring context(I can see all controller and its method having been mapped.), i.e. using @AutoConfigureMockMvc together with @SpringBootTest to inject a MockMvc instance. Spring uses this MockMVC to send HTTP requests into the DispatcherServlet, instead of a Test Rest Template),  and then hand it off to controllers. It explains there is no need to have a real server on. 

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ApplicationTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void shouldReturnDefaultMessage() throws Exception {
        this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
                .andExpect(content().string(containsString("Hello World")));
    }
}
We may also test without turning sever on, and load partially Spring context(for a single controller). By this way, we may narrow down the test to a web layer alone,  by using @WebMvcTest.

@RunWith(SpringRunner.class)
@WebMvcTest
public class WebLayerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void shouldReturnDefaultMessage() throws Exception {
        this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
                .andExpect(content().string(containsString("Hello World")));
    }
}
  • When you start testing real systems, you often find it’s helpful to mock out specific beans. Common scenarios for mocking include simulating services that you can’t use when running tests, or testing failure scenarios that are difficult to trigger in a live system.
With Spring Boot 1.4 you can easily create a Mockito mocks that can replace an existing bean, or create a new one:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class SampleTestApplicationWebIntegrationTests {

    @Autowired
    private TestRestTemplate restTemplate;

    @MockBean
    private VehicleDetailsService vehicleDetailsService;

    @Before
    public void setup() {
        given(this.vehicleDetailsService.
            getVehicleDetails("123")
        ).willReturn(
            new VehicleDetails("Honda", "Civic"));
    }

    @Test
    public void test() {
        this.restTemplate.getForEntity("/{username}/vehicle", 
            String.class, "sframework");
    }

}
In this example we’re:
  • Creating a Mockito mock for VehicleDetailsService.
  • Injecting it into the ApplicationContext as a bean using @MockBean
  • Injecting it into the field in the test.
  • Stubbing behavior in the setup method.
  • Trigger something that will ultimately call the mock.
Mocks will be automatically reset across tests. They also form part of the cache key used by Spring Test (so there’s no need to add @DirtiesContext)

Spies work in a similar way. Simply annotate a test field with @SpyBean to have a spy wrap any existing bean in the ApplicationContext.

What is Spring Application Context?


The ApplicationContext provides:

  • Bean factory methods for accessing application components.
  • The ability to load file resources in a generic fashion.
  • The ability to publish events to registered listeners.
  • The ability to resolve messages to support internationalization.
  • Inheritance from a parent context.



References: 
Spring Test Document

Building REST services with Spring

For understanding test in Spring boot, the following link is a good one.
Testing improvements in Spring Boot 1.4  






at May 30, 2017 No comments:
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels: SpringBoot

Saturday, 20 May 2017

Calculate distance when knowing longitude and latitude in java

Maven Dependency 

<dependency>
  <groupId>com.javadocmd</groupId>
  <artifactId>simplelatlng</artifactId>
  <version>RELEASE</version>
</dependency>

Getting Started
SimpleLatLng is written and compiled to Java 6. After you have included the library jar in your project or added it as a dependency in your build tool of choice, you can create a point in latitude and longitude as follows:

LatLng point = new LatLng(33.123123, -127.123123);
The important thing to note is that all latitudes and longitudes are handled in degrees using the convention that positive latitudes are in the northern hemisphere and positive longitudes are east of the Prime Meridian.

This library generally considers degree measurements to the millionth (0.000001) degree. So 33.0000010 is equal to 33.0000015, but not equal to 33.0000020. This yields a resolution of around 1 centimeter which is far more accurate than the distance calculations require.

When a LatLng point is constructed, its latitude and longitude will be normalized to fall within the ±90 degrees latitude, ±180 degree longitude range. Special case: if your LatLng is constructed at a pole (±90 degrees latitude), its longitude will be set to 0 because all longitudes intersect at the poles.
Distance between two points

To calculate the distance between two points, use the static methods in LatLngTool.
LatLng point1 = new LatLng(33.123123, -127.123123);
LatLng point2 = new LatLng(33.321321, -127.321321);
double distanceInMiles = LatLngTool.distance(point1, point2, LengthUnit.MILE);
All supported units of length are contained by the enum LengthUnit. We will add supported units if there is call for them.

Reference 

SimpleLatLng is a Java implementation of common latitude and longitude calculations.

Algorithm to find all Latitude Longitude locations within a certain distance from a given Lat Lng location

Spring Data Elasticsearch

index Latitude and Longitude and fast SQL query

at May 20, 2017 No comments:
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

Monday, 15 May 2017

Cannot determine embedded database driver class for database type NONE

Action:

If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).

Meeting the error msg. above,  when setting up Postgres database in the Spring boot.

This error is mostly due to the missing Postgres driver dependencies in the pom.


at May 15, 2017 No comments:
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

Thursday, 11 May 2017

Project Lombok


The project aims to reduce java boilerplate code. It may auto-generate getter, setters, to-string, hashcode, equals, and constructors etc.  To import it, add the following element as a child of dependecies in the pom.xml file:


<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.16</version>
</dependency>

Important: scope should be provided because we do not want Lombok jar file to be included in our build file.

Reference

Tutorial: using Lombok to reduce boilerplate code in Java
at May 11, 2017 No comments:
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels: LomBok

Wednesday, 10 May 2017

How to connect Postgres Database outside Heroku?

I tried to connect to the heroku Postgres database locally in the Netbeans, but I failed.

Open heroku "database credentials", copy the host, username, password etc into Netbeans as building a new database connection.

It needs to add the following at the end of JDBC URL, otherwise connection will fail.
?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory

?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory


at May 10, 2017 No comments:
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels: PaaS, Spring, SpringBoot

Using Profile Specific Property to Config. Dev. and Test Env.


Instead of loading different application property files, Spring Boot provides a simple way to setup different application properties according to build purposes.

Ref. to
profile specific properties 

Key points.
1) using the naming convention, i.e. application-{profile}.properties to define build specific property files.  for instance,  application-tst.properties

2) the profile specific property file can be triggered by adding the property spring.profile.active = tst  in application.properties file.

Please Note:
the property defined in the profile specific property file overrides the one defined in the application.properties. It doesn't mean the profile specific property file overrides the whole property definition defined in application.properties file.

I need to test the following way:
Spring Profile
Using Spring profile to make classes decorated by @Component or @Configuration to be alive.
Set @Profile("production") on @Component or @Configuration
spring.profiles.active=production

at May 10, 2017 No comments:
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels: SpringBoot

Sunday, 7 May 2017

How to Deploy Spring Boot to Heroku

Heroku is a popular PaaS platform.

For a window OS, installing Heroku CLI first; download it from Heroku homepage.
Starting Windows cmd, type in
$heroku login; 
input login email and pwd;
Switching to your working directory.

Create a new Git repository
Initialize a git repository in a new or existing directory

$ cd my-project/
$ git init
$ heroku git:remote -a yardnbud

Deploy the App
(1) create an app on Heroku, which prepares Heroku to receive your source code. 
$heroku create   
or create a named app. 
$heroku apps:create myapp

(2) deploy your code in the heroku
$git push heroku master

ensure that at least one instance of the app is running
$heroku ps:scale web=1
meaning that, right now your app is running on a single web dyno. 

a handy shortcut to open the website as follows: 
$heroku open

View Logs 
$heroku logs --tail  
stop streaming the logs  by control +c

Define a Procfile
Creating a Procfile, a text file without any extension, in the root directory of your application, to tell heroku how to start your app. 

 You must configure your application to listen on the correct port.
web: java -Dserver.port=$PORT -jar target/demo-0.0.1-SNAPSHOT.jar

Spring Boot makes -D arguments available as properties accessible from a Spring Environment instance. The server.port configuration property is fed to the embedded Tomcat, Jetty or Undertow instance which then uses it when it starts up. The $Port environment variables is assigned to us by the Heroku Paas.  

web is important here. it declares that this process type will be attached to the HTTP routing stack of Heroku and receive web traffic when deployed

Declare app dependencies
Heroku recognizes an app as Java by the existence of a pom file in the root directory. It specifies the dependencies that need to be installed with you application. When an app is deployed, Heroku reads this pom file and install the dependencies.

Heroku by default use Java 8; so Maven or Gradle build need to use the same version(Maven can use the java.version property). If requiring JDK 7, creating a new file adjacent to pom.xml and Procfile, called system.properties, where adding the following:

java.runtime.version=1.7

The most common workflow for Heroku deployments is to git push the code to the production. 
$git push heroku master

Scale the app
A dyno is considered as a lightweight container that runs the command specified in the Procfile
check how many dynos running using the command
$heroku ps

scale number of dyno up
$heroku ps:scale web=1 

Run app locally
$heroku local web 

heroku local examines the Procfile to determine what command to run

Using Database

The HikariCP(ref. to A) database connection pool(ref. to B) is initialized with the configuration application.properties
spring.datasource.url: ${JDBC_DATABASE_URL:}
This sets spring.datasource.url to the value in the JDBC_DATABASE_URL environment variable, set by the database add-on, and establishes a pool of connections to the database.
Listing database provisioned for your app using the following command
$heroku addons 

Listing config vars for your app that will display the URL to the database. 
$heroku config 

Listing a lot more
$heroku pg


Deploy local changes

$git add .
$git commit -m "demo"
$git push heroku master
$heroku open 




A: What is HikariCP?
HikariCP is a “zero-overhead” production-quality connection pool.

B: What is Connection Pool?
Establishing JDBC connections is resource-expensive, especially when the JDBC API is used in a middle-tier server environment, such as when DataDirect Connect for JDBC or DataDirect SequeLink for JDBC is running on a Java-enabled web server. In this type of environment, performance can be improved significantly when connection pooling is used. Connection pooling means that connections are reused rather than created each time a connection is requested. To facilitate connection reuse, a memory cache of database connections, called a connection pool, is maintained by a connection pooling module as a layer on top of any standard JDBC driver product.

C: Using SPRING_DATASOURCE_URL in Spring Boot APP
Heroku buildpacks for Java and Gradle will attempt to create SPRING_DATASOURCE_URL, SPRING_DATASOURCE_USERNAME, and SPRING_DATASOURCE_PASSWORD environment variables when a dyno starts up. The values of the variables will be identical to the values in the corresponding JDBC variables.
As long as your application has the proper JDBC driver defined as a dependency, these environment variables should allow your Spring Boot application to connect to the database without any other configuration.
If you need to override the predefined SPRING_DATASOURCE_* environment variables you can set them yourself with the heroku config:set command or in the dashboard. Alternatively, you can add the -Dspring.datasource.url property to your Procfile, which will take precedence over the OS-level environment variables.


D: Where define environment variables in Spring Boot APP?
environment variables(key = value) are defined in the .env text file located at the root directory. It may be configured from CLI, for instance as the following;
$heroku config:set ENERGY = "20 Gev."
In runtime, environment variables are exposed to application and then can be grabbed like the following;
String energy = System.getEnv().get("ENERGY"); 


at May 07, 2017 No comments:
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels: Heroku
Newer Posts Older Posts Home
Subscribe to: Posts (Atom)

Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...

  • Could not extract response: no suitable HttpMessageConverter found for response type [class dk.enettet.evu.core.model.Address] and content type [application/octet-stream]
    Could not extract response: no suitable HttpMessageConverter found for response type [class dk.enettet.evu.core.model.Address] and content ...
  • More than one row with the given identifier was found: 8
    First time met this hibernate exception. I think this issue should due to one to one relationship. One driver has one car; one car has on...
  • Caused by: org.postgresql.util.PSQLException: Large Objects may not be used in auto-commit mode.
    A large object refers to the entity property that is modified by @Lob. It may be persisted in several records. However, in database manage...

About Me

Yichun Zhao
View my complete profile

Blog Archive

  • September (2)
  • July (1)
  • June (1)
  • April (4)
  • March (2)
  • February (1)
  • January (1)
  • November (1)
  • September (5)
  • August (3)
  • July (1)
  • June (1)
  • July (3)
  • June (8)
  • May (3)
  • April (2)
  • March (1)
  • February (3)
  • January (9)
  • December (1)
  • October (1)
  • August (2)
  • July (4)
  • May (1)
  • April (2)
  • December (1)
  • October (8)
  • September (7)
  • August (4)
  • June (1)
  • May (2)
  • April (2)
  • February (1)
  • December (5)
  • September (2)
  • August (1)
  • July (2)
  • June (1)
  • May (5)
  • April (1)
  • March (5)
  • February (2)
  • January (2)
  • November (1)
  • October (1)
  • September (1)
  • August (4)
  • July (1)
  • June (2)
  • May (7)
  • April (6)
  • March (1)
  • November (1)
  • September (4)
  • August (9)
  • June (13)
  • May (20)
  • April (2)
  • March (2)
  • June (1)
  • September (3)
  • July (1)
  • March (1)
  • October (1)
  • September (1)
  • August (3)
  • April (1)
  • November (1)
  • September (5)
  • August (3)
  • July (4)
  • June (4)
  • May (1)

Labels

  • Algorithm
  • BigNumber
  • Database
  • Docker
  • Email
  • Exception
  • File IO
  • Generics and Collections
  • Git
  • GMap API
  • Heroku
  • IDE
  • Java 8
  • JavaCharts
  • JDBC
  • JPA-Hibernate
  • K8s
  • Keyword
  • Linux
  • Log
  • LomBok
  • MacOS
  • Maven
  • microservice
  • OCPJP
  • OOAD
  • Overriding
  • PaaS
  • Patterns
  • Performance
  • Persist LOB using JPA in J2EE/J2SE
  • PostgreSQL
  • Regx
  • Spring
  • SpringBoot
  • SpringBoot Test
  • SQL
  • Test
  • Threads and Concurrency
  • Time Complexity
  • URI

Report Abuse

  • Home

Search This Blog

Simple theme. Theme images by luoman. Powered by Blogger.