Skip to content

Commit 67ae964

Browse files
committed
introduce customer and user api tests
1 parent 7789128 commit 67ae964

9 files changed

Lines changed: 193 additions & 77 deletions

File tree

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ dependencies {
5454
testImplementation 'io.projectreactor:reactor-test'
5555
testImplementation 'org.springframework.security:spring-security-test'
5656
testImplementation 'org.easymock:easymock:5.+'
57+
testImplementation 'com.h2database:h2'
5758
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
5859
}
5960

6061
tasks.named('test') {
62+
environment "DEBUG", "true"
6163
useJUnitPlatform()
6264
}

run/intellij/Test Run.http

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
POST http://localhost:8080/api/users
2+
Content-Type: application/json
3+
Authorization: Basic ZGV2Og==
4+
5+
{
6+
"username": "Brother Jim"
7+
}
8+
9+
###
10+
111
POST http://localhost:8080/api/customers
212
Content-Type: application/json
313
Authorization: Basic ZGV2Og==
@@ -42,6 +52,24 @@ Authorization: Basic ZGV2Og==
4252

4353
###
4454

55+
POST http://localhost:8080/api/timetableEntries/createAssignment
56+
Content-Type: application/json
57+
Authorization: Basic ZGV2Og==
58+
59+
{
60+
"entryInfo": {
61+
"customerInfo": {
62+
"name": "Lord Faarquardt",
63+
"department": "Private House"
64+
},
65+
"startTime": "2025-09-05T14:34:56"
66+
},
67+
"username": "Brother Jim",
68+
"notes": "hat für den bauherren gebetet"
69+
}
70+
71+
###
72+
4573
POST http://localhost:8080/api/timetableEntries/createInterruption
4674
Content-Type: application/json
4775
Authorization: Basic ZGV2Og==

src/main/java/de/kaleidox/workbench/WorkbenchApplication.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.boot.SpringApplication;
99
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
1011
import org.springframework.boot.autoconfigure.domain.EntityScan;
12+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
1113
import org.springframework.boot.jdbc.DataSourceBuilder;
1214
import org.springframework.context.annotation.Bean;
1315
import org.springframework.context.annotation.ImportResource;
16+
import org.springframework.core.annotation.Order;
1417
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
1518

1619
import javax.sql.DataSource;
@@ -20,10 +23,10 @@
2023
import java.util.Objects;
2124

