Skip to content

Commit

Permalink
Added jpa tests with h2 + tested postgres profile local #3634
Browse files Browse the repository at this point in the history
  • Loading branch information
lorriborri committed Nov 22, 2024
1 parent c564f62 commit 1e583e8
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface UserRepository extends JpaRepository<User, String> {
}
2 changes: 1 addition & 1 deletion sechub-web-server/src/main/resources/application-h2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ spring:
flyway:
enabled: true
locations:
- classpath:db/migration
- classpath:db/migration/common
- classpath:db/migration/h2
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ spring:
driver-class-name: org.postgresql.Driver
flyway:
enabled: true
locations: classpath:db/migration
locations: classpath:db/migration/common
jpa:
database:
database-platform: org.hibernate.dialect.PostgreSQLDialect

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- SPDX-License-Identifier: MIT
CREATE TABLE adm_user
(
user_id varchar(60) not null,
user_email_address varchar(255) not null,
user_roles VARCHAR(50) NOT NULL,
PRIMARY KEY (user_id)
);
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.test.context.ActiveProfiles;

import java.util.List;
import java.util.Optional;

@DataJpaTest
@ActiveProfiles("h2")
Expand All @@ -25,4 +26,48 @@ void assert_flyway_test_user_is_in_h2_database() {
assert (users.get(0).getEmailAddress().equals("[email protected]"));
assert (users.get(0).getRoles().equals("USER"));
}

@Test
void save_user_to_database() {
/* prepare */
User user = new User();
user.setName("testuser2");
user.setEmailAddress("[email protected]");
user.setRoles("USER");

/* execute */
userRepository.save(user);

/* test */
List<User> users = userRepository.findAll();
assert (users.size() == 2);
}

@Test
void find_user_from_database() {
/* execute */
Optional<User> optUser = userRepository.findById("testuser");

/* test */
assert (optUser.isPresent());
User user = optUser.get();
assert (user.getName().equals("testuser"));
assert (user.getEmailAddress().equals("[email protected]"));
assert (user.getRoles().equals("USER"));
}

@Test
void save_and_delete_user_from_database() {
/* prepare */
Optional<User> optUser = userRepository.findById("testuser");
assert (optUser.isPresent());
User user = optUser.get();

/* execute */
userRepository.delete(user);

/* test */
List<User> users = userRepository.findAll();
assert (users.isEmpty());
}
}

0 comments on commit 1e583e8

Please sign in to comment.