Skip to content

Commit c40af4f

Browse files
committed
betterer code (C) whatthecommit.com
1 parent faf56fa commit c40af4f

26 files changed

+727
-0
lines changed

.travis.yml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
notifications:
2+
email: false
3+
git:
4+
quiet: true
5+
depth: 1
6+
language: java
7+
jdk: openjdk8
8+
python: 3
9+
addons:
10+
apt:
11+
update: true
12+
packages:
13+
- sudo
14+
- lsof
15+
- wget
16+
- bash
17+
- curl
18+
- jq
19+
- python3-dev
20+
- python3-pip
21+
- python3-six
22+
- python3-setuptools
23+
install: true
24+
before_install:
25+
- export PATH=$HOME/.local/bin:$PATH
26+
- pip3 install --user $(whoami) --upgrade pip >/dev/null # pip3 -> pip
27+
- pip install --user $(whoami) --upgrade httpie >/dev/null 2>&1
28+
- http --version --debug
29+
#
30+
- source <(curl -s https://raw.githubusercontent.com/daggerok/bash-functions/master/main.bash)
31+
- stop_any 80 8001 8002 8080 5432
32+
jobs:
33+
include:
34+
- stage: test
35+
jdk: openjdk8
36+
name: mvn test
37+
script: cd $TRAVIS_BUILD_DIR && ./mvnw
38+
- stage: test
39+
jdk: openjdk11
40+
name: mvn test openjdk11
41+
script: cd $TRAVIS_BUILD_DIR && ./mvnw
42+
- stage: test
43+
jdk: openjdk8
44+
name: ddd integration tests
45+
script:
46+
- cd $TRAVIS_BUILD_DIR && ./mvnw -pl ddd
47+
- java -jar $TRAVIS_BUILD_DIR/ddd/target/*.jar &
48+
- wait_for 8002
49+
- http :8002 name=test
50+
- http :8002 name=test
51+
- http :8002 name="test 2"
52+
- http :8002/statistics
53+
- stop_any 80 8002
54+
- stage: test
55+
jdk: openjdk11
56+
name: ddd integration tests openjdk11
57+
script:
58+
- cd $TRAVIS_BUILD_DIR && ./mvnw -f $TRAVIS_BUILD_DIR/ddd/pom.xml
59+
- java -jar $TRAVIS_BUILD_DIR/ddd/target/*.jar &
60+
- wait_for 8002
61+
- http :8002 name=test
62+
- http :8002 name=test
63+
- http :8002 name="test 2"
64+
- http :8002/statistics
65+
- stop_any 80 8002
66+
- stage: test
67+
jdk: openjdk8
68+
name: simple integration tests
69+
script:
70+
- cd $TRAVIS_BUILD_DIR && ./mvnw -pl :simple
71+
- java -jar $TRAVIS_BUILD_DIR/simple/target/*.jar &
72+
- wait_for 8001
73+
- http :8001
74+
- http :8001 name=test
75+
- http :8001/find-all
76+
- stop_any 80 8001
77+
- stage: test
78+
jdk: openjdk11
79+
name: simple integration tests openjdk11
80+
script:
81+
- cd $TRAVIS_BUILD_DIR && ./mvnw -f simple/pom.xml
82+
- bash $TRAVIS_BUILD_DIR/simple/target/*.jar &
83+
- wait_for 8001
84+
- http :8001
85+
- http :8001 name=test
86+
- http :8001/find-all
87+
- stop_any 80 8001
88+
- stage: test
89+
jdk: openjdk8
90+
name: mvn versions:display-property-updates
91+
script: cd $TRAVIS_BUILD_DIR && ./mvnw versions:display-property-updates
92+
- stage: test
93+
jdk: openjdk11
94+
name: mvn versions:display-property-updates (openjdk11)
95+
script: cd $TRAVIS_BUILD_DIR && ./mvnw versions:display-property-updates
96+
cache:
97+
directories:
98+
- ~/.docker
99+
- ~/.m2
100+
packages: true
101+
pip: true

ddd/integration-tests.http

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
POST http://127.0.0.1:8002/
2+
Content-Type: application/json
3+
Accept: application/json
4+
5+
{
6+
"name": "test"
7+
}
8+
9+
###
10+
POST http://127.0.0.1:8002/
11+
Content-Type: application/json
12+
Accept: application/json
13+
14+
{
15+
"name": "test"
16+
}
17+
18+
###
19+
POST http://127.0.0.1:8002/
20+
Content-Type: application/json
21+
Accept: application/json
22+
23+
{
24+
"name": "test 2"
25+
}
26+
27+
###
28+
GET http://127.0.0.1:8002/statistics
29+
Content-Type: application/json
30+
Accept: application/json
31+
32+
###
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.github.daggerok.ddd.domain;
2+
3+
import com.github.daggerok.ddd.events.CustomerCreatedEvent;
4+
import lombok.Value;
5+
import lombok.experimental.Wither;
6+
import org.springframework.data.annotation.Id;
7+
import org.springframework.data.domain.AbstractAggregateRoot;
8+
9+
import java.time.LocalDate;
10+
11+
@Value
12+
@Wither
13+
public class Customer extends AbstractAggregateRoot<Customer> {
14+
15+
@Id
16+
Long id;
17+
String name;
18+
LocalDate date;
19+
Gender gender;
20+
21+
public static Customer createForName(String name) {
22+
Customer customer = new Customer(null, name, LocalDate.now(), null);
23+
CustomerCreatedEvent event = new CustomerCreatedEvent(customer);
24+
customer.registerEvent(event);
25+
return customer;
26+
}
27+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.github.daggerok.ddd.domain;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
5+
public interface CustomerRepository extends CrudRepository<Customer, Long> {}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.github.daggerok.ddd.domain;
2+
3+
import org.springframework.http.ResponseEntity;
4+
import org.springframework.transaction.annotation.Transactional;
5+
import org.springframework.web.bind.annotation.*;
6+
7+
import java.util.Map;
8+
import java.util.Objects;
9+
import java.util.concurrent.CompletableFuture;
10+
11+
import static java.util.Arrays.asList;
12+
13+
@RestController
14+
public class CustomerResource {
15+
16+
private final CustomerRepository customerRepository;
17+
18+
public CustomerResource(CustomerRepository customerRepository) {
19+
this.customerRepository = customerRepository;
20+
}
21+
22+
@GetMapping("/find-all")
23+
public CompletableFuture<Iterable<Customer>> findAll() {
24+
return CompletableFuture.supplyAsync(customerRepository::findAll);
25+
}
26+
27+
@PostMapping
28+
@Transactional
29+
public CompletableFuture<Customer> post(@RequestBody Map<String, String> req) {
30+
String name = Objects.requireNonNull(req.get("name"), "name is required");
31+
return CompletableFuture.supplyAsync(() -> {
32+
Customer neeCustomer = Customer.createForName(name);
33+
return customerRepository.save(neeCustomer);
34+
});
35+
}
36+
37+
@RequestMapping
38+
public ResponseEntity<?> apiFallback() {
39+
return ResponseEntity.ok(
40+
asList(
41+
"create a customer: http post :8001 name={name}",
42+
"get all customers: http get :8001/find-all"
43+
)
44+
);
45+
}
46+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.github.daggerok.ddd.domain;
2+
3+
import org.springframework.data.annotation.Id;
4+
5+
class Gender {
6+
7+
@Id
8+
Long id;
9+
String whoCares;
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.github.daggerok.ddd.events;
2+
3+
import org.springframework.context.ApplicationEvent;
4+
5+
public class CustomerCreatedEvent extends ApplicationEvent {
6+
public CustomerCreatedEvent(Object source) {
7+
super(source);
8+
}
9+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.github.daggerok.ddd.events;
2+
3+
import com.github.daggerok.ddd.domain.Customer;
4+
import com.github.daggerok.ddd.domain.CustomerRepository;
5+
import lombok.RequiredArgsConstructor;
6+
import lombok.extern.log4j.Log4j2;
7+
import org.springframework.context.event.EventListener;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
import javax.annotation.PostConstruct;
13+
import java.util.Map;
14+
import java.util.concurrent.ConcurrentHashMap;
15+
import java.util.concurrent.atomic.AtomicLong;
16+
import java.util.stream.StreamSupport;
17+
18+
@Log4j2
19+
@RestController
20+
@RequiredArgsConstructor
21+
public class StatisticsResource {
22+
23+
private final CustomerRepository customerRepository;
24+
25+
private Map<String, AtomicLong> statistics = new ConcurrentHashMap<>();
26+
27+
@PostConstruct
28+
public void reconstruct() {
29+
StreamSupport.stream(customerRepository.findAll().spliterator(), true)
30+
.forEach(this::updateStatisticsFor);
31+
}
32+
33+
@GetMapping("/statistics")
34+
public ResponseEntity<Map<String, AtomicLong>> getStatistics() {
35+
return ResponseEntity.ok(statistics);
36+
}
37+
38+
@EventListener
39+
public void on(CustomerCreatedEvent event) {
40+
log.info("received: {}", event);
41+
Customer newCustomer = (Customer) event.getSource();
42+
updateStatisticsFor(newCustomer);
43+
}
44+
45+
private void updateStatisticsFor(Customer newCustomer) {
46+
String name = newCustomer.getName();
47+
statistics.putIfAbsent(name, new AtomicLong(0));
48+
AtomicLong counter = statistics.get(name);
49+
counter.incrementAndGet();
50+
statistics.put(name, counter);
51+
}
52+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.github.daggerok.ddd;
2+
3+
import com.github.daggerok.ddd.domain.Customer;
4+
import com.github.daggerok.ddd.domain.CustomerRepository;
5+
import lombok.extern.log4j.Log4j2;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.extension.ExtendWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.autoconfigure.data.jdbc.DataJdbcTest;
10+
import org.springframework.test.context.junit.jupiter.SpringExtension;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
@Log4j2
15+
@DataJdbcTest
16+
@ExtendWith(SpringExtension.class)
17+
class CustomerRepositoryTest {
18+
19+
@Autowired
20+
CustomerRepository customerRepository;
21+
22+
@Test
23+
void test() {
24+
log.info("junit jupiter test");
25+
Customer newCustomer = Customer.createForName("ololo trololo");
26+
Customer savedCustomer = customerRepository.save(newCustomer);
27+
assertThat(savedCustomer.getName()).isEqualTo(savedCustomer.getName());
28+
assertThat(savedCustomer.getId()).isNotNull();
29+
log.info("savedCustomer {}", savedCustomer);
30+
}
31+
}

simple/integration-tests.http

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
GET http://127.0.0.1:8001/
2+
Content-Type: application/json
3+
Accept: application/json
4+
5+
###
6+
GET http://127.0.0.1:8001/find-all
7+
Content-Type: application/json
8+
Accept: application/json
9+
10+
###
11+
POST http://127.0.0.1:8001/
12+
Content-Type: application/json
13+
Accept: application/json
14+
15+
{
16+
"name": "test 2"
17+
}
18+
19+
###
20+
GET http://127.0.0.1:8001/find-all
21+
Content-Type: application/json
22+
Accept: application/json
23+
24+
###

0 commit comments

Comments
 (0)