Spring Boot integration test against real postgres DB - postgresql

I have been looking at this problem now for hours and I know it's something silly.
My Spring Boot integration test can't find my Postgres table (relation) while the REST API works fine.
org.postgresql.util.PSQLException: ERROR: relation "data_source" does not exist
The DB is Postgres in Docker container (not using in mem container).
Here relevant code.
Any help will be much appreciated !
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
</dependencies>
test/resources/application.properties
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQL95Dialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.initialization-mode=always
spring.datasource.initialize=true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
# parses java variables in camel case to data base naming convention with underscores
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
#SpringBootTest
#AutoConfigureMockMvc
public class TrainingDataServiceIT {
#Autowired
private MockMvc mockMvc;
#Autowired
private ObjectMapper objectMapper;
#Autowired
DataSourceRepository dataSourceRepository;
#Test
public void myTest() throws Exception {
mockMvc.perform(post("/get-ds")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content()
.contentTypeCompatibleWith(MediaType.APPLICATION_JSON));
}
EDIT to add my entity (columns emitted):
#Entity
#DynamicUpdate
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
#Table(name = "data_source")
public class DataSourceEntity {
EDIT
postgres=# \dn
List of schemas
Name | Owner
--------+----------
public | postgres
(1 row)
Edit:
version: '3'
services:
postgres:
image: postgres:10.15-alpine
container_name: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
volumes:
- ./postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
ports:
- 5432:5432

Related

The background display of the startup project is as follows

Spring integrates jwt to realize login.
pom.xml configuration
<!--security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--JWT-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
application.yml configuration
logging:
level:
com.xxxx.server.mapper: debug
jwt:
tokenHeader: Authorization
secret: yeb-secret
expiration: 604800
tokenHead: Bearer
Running the project background prompts the following information.
Description:
Failed to bind properties under 'logging.level.jwt.tokenheader' to org.springframework.boot.logging.LogLevel:
Property: logging.level.jwt.tokenheader
Value: "Authorization"
Origin: class path resource [config/application.yml] - 52:20
Reason: failed to convert java.lang.String to org.springframework.boot.logging.LogLevel (caused by java.lang.IllegalArgumentException: No enum constant org.springframework.boot.logging.LogLevel.Authorization)
Why it can't works?
thanks!!

Failed connection when trying for Heroku PostgreSQL with Spring

I am trying to set up a Spring Boot application. Currently I am working in a local environment and I have a Heroku PostgreSQL db. When I start my application it starts with error:
HikariPool-1 - Exception during pool initialization.
org.postgresql.util.PSQLException: El intento de conexión falló.
...
Caused by: java.net.UnknownHostException: #host
...
HHH000342: Could not obtain connection to query metadata
Where I have added the correct hostname and hideen it for security. I know this hostname is right since I've managed to connect using an Express app.
My POM.xml has the following dependencies:}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
And my application.properties file is:
spring.datasource.url=jdbc:postgresql://#host:5432/dal2fiqv9or5eg?user=----&password=----&sslmode=require
spring.datasource.username=----
spring.datasource.password=----
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa-hibernate.ddl-auto=create-drop
spring.jpa.show=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true
Whre once again I hid the host, username and pwd.
After this the application starts.
The spring.datasource.url property in your application.properties seems to be incorrect.
This file has worked for me:
#postgres parameters
spring.datasource.url=jdbc:postgresql://<host>:<port>/<database>
spring.datasource.username=<user>
spring.datasource.password=<password>
spring.jpa.show-sql=true
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
logging.level.org.springframework=TRACE
logging.level.org.hibernate.type=TRACE
server.port=${PORT:8080}
The values for port, host, database, user, and password can be derived from the HerokuPostgres addon you add to your Heroku app
Do not add # or any special symobols. Simply replace the values with the values you see in Heroku.
eg: spring.datasource.url=jdbc:postgresql://myhostname:5432/mydbname

MongoTemplate.getCollectionNames and MongoTemplate,collectionExists method throws StringIndexOutOfBoundsException when connected to Azure Cosmos DB

I am trying to get the list of collections on a database. My application uses spring-boot 2.4.1 and Azure Cosmos DB. Below is the code block.
String uri= "mongodb://<username>:<password>#<hostname.documents.azure.com>:<port>/?ssl=true";
String dbName= "databaseName";
MongoClient mongoClient = MongoClients.create(uri);
SimpleMongoClientDatabaseFactory mongoDbFactory =
new SimpleMongoClientDatabaseFactory(mongoClient, dbName);
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory);
System.out.println(mongoTemplate.getCollectionNames());
The code throws a very strange StringIndexOutOfBoundsException.
Stacktrace:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -4
at java.lang.String.substring(String.java:1931)
at com.mongodb.internal.operation.ListCollectionsOperation.projectFromFullNamespaceToCollectionName(ListCollectionsOperation.java:512)
at com.mongodb.internal.operation.ListCollectionsOperation.access$1500(ListCollectionsOperation.java:86)
at com.mongodb.internal.operation.ListCollectionsOperation$ProjectingBatchCursor.next(ListCollectionsOperation.java:418)
at com.mongodb.client.internal.MongoBatchCursorAdapter.next(MongoBatchCursorAdapter.java:64)
at com.mongodb.client.internal.MongoMappingCursor.next(MongoMappingCursor.java:48)
at org.springframework.data.mongodb.core.MongoTemplate.lambda$getCollectionNames$28(MongoTemplate.java:2314)
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:535)
at org.springframework.data.mongodb.core.MongoTemplate.getCollectionNames(MongoTemplate.java:2312)
at TestApp.main(TestApp.java:20)
When I am connecting to a mongo instance that is running on my local with the connection string "mongodb://localhost:27017" the above code works fine.
And my relevant part of the pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
</parent>
<groupId>org.example</groupId>
<artifactId>test-mongo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
</project>
Any thoughts about this error?
Are you sure your connection uri correctness?
i cant see password part your uri
Add the password section uri like this
String uri= "mongodb://<username>:<password>#...

