Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ This is sample 'Motor depot' web application.
- - **Data Access:**
- [Spring JDBC](https://docs.spring.io/spring-framework/docs/5.3.x/reference/html/data-access.html#jdbc)
<a href="https://spring.io/projects/spring-boot" target="_blank" rel="noreferrer"> <img src="https://www.vectorlogo.zone/logos/springio/springio-icon.svg" alt="spring" width="18" height="18"/> </a>
- [Spring Data MongoDB](https://spring.io/projects/spring-data-mongodb)
<a href="https://spring.io/projects/spring-boot" target="_blank" rel="noreferrer"> <img src="https://www.vectorlogo.zone/logos/springio/springio-icon.svg" alt="spring" width="18" height="18"/> </a>
- **Build System:** [Maven](https://maven.apache.org/) <img height="20" width="20" src="https://raw.githubusercontent.com/vscode-icons/vscode-icons/master/icons/file_type_maven.svg"/>
- **Control System:** [Git](https://git-scm.com/) <a href="https://git-scm.com/" target="_blank" rel="noreferrer"> <img src="https://www.vectorlogo.zone/logos/git-scm/git-scm-icon.svg" alt="git" width="18" height="18"/> </a>
- **License:** [Apache license, version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
- **Automated Testing:**
- [JUnit5](https://junit.org/junit5/) <img height="20" width="20" src="https://unpkg.com/simple-icons@v6/icons/junit5.svg"/>
- [Mockito](http://site.mockito.org/) <img height="20" width="40" src="documentation/img/mokito.svg"/>
- [Mockito](http://site.mockito.org/) <img height="20" width="40" src="https://cdn.nearsoft.com/uploads/2017/11/annotation-magic-with-mockito-mock-and-spy-lede.png"/>
- **Log:** [Log4j 2](https://logging.apache.org/log4j/2.x/)
- **Database:**
- [H2](http://www.h2database.com/html/main.html) <img height="20" width="20" src="documentation/img/h2.svg"/>
- [H2](http://www.h2database.com/html/main.html) <img height="20" width="20" src="https://www.h2database.com/html/images/h2-logo-2.png"/>
- [PostgreSQL](https://www.postgresql.org/) <a href="https://www.postgresql.org" target="_blank" rel="noreferrer"> <img src="https://raw.githubusercontent.com/devicons/devicon/master/icons/postgresql/postgresql-original-wordmark.svg" alt="postgresql" width="20" height="20"/> </a>
- [MongoDB](https://www.mongodb.com/) <img height="20" width="20" src="https://www.opc-router.de/wp-content/uploads/2021/03/mongodb_thumbnail.png"/>
- **API documentation generation:**
- [Swagger UI](https://swagger.io/tools/swagger-ui/) <img height="20" width="20" src="https://raw.githubusercontent.com/AliasIO/wappalyzer/master/src/drivers/webextension/images/icons/Swagger%20UI.svg"/>
- **Code generation:**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.epam.brest.dao_api;

import com.epam.brest.model.ModelSpecification;

public interface ModelSpecificationDao {

/**
* Getting car's model specification by model name.
*
* @param carModel String;
* @return ModelSpecification.
*/

ModelSpecification getModelSpecificationByCarModel(String carModel);
}
16 changes: 9 additions & 7 deletions dao-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand All @@ -59,14 +65,10 @@
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<!--logger-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.epam.brest.dao;

import com.epam.brest.dao_api.ModelSpecificationDao;
import com.epam.brest.model.ModelSpecification;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import static com.epam.brest.dao.Queries.FIND_MODEL_SPECIFICATION_BY_CAR_MODEL;

@Repository
public class ModelSpecificationDaoImpl implements ModelSpecificationDao {

public static final Logger LOG = LogManager.getLogger(ModelSpecificationDaoImpl.class);

/**
* Field namedParameterJdbcTemplate.
*/

private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;

/**
* @Constructor
*
* @param namedParameterJdbcTemplate NamedParameterJdbcTemplate.
*/

public ModelSpecificationDaoImpl(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
}

/**
* Getting car's model specification by model name.
*
* @param carModel String;
* @return instance of ModelSpecification.
*/

@Override
public ModelSpecification getModelSpecificationByCarModel(String carModel) {
LOG.info("Method getModelSpecificationByCarModel() of class {} started",
getClass().getName());

SqlParameterSource sqlParameterSource =
new MapSqlParameterSource("modelName", carModel);
return namedParameterJdbcTemplate.queryForObject(FIND_MODEL_SPECIFICATION_BY_CAR_MODEL,
sqlParameterSource, BeanPropertyRowMapper.newInstance(ModelSpecification.class));
}
}
10 changes: 10 additions & 0 deletions dao-jdbc/src/main/java/com/epam/brest/dao/Queries.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,14 @@ public class Queries {

public static final String CARS_LIST_ASSIGN_TO_DRIVER =
"SELECT * FROM car c WHERE c.driver_id = :driverId";

/**
* Field constant FIND_MODEL_SPECIFICATION_BY_CAR_MODEL.
*/

public static final String FIND_MODEL_SPECIFICATION_BY_CAR_MODEL =
"SELECT ms.model_id AS modelId, ms.model_name AS modelName, ms.description AS description,"
+ " ms.max_speed AS maxSpeed, ms.carrying_capacity AS carryingCapacity"
+ " FROM model_specifications AS ms WHERE ms.model_name=:modelName";

}
48 changes: 43 additions & 5 deletions dao-jdbc/src/main/resources/log4j2.properties
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
status=warn
name=PropertiesConfig
appenders=console
status=warn, rolling, console
name=PropertiesConfig, RollingFileLogConfigDemo
appenders=console, rolling

appender.console.type=Console
appender.console.name=STDOUT
appender.console.layout.type=PatternLayout
appender.console.layout.pattern=%d{HH:mm:ss} %-5p %-20.20C{1} %m%n
log.test_app.name=com.epam.brest.dao
log.test_app.level=debug
log.test_app.level=info
log.test_app.appenderRef.stdout.ref=STDOUT
rootLogger.level=debug
rootLogger.appenderRef.stdout.ref=STDOUT

# Log files location
property.basePath =../

# RollingFileAppender name, pattern, path and rollover policy
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName=${basePath}/cacheLogs.log
appender.rolling.filePattern= ${basePath}/cache.log.gz
#appender.rolling.filePattern= ${basePath}/app_%d{yyyyMMdd}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] [%l] - %msg%n
appender.rolling.policies.type = Policies
appender.rolling.filter.threshold.type = ThresholdFilter
appender.rolling.filter.threshold.level = warn

# Configure root logger
rootLogger.level = info
rootLogger.appenderRef.rolling.ref = fileLogger

# RollingFileAppender rotation policy
#appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
#appender.rolling.policies.size.size = 10MB
#appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
#appender.rolling.policies.time.interval = 1
#appender.rolling.policies.time.modulate = true
#appender.rolling.strategy.type = DefaultRolloverStrategy
#appender.rolling.strategy.delete.type = Delete
#appender.rolling.strategy.delete.basePath = ${basePath}
#appender.rolling.strategy.delete.maxDepth = 10
#appender.rolling.strategy.delete.ifLastModified.type = IfLastModified

# Delete all files older than 30 days
#appender.rolling.strategy.delete.ifLastModified.age = 30d




Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.epam.brest.dao;

import com.epam.brest.model.ModelSpecification;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.*;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import static com.epam.brest.dao.Queries.FIND_MODEL_SPECIFICATION_BY_CAR_MODEL;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class ModelSpecificationDaoImplTest {

public static final Logger LOG = LogManager.getLogger(ModelSpecificationDaoImplTest.class);

@InjectMocks
private ModelSpecificationDaoImpl modelSpecificationDao;

@Mock
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

@Captor
private ArgumentCaptor<SqlParameterSource> sqlParameterSourceArgumentCaptor;

@Captor
private ArgumentCaptor<BeanPropertyRowMapper<ModelSpecification>> beanPropertyRowMapperArgumentCaptor;

private ModelSpecification modelSpecification;


@BeforeEach
void setUp() {
modelSpecification = new ModelSpecification(
"NISSAN", "Passenger car: made in Japan", 200, 1870);
}

@Test
void getModelSpecificationByCarModel() {
LOG.info("Method getModelSpecificationByCarModel() of class {} started",
getClass().getName());
assertNotNull(namedParameterJdbcTemplate);

lenient().when(namedParameterJdbcTemplate.queryForObject(anyString(),
ArgumentMatchers.<SqlParameterSource>any(),
any(BeanPropertyRowMapper.class))).thenReturn(modelSpecification);

ModelSpecification modelSpecificationDst = modelSpecificationDao.getModelSpecificationByCarModel(modelSpecification.getModelName());

verify(namedParameterJdbcTemplate).queryForObject(eq(FIND_MODEL_SPECIFICATION_BY_CAR_MODEL),
sqlParameterSourceArgumentCaptor.capture(), beanPropertyRowMapperArgumentCaptor.capture());

BeanPropertyRowMapper<ModelSpecification> rowMapper = beanPropertyRowMapperArgumentCaptor.getValue();
SqlParameterSource sqlParameterSource = sqlParameterSourceArgumentCaptor.getValue();

assertNotNull(modelSpecificationDst);
assertNotNull(rowMapper);
assertNotNull(sqlParameterSource);
assertEquals(modelSpecificationDst, modelSpecification);
LOG.info("ModelSpecification was received after getModelSpecificationByCarModel() {} equals modelSpecification till it {}",
modelSpecificationDst, modelSpecification);
}
}
Loading