Skip to content

Commit

Permalink
Refine props reading (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavetok authored Oct 14, 2023
1 parent 7854166 commit 22e164d
Show file tree
Hide file tree
Showing 71 changed files with 343 additions and 261 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
import smecalculus.bezmen.core.SepulkaSliceMapperImpl;
import smecalculus.bezmen.messaging.SepulkaClient;
import smecalculus.bezmen.messaging.SepulkaClientImpl;
import smecalculus.bezmen.messaging.SepulkaMsgMapper;
import smecalculus.bezmen.messaging.SepulkaMsgMapperImpl;
import smecalculus.bezmen.messaging.SepulkaMessageMapper;
import smecalculus.bezmen.messaging.SepulkaMessageMapperImpl;
import smecalculus.bezmen.messaging.springmvc.SepulkaController;
import smecalculus.bezmen.storage.SepulkaDao;
import smecalculus.bezmen.storage.SepulkaDaoMyBatis;
import smecalculus.bezmen.storage.SepulkaDaoSpringData;
import smecalculus.bezmen.storage.SepulkaRecMapper;
import smecalculus.bezmen.storage.SepulkaRecMapperImpl;
import smecalculus.bezmen.storage.SepulkaStateMapper;
import smecalculus.bezmen.storage.SepulkaStateMapperImpl;
import smecalculus.bezmen.storage.mybatis.SepulkaSqlMapper;
import smecalculus.bezmen.storage.springdata.SepulkaRepository;
import smecalculus.bezmen.validation.EdgeValidator;
Expand All @@ -43,12 +43,12 @@ SepulkaController sepulkaControllerSpringMvc(SepulkaClient client) {
}

@Bean
SepulkaMsgMapper sepulkaMsgMapper() {
return new SepulkaMsgMapperImpl();
SepulkaMessageMapper sepulkaMessageMapper() {
return new SepulkaMessageMapperImpl();
}

@Bean
SepulkaClient sepulkaClient(EdgeValidator validator, SepulkaMsgMapper mapper, SepulkaService service) {
SepulkaClient sepulkaClient(EdgeValidator validator, SepulkaMessageMapper mapper, SepulkaService service) {
return new SepulkaClientImpl(validator, mapper, service);
}

Expand All @@ -63,19 +63,19 @@ SepulkaService sepulkaService(SepulkaSliceMapper mapper, SepulkaDao dao) {
}

@Bean
SepulkaRecMapper sepulkaRecMapper() {
return new SepulkaRecMapperImpl();
SepulkaStateMapper sepulkaStateMapper() {
return new SepulkaStateMapperImpl();
}

@Bean
@ConditionalOnStateMappingMode(SPRING_DATA)
SepulkaDaoSpringData sepulkaDaoSpringData(SepulkaRecMapper mapper, SepulkaRepository repository) {
SepulkaDaoSpringData sepulkaDaoSpringData(SepulkaStateMapper mapper, SepulkaRepository repository) {
return new SepulkaDaoSpringData(mapper, repository);
}

@Bean
@ConditionalOnStateMappingMode(MY_BATIS)
SepulkaDaoMyBatis sepulkaDaoMyBatis(SepulkaRecMapper recMapper, SepulkaSqlMapper sqlMapper) {
SepulkaDaoMyBatis sepulkaDaoMyBatis(SepulkaStateMapper recMapper, SepulkaSqlMapper sqlMapper) {
return new SepulkaDaoMyBatis(recMapper, sqlMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class SepulkaServiceImpl implements SepulkaService {

@Override
public SepulkaNewResponse register(SepulkaNewRequest request) {
var sepulkaCreated = mapper.toDomain(request).id(randomUUID()).build();
var sepulkaCreated = mapper.toEntity(request).id(randomUUID()).build();
var sepulkaSaved = dao.save(sepulkaCreated);
return mapper.toSlice(sepulkaSaved);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ public class SepulkaClientImpl implements SepulkaClient {
private EdgeValidator validator;

@NonNull
private SepulkaMsgMapper mapper;
private SepulkaMessageMapper mapper;

@NonNull
private SepulkaService service;

@Override
public SepulkaNewResponseMsg register(SepulkaNewRequestMsg requestMsg) {
validator.validate(requestMsg);
var request = mapper.toDomain(requestMsg);
public SepulkaNewResponseEdge register(SepulkaNewRequestEdge requestEdge) {
validator.validate(requestEdge);
var request = mapper.toDomain(requestEdge);
var response = service.register(request);
return mapper.toMsg(response);
return mapper.toEdge(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import smecalculus.bezmen.messaging.SepulkaClient;
import smecalculus.bezmen.messaging.SepulkaNewRequestMsg;
import smecalculus.bezmen.messaging.SepulkaNewResponseMsg;
import smecalculus.bezmen.messaging.SepulkaNewRequestEdge;
import smecalculus.bezmen.messaging.SepulkaNewResponseEdge;

@RestController
@RequestMapping("sepulkas")
Expand All @@ -21,8 +21,8 @@ public class SepulkaController {
private SepulkaClient client;

@PostMapping
ResponseEntity<SepulkaNewResponseMsg> register(@RequestBody SepulkaNewRequestMsg requestMsg) {
var responseMsg = client.register(requestMsg);
return ResponseEntity.status(HttpStatus.CREATED).body(responseMsg);
ResponseEntity<SepulkaNewResponseEdge> register(@RequestBody SepulkaNewRequestEdge requestEdge) {
var responseEdge = client.register(requestEdge);
return ResponseEntity.status(HttpStatus.CREATED).body(responseEdge);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.util.UUID;
import smecalculus.bezmen.core.Sepulka;

/**
* Server side interface
*/
public interface SepulkaDao {

Sepulka getById(UUID id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@
public class SepulkaDaoMyBatis implements SepulkaDao {

@NonNull
private SepulkaRecMapper recMapper;
private SepulkaStateMapper stateMapper;

@NonNull
private SepulkaSqlMapper sqlMapper;

@Override
public Sepulka getById(@NonNull UUID id) {
return sqlMapper.findById(id.toString()).map(recMapper::toDomain).orElse(null);
return sqlMapper.findById(id.toString()).map(stateMapper::toDomain).orElse(null);
}

@Override
public Sepulka save(@NonNull Sepulka sepulka) {
SepulkaRec sepulkaRec = recMapper.toRec(sepulka);
sqlMapper.insert(sepulkaRec);
var sepulkaEdge = stateMapper.toEdge(sepulka);
sqlMapper.insert(sepulkaEdge);
return sepulka;
}

@Override
public List<Sepulka> getSepulkas() {
return sqlMapper.selectAll().stream().map(recMapper::toDomain).collect(toList());
return sqlMapper.selectAll().stream().map(stateMapper::toDomain).collect(toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class SepulkaDaoSpringData implements SepulkaDao {

@NonNull
private SepulkaRecMapper mapper;
private SepulkaStateMapper mapper;

@NonNull
private SepulkaRepository repository;
Expand All @@ -26,9 +26,9 @@ public Sepulka getById(@NonNull UUID id) {

@Override
public Sepulka save(@NonNull Sepulka sepulka) {
SepulkaRec newSepulkaRec = mapper.toRec(sepulka);
SepulkaRec savedSepulkaRec = repository.save(newSepulkaRec);
return mapper.toDomain(savedSepulkaRec);
var newSepulkaEdge = mapper.toEdge(sepulka);
var savedSepulkaEdge = repository.save(newSepulkaEdge);
return mapper.toDomain(savedSepulkaEdge);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
@Data
@Table("sepulkas")
public class SepulkaRec {
public class SepulkaEdge {
@Id
String id;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package smecalculus.bezmen.storage;

import org.mapstruct.Mapper;
import smecalculus.bezmen.core.Sepulka;

@Mapper
public interface SepulkaStateMapper {
SepulkaEdge toEdge(Sepulka sepulka);

Sepulka toDomain(SepulkaEdge sepulkaEdge);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import smecalculus.bezmen.storage.SepulkaRec;
import smecalculus.bezmen.storage.SepulkaEdge;

public interface SepulkaSqlMapper {

@Insert("INSERT INTO sepulkas (id, version, name) VALUES (#{id}, #{version}, #{name})")
void insert(SepulkaRec sepulkaRec);
void insert(SepulkaEdge sepulkaEdge);

@Select("SELECT id, version, name FROM sepulkas WHERE id = #{id}")
Optional<SepulkaRec> findById(@Param("id") String id);
Optional<SepulkaEdge> findById(@Param("id") String id);

@Select("SELECT * FROM sepulkas")
List<SepulkaRec> selectAll();
List<SepulkaEdge> selectAll();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package smecalculus.bezmen.storage.springdata;

import org.springframework.data.repository.CrudRepository;
import smecalculus.bezmen.storage.SepulkaRec;
import smecalculus.bezmen.storage.SepulkaEdge;

public interface SepulkaRepository extends CrudRepository<SepulkaRec, String> {}
public interface SepulkaRepository extends CrudRepository<SepulkaEdge, String> {}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import smecalculus.bezmen.core.SepulkaService;
import smecalculus.bezmen.messaging.SepulkaClient;
import smecalculus.bezmen.messaging.SepulkaClientImpl;
import smecalculus.bezmen.messaging.SepulkaClientSpringWeb;
import smecalculus.bezmen.messaging.SepulkaMsgMapperImpl;
import smecalculus.bezmen.messaging.SepulkaClientSpringWebTest;
import smecalculus.bezmen.messaging.SepulkaMessageMapperImpl;
import smecalculus.bezmen.messaging.springmvc.SepulkaController;
import smecalculus.bezmen.validation.EdgeValidator;

@Import(ValidationBeans.class)
@Import({ConfigBeans.class, ValidationBeans.class})
@Configuration(proxyBeanMethods = false)
public class SepulkaClientBeans {

Expand All @@ -25,14 +25,14 @@ public SepulkaService sepulkaService() {

@Bean
SepulkaClient internalClient(EdgeValidator validator, SepulkaService service) {
var mapper = new SepulkaMsgMapperImpl();
var mapper = new SepulkaMessageMapperImpl();
return new SepulkaClientImpl(validator, mapper, service);
}

@Bean
SepulkaClient externalClient(SepulkaClient internalClient) {
var client = MockMvcWebTestClient.bindToController(new SepulkaController(internalClient))
.build();
return new SepulkaClientSpringWeb(client);
return new SepulkaClientSpringWebTest(client);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import smecalculus.bezmen.storage.SepulkaDao;
import smecalculus.bezmen.storage.SepulkaDaoMyBatis;
import smecalculus.bezmen.storage.SepulkaDaoSpringData;
import smecalculus.bezmen.storage.SepulkaRecMapper;
import smecalculus.bezmen.storage.SepulkaRecMapperImpl;
import smecalculus.bezmen.storage.SepulkaStateMapper;
import smecalculus.bezmen.storage.SepulkaStateMapperImpl;
import smecalculus.bezmen.storage.mybatis.SepulkaSqlMapper;
import smecalculus.bezmen.storage.springdata.SepulkaRepository;

Expand All @@ -31,13 +31,13 @@ public class SepulkaDaoBeans {

@Bean
public DataSource dataSource(StorageProps storageProps) {
List<String> common = List.of("DB_CLOSE_DELAY=-1");
List<String> specific =
var common = List.of("DB_CLOSE_DELAY=-1");
var specific =
switch (storageProps.protocolProps().protocolMode()) {
case H2 -> List.of("MODE=STRICT");
case POSTGRES -> List.of("MODE=PostgreSQL", "DATABASE_TO_LOWER=TRUE", "DEFAULT_NULL_ORDERING=HIGH");
};
String nameWithSettings = Stream.of(List.of(DB), common, specific)
var nameWithSettings = Stream.of(List.of(DB), common, specific)
.flatMap(Collection::stream)
.collect(joining(";"));
return new EmbeddedDatabaseBuilder()
Expand All @@ -49,16 +49,16 @@ public DataSource dataSource(StorageProps storageProps) {
}

@Bean
public SepulkaRecMapper sepulkaRecMapper() {
return new SepulkaRecMapperImpl();
public SepulkaStateMapper sepulkaStateMapper() {
return new SepulkaStateMapperImpl();
}

@Configuration(proxyBeanMethods = false)
public static class SpringDataPostgres {

@Bean
public SepulkaDao underTest(SepulkaRecMapper recMapper, SepulkaRepository repository) {
return new SepulkaDaoSpringData(recMapper, repository);
public SepulkaDao underTest(SepulkaStateMapper mapper, SepulkaRepository repository) {
return new SepulkaDaoSpringData(mapper, repository);
}

@Bean
Expand All @@ -77,8 +77,8 @@ public StorageProps storageProps() {
public static class MyBatisPostgres {

@Bean
public SepulkaDao underTest(SepulkaRecMapper recMapper, SepulkaSqlMapper sqlMapper) {
return new SepulkaDaoMyBatis(recMapper, sqlMapper);
public SepulkaDao underTest(SepulkaStateMapper stateMapper, SepulkaSqlMapper sqlMapper) {
return new SepulkaDaoMyBatis(stateMapper, sqlMapper);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static smecalculus.bezmen.core.SepulkaNewResponseEg.Pojos.sepulkaNewResponse;
import static smecalculus.bezmen.messaging.SepulkaNewRequestEg.Pojos.sepulkaNewRequestMsg;
import static smecalculus.bezmen.messaging.SepulkaNewResponseEg.Pojos.sepulkaNewResponseMsg;
import static smecalculus.bezmen.messaging.SepulkaNewRequestEg.Pojos.sepulkaNewRequestEdge;
import static smecalculus.bezmen.messaging.SepulkaNewResponseEg.Pojos.sepulkaNewResponseEdge;

import java.util.UUID;
import org.junit.jupiter.api.Test;
Expand All @@ -32,11 +32,11 @@ void shouldRegisterSepulka() {
// given
var id = UUID.randomUUID();
// and
var request = sepulkaNewRequestMsg();
var request = sepulkaNewRequestEdge();
// and
when(serviceMock.register(any(SepulkaNewRequest.class))).thenReturn(sepulkaNewResponse(id));
// and
var expectedResponse = sepulkaNewResponseMsg(id);
var expectedResponse = sepulkaNewResponseEdge(id);
// when
var actualResponse = externalClient.register(request);
// then
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package smecalculus.bezmen.messaging;

public class SepulkaClientSpringWebTestIT extends SepulkaClientIT {}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import smecalculus.bezmen.construction.SepulkaDaoBeans;
import smecalculus.bezmen.core.Sepulka;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = SepulkaDaoBeans.class)
Expand All @@ -23,11 +22,11 @@ abstract class SepulkaDaoIT {
@Test
void shouldSaveOneSepulka() {
// given
Sepulka expectedSepulka = sepulka();
var expectedSepulka = sepulka();
// when
Sepulka actualSepulka1 = sepulkaDao.save(expectedSepulka);
var actualSepulka1 = sepulkaDao.save(expectedSepulka);
// and
Sepulka actualSepulka2 = sepulkaDao.getById(expectedSepulka.id());
var actualSepulka2 = sepulkaDao.getById(expectedSepulka.id());
// then
assertThat(actualSepulka1).isEqualTo(expectedSepulka);
// and
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package smecalculus.bezmen.messaging;

public interface SepulkaClient {
SepulkaNewResponseMsg register(SepulkaNewRequestMsg request);
SepulkaNewResponseEdge register(SepulkaNewRequestEdge request);
}
Loading

0 comments on commit 22e164d

Please sign in to comment.