2225
@Log
23-
@SpringBootApplication
2426
@ImportResource({ "classpath:beans.xml" })
25-
@EnableJpaRepositories(basePackages = "de.kaleidox.workbench.repo")
2627
@EntityScan(basePackages = "de.kaleidox.workbench.model.jpa")
28+
@EnableJpaRepositories(basePackages = "de.kaleidox.workbench.repo")
29+
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
2730
public class WorkbenchApplication {
2831
public static void main(String[] args) {
2932
SpringApplication.run(WorkbenchApplication.class, args);
@@ -49,6 +52,8 @@ public AppConfig config(@Autowired File configFile, @Autowired ObjectMapper obje
4952
}
5053

5154
@Bean
55+
@Order
56+
@ConditionalOnMissingBean(DataSource.class)
5257
public DataSource dataSource(@Autowired AppConfig config) {
5358
var db = config.database();
5459
return DataSourceBuilder.create()

src/main/java/de/kaleidox/workbench/model/jpa/representant/Customer.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
import jakarta.persistence.Entity;
55
import jakarta.persistence.Id;
66
import jakarta.persistence.IdClass;
7+
import lombok.AllArgsConstructor;
78
import lombok.Data;
89
import lombok.EqualsAndHashCode;
10+
import lombok.NoArgsConstructor;
911

1012
@Data
1113
@Entity
12-
@IdClass(Customer.CompositeKey.class)
14+
@NoArgsConstructor
15+
@AllArgsConstructor
1316
@EqualsAndHashCode(of = "name")
17+
@IdClass(Customer.CompositeKey.class)
1418
public class Customer {
1519
@Id String name;
1620
@Id String department;
@@ -20,6 +24,10 @@ public String toString() {
2024
return name;
2125
}
2226

27+
public CompositeKey key() {
28+
return new CompositeKey(name, department);
29+
}
30+
2331
@Embeddable
2432
public record CompositeKey(String name, String department) {}
2533
}

src/test/java/de/kaleidox/workbench/TimetableApiTest.java

Lines changed: 0 additions & 74 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package de.kaleidox.workbench.test;
2+
3+
import de.kaleidox.workbench.model.jpa.representant.User;
4+
import org.h2.Driver;
5+
import org.springframework.boot.jdbc.DataSourceBuilder;
6+
import org.springframework.boot.test.web.client.TestRestTemplate;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
import javax.sql.DataSource;
11+
12+
@Configuration
13+
public class TestConfiguration {
14+
@Bean
15+
public DataSource dataSource() {
16+
return DataSourceBuilder.create()
17+
.url("jdbc:h2:mem:test")
18+
.username("sa")
19+
.password("")
20+
.driverClassName(Driver.class.getCanonicalName())
21+
.build();
22+
}
23+
24+
@Bean
25+
public TestRestTemplate rest() {
26+
return new TestRestTemplate(User.DEV.getUsername(), "");
27+
}
28+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package de.kaleidox.workbench.test.api;
2+
3+
import de.kaleidox.workbench.model.jpa.representant.Customer;
4+
import de.kaleidox.workbench.repo.CustomerRepository;
5+
import org.junit.jupiter.api.Order;
6+
import org.junit.jupiter.api.Test;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.boot.test.web.client.TestRestTemplate;
10+
11+
import java.util.List;
12+
13+
import static org.junit.jupiter.api.Assertions.*;
14+
15+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
16+
public class CustomerApiTest {
17+
public static final String FAARQUARDT_NAME = "Lord Faarquardt";
18+
public static final Customer FAARQUARDT_CASTLE = new Customer(FAARQUARDT_NAME, "Castle");
19+
public static final Customer FAARQUARDT_HOUSE = new Customer(FAARQUARDT_NAME, "House");
20+
21+
@Autowired
22+
private CustomerRepository customers;
23+
@Autowired
24+
private TestRestTemplate rest;
25+
26+
@Test
27+
@Order(0)
28+
void contextLoads() {
29+
assertNotNull(customers);
30+
}
31+
32+
@Test
33+
@Order(10)
34+
public void createCustomers() {
35+
for (var customer : List.of(FAARQUARDT_CASTLE, FAARQUARDT_HOUSE)) {
36+
var response = rest.postForEntity("http://localhost:8080/api/customers", customer, Void.class);
37+
assertEquals(201, response.getStatusCode().value(), "status code mismatch");
38+
39+
var result = customers.findById(customer.key());
40+
assertTrue(result.isPresent(), "customer not found");
41+
assertEquals(customer, result.get());
42+
}
43+
}
44+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package de.kaleidox.workbench.test.api;
2+
3+
import de.kaleidox.workbench.repo.TimetableEntryRepository;
4+
import org.junit.jupiter.api.Order;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.boot.test.web.client.TestRestTemplate;
9+
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
13+
public class TimetableApiTest {
14+
@Autowired private TimetableEntryRepository entries;
15+
@Autowired private TestRestTemplate rest;
16+
17+
@Test
18+
@Order(0)
19+
void contextLoads() {
20+
assertNotNull(entries);
21+
}
22+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package de.kaleidox.workbench.test.api;
2+
3+
import de.kaleidox.workbench.model.jpa.representant.User;
4+
import de.kaleidox.workbench.repo.UserRepository;
5+
import org.junit.jupiter.api.Order;
6+
import org.junit.jupiter.api.Test;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.boot.test.web.client.TestRestTemplate;
10+
11+
import java.util.List;
12+
13+
import static org.junit.jupiter.api.Assertions.*;
14+
15+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
16+
public class UserApiTest {
17+
public static final User USER_1 = new User("user1", "User One");
18+
public static final User USER_2 = new User("user2", "User Two");
19+
20+
@Autowired
21+
private UserRepository users;
22+
@Autowired
23+
private TestRestTemplate rest;
24+
25+
@Test
26+
@Order(0)
27+
void contextLoads() {
28+
assertNotNull(users);
29+
}
30+
31+
@Test
32+
@Order(1)
33+
public void fetchSelf() {
34+
var self = rest.getForEntity("http://localhost:8080/api/me", User.class).getBody();
35+
36+
assertNotNull(self, "self not found");
37+
assertEquals(User.DEV.getUsername(), self.getUsername(), "username mismatch");
38+
assertEquals(User.DEV.getDisplayName(), self.getDisplayName(), "displayname mismatch");
39+
}
40+
41+
@Test
42+
@Order(10)
43+
public void createUsers() {
44+
for (var user : List.of(USER_1, USER_2)) {
45+
var response = rest.postForEntity("http://localhost:8080/api/users", user, Void.class);
46+
assertEquals(201, response.getStatusCode().value(), "status code mismatch");
47+
48+
var result = users.findById(user.getUsername());
49+
assertTrue(result.isPresent(), "user not found");
50+
assertEquals(user, result.get());
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)