Spring Boot admin not showing any client applications in dashboard

I have been following this tutorial for Spring boot Admin, but when I run the admin application I can't see any spring boot client application for monitoring. I have checked that both the admin and client apps are running. Admin is running on port 8080 and client application is running on port 9010
This is what I have done in my Admin application
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>1.5.3</version>
</dependency>
Added these annotations
#Configuration
#SpringBootApplication
#EnableAdminServer
public class SpringBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}
This is what I have added in my client application
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>1.5.3</version>
</dependency>
and added this in my properties file (YAML)
spring:
boot:
admin:
url:
- http://localhost:8080
management:
security:
enabled: false
This is what my Dashboard looks like, as you can see there are no applications to monitor.

Connecting Mongodb Atlas from Spring boot

I i am new to the Mongodb with Spring Boot.And i have MONGODB ATLAS URI Connection String
mongodb://userName:<PASSWORD>#icarat-shard-00-00-7lopx.mongodb.net:27017,icarat-shard-00-01-7lopx.mongodb.net:27017,icarat-shard-00-02-7lopx.mongodb.net:27017/<DATABASE>?ssl=true&replicaSet=icarat-shard-0&authSource=admin
Then in my Spring Boot Application i set uri in application.properties like
spring.data.mongodb.uri: mongodb://userName:*****#icarat-shard-00-00-7lopx.mongodb.net:27017,icarat-shard-00-01-7lopx.mongodb.net:27017,icarat-shard-00-02-7lopx.mongodb.net:27017/vehicleplatform?ssl=true&replicaSet=icarat-shard-0&authSource=admin
This Repository intefface
public interface OrganizationRepository extends MongoRepository<Organization, String> {
}
when i Inject OrganizationRepository interface Its showing error like this
Failed to instantiate [com.mongodb.MongoClient]: Factory method 'mongo' threw exception; nested exception is java.lang.IllegalArgumentException: The connection string contains invalid user information. If the username or password contains a colon (:) or an at-sign (#) then it must be urlencoded
And this my Document class
#Document(collection="Organization")
public class Organization {
#Id
String id;
String orgName;
String orgAddress;
String pinCode;
//getter
//setter
}
This is my pom.xml
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.icarat</groupId>
<artifactId>vehicleplatform</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>vehicleplatform</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mongodb java driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.11.0</version>
</dependency>
<!-- swagger2 dependency -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId> springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
How we can fix this? Thanks
I find the actual problem. In my pevious Atlas password contain # (symbol) so its not supoort. I follwed document HERE
IMPORTANT If the password contains reserved URI characters, you must
escape the characters per RFC 2396. For example, if your password is
#bc123, you must escape the # character when specifying the password
in the connection string; i.e. %40bc123.
Failed to instantiate [com.mongodb.MongoClient]: Factory method
'mongo' threw exception; nested exception is
java.lang.IllegalArgumentException: The connection string contains
invalid user information. If the username or password contains a colon
(:) or an at-sign (#) then it must be urlencoded
The problem is crystal clear, if you are trying to connect with MongoDB Atlas with SpringBoot, your password must be URL Encoded which is more secure.
Here you can see how to URL encode password in MongoDB Atlas.
You can use the Mongodb SRV record to define all nodes associated to the cluster
# Mongo Connection
spring.data.mongodb.uri=mongodb+srv://myuser:mypassword#mycluster.azure.mongodb.net/mydb?retryWrites=true&w=majority
where
myuser: Mongodb user
mypassword: password of the user above
mycluster.azure.mongodb.net: host of your cluster
mydb: Mongodb database of your application
It is important to use the correct SRV assigned to you by Atlas (in your example you have combined all three shards), which you can get in the "Connect Your Application" screen, together with a code example (if needed).
Copy the connection string, replace password and dbname, set this in the SpringBoot property file.
I think best escape is to follow this pattern:
spring.data.mongodb.authentication-database= # Authentication database name.
spring.data.mongodb.database=db_name.
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.password=passwrd
spring.data.mongodb.port=27017
spring.data.mongodb.repositories.enabled=true # Enable Mongo repositories.
spring.data.mongodb.username= # Login user of the mongo server.
In my case I'm added {} surrounded with the documentDb url
I faced same issue when connecting to Mongo DB Atlas from spring boot:
'If the username or password contains a colon (:) or an at-sign (#) then it must be urlencoded'
This occurs if the password contains above mentioned special characters,
I was able to solve the issue by using the encoded password.
Go to https://www.urlencoder.io/ -> type the password to be encoded, Example: 'sample#password' , encoded string will be 'sample%40password'. Now use this in your connection uri by replacing the password
the password value will look something like
mongodb://userName:sample%40password#icarat-shard-00-00-7lopx.mongodb.net:27017,icarat-shard-00-01-7lopx.mongodb.net:27017,icarat-shard-00-02-7lopx.mongodb.net:27017/testdb?ssl=true&replicaSet=icarat-shard-0&authSource=admin