-
Notifications
You must be signed in to change notification settings - Fork 35
YK Myhomework #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: homework
Are you sure you want to change the base?
YK Myhomework #12
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,115 +1,132 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>com.dmdev</groupId> | ||
| <artifactId>junit5-trainer</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <groupId>com.dmdev</groupId> | ||
| <artifactId>junit5-trainer</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
|
|
||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <maven.compiler.source>17</maven.compiler.source> | ||
| <maven.compiler.target>17</maven.compiler.target> | ||
| <lombok.version>1.18.22</lombok.version> | ||
| </properties> | ||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <maven.compiler.source>17</maven.compiler.source> | ||
| <maven.compiler.target>17</maven.compiler.target> | ||
| <lombok.version>1.18.22</lombok.version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.apache.commons</groupId> | ||
| <artifactId>commons-lang3</artifactId> | ||
| <version>3.12.0</version> | ||
| </dependency> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.apache.commons</groupId> | ||
| <artifactId>commons-lang3</artifactId> | ||
| <version>3.12.0</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.postgresql</groupId> | ||
| <artifactId>postgresql</artifactId> | ||
| <version>42.3.3</version> | ||
| <scope>runtime</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.postgresql</groupId> | ||
| <artifactId>postgresql</artifactId> | ||
| <version>42.7.2</version> | ||
| <scope>runtime</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.projectlombok</groupId> | ||
| <artifactId>lombok</artifactId> | ||
| <version>${lombok.version}</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.projectlombok</groupId> | ||
| <artifactId>lombok</artifactId> | ||
| <version>${lombok.version}</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-engine</artifactId> | ||
| <version>5.8.2</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.h2database</groupId> | ||
| <artifactId>h2</artifactId> | ||
| <version>2.1.210</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-engine</artifactId> | ||
| <version>5.11.4</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-params</artifactId> | ||
| <version>5.11.3</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.assertj</groupId> | ||
| <artifactId>assertj-core</artifactId> | ||
| <version>3.26.3</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.mockito</groupId> | ||
| <artifactId>mockito-junit-jupiter</artifactId> | ||
| <version>5.14.0</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.h2database</groupId> | ||
| <artifactId>h2</artifactId> | ||
| <version>2.2.220</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-wrapper-plugin</artifactId> | ||
| <version>3.1.0</version> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-failsafe-plugin</artifactId> | ||
| <version>3.0.0-M5</version> | ||
| <executions> | ||
| <execution> | ||
| <goals> | ||
| <goal>integration-test</goal> | ||
| <goal>verify</goal> | ||
| </goals> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <version>3.10.1</version> | ||
| <configuration> | ||
| <annotationProcessorPaths> | ||
| <path> | ||
| <groupId>org.projectlombok</groupId> | ||
| <artifactId>lombok</artifactId> | ||
| <version>${lombok.version}</version> | ||
| </path> | ||
| </annotationProcessorPaths> | ||
| </configuration> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <version>3.0.0-M5</version> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.jacoco</groupId> | ||
| <artifactId>jacoco-maven-plugin</artifactId> | ||
| <version>0.8.7</version> | ||
| <executions> | ||
| <execution> | ||
| <id>prepare-agent</id> | ||
| <goals> | ||
| <goal>prepare-agent</goal> | ||
| </goals> | ||
| </execution> | ||
| <execution> | ||
| <id>generate-jacoco-report</id> | ||
| <goals> | ||
| <goal>report</goal> | ||
| </goals> | ||
| <phase>verify</phase> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-wrapper-plugin</artifactId> | ||
| <version>3.1.0</version> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-failsafe-plugin</artifactId> | ||
| <version>3.0.0-M5</version> | ||
| <executions> | ||
| <execution> | ||
| <goals> | ||
| <goal>integration-test</goal> | ||
| <goal>verify</goal> | ||
| </goals> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <version>3.10.1</version> | ||
| <configuration> | ||
| <annotationProcessorPaths> | ||
| <path> | ||
| <groupId>org.projectlombok</groupId> | ||
| <artifactId>lombok</artifactId> | ||
| <version>${lombok.version}</version> | ||
| </path> | ||
| </annotationProcessorPaths> | ||
| </configuration> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <version>3.0.0-M5</version> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.jacoco</groupId> | ||
| <artifactId>jacoco-maven-plugin</artifactId> | ||
| <version>0.8.7</version> | ||
| <executions> | ||
| <execution> | ||
| <id>prepare-agent</id> | ||
| <goals> | ||
| <goal>prepare-agent</goal> | ||
| </goals> | ||
| </execution> | ||
| <execution> | ||
| <id>generate-jacoco-report</id> | ||
| <goals> | ||
| <goal>report</goal> | ||
| </goals> | ||
| <phase>verify</phase> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| </project> | ||
| </project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package com.dmdev.dao; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import com.dmdev.entity.Provider; | ||
| import com.dmdev.entity.Status; | ||
| import com.dmdev.entity.Subscription; | ||
| import com.dmdev.integration.IntegrationTestBase; | ||
| import java.time.Instant; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class SubscriptionDaoIT extends IntegrationTestBase { | ||
|
|
||
| private final SubscriptionDao subscriptionDao = SubscriptionDao.getInstance(); | ||
|
|
||
| @Test | ||
| void findAll() { | ||
| var subscriptionFirst = subscriptionDao.insert(getSubscription("first")); | ||
| var subscriptionSecond = subscriptionDao.insert(getSubscription("second ")); | ||
| var subscriptionThird = subscriptionDao.insert(getSubscription("third")); | ||
|
|
||
| List<Subscription> result = subscriptionDao.findAll(); | ||
|
|
||
| assertEquals(List.of(subscriptionFirst, subscriptionSecond, subscriptionThird), result); | ||
|
|
||
| List<Integer> ids = result.stream() | ||
| .map(Subscription::getId) | ||
| .toList(); | ||
| assertThat(ids).contains(subscriptionFirst.getId(), subscriptionSecond.getId(), | ||
| subscriptionThird.getId()); | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Разделяй четко 3 секции в тесте, чтобы улучшить его читабильность: given/when/then |
||
|
|
||
|
|
||
| @Test | ||
| void findByIdWhenIdExistsThenReturnSubscription() { | ||
| var subscriptionFirst = subscriptionDao.insert(getSubscription("first")); | ||
|
|
||
| var result = subscriptionDao.findById(subscriptionFirst.getId()); | ||
|
|
||
| assertNotNull(result); | ||
| assertEquals(Optional.of(subscriptionFirst), result); | ||
| } | ||
|
|
||
| @Test | ||
| void findByIdWhenIdNotExistsThenReturnOptionalEmpty() { | ||
| var result = subscriptionDao.findById(3212); | ||
|
|
||
| assertEquals(Optional.empty(), result); | ||
| } | ||
|
|
||
| @Test | ||
| void deleteWhenIdExistsThenReturnTrue() { | ||
| var subscriptionFirst = subscriptionDao.insert(getSubscription("first")); | ||
|
|
||
| var result = subscriptionDao.delete(subscriptionFirst.getId()); | ||
|
|
||
| assertTrue(result); | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не надо делать отступов в конце метода |
||
| } | ||
|
|
||
| @Test | ||
| void deleteWhenIdNotExistsThenReturnFalse() { | ||
| subscriptionDao.insert(getSubscription("first")); | ||
|
|
||
| var result = subscriptionDao.delete(1234); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. нужно выбирать более нереалистичное значение id |
||
|
|
||
| assertFalse(result); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| void update() { | ||
| var subscription = getSubscription("first"); | ||
| subscriptionDao.insert(subscription); | ||
| subscription.setName("second"); | ||
| subscription.setProvider(Provider.GOOGLE); | ||
|
|
||
| var result = subscriptionDao.update(subscription); | ||
|
|
||
| assertNotNull(result); | ||
| assertEquals(subscription, result); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| void insert() { | ||
| var subscription = getSubscription("first"); | ||
|
|
||
| var result = subscriptionDao.insert(subscription); | ||
|
|
||
| assertNotNull(result); | ||
| } | ||
|
|
||
| @Test | ||
| void findByUserIdWhenIdExistsThenReturnSubscriptions() { | ||
| var userid = 123; | ||
| var subscriptionFirst = subscriptionDao.insert(getSubscription("first").setUserId(userid)); | ||
| var subscriptionSecond = subscriptionDao.insert(getSubscription("second").setUserId(userid)); | ||
|
|
||
| List<Subscription> result = subscriptionDao.findByUserId(userid); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Для теста метода findByUserId нужна вставить несколько подписок с разным userId - иначе некорректный dataset. Его можно заменить на findAll, например, и он все еще будет проходить |
||
|
|
||
| assertNotNull(result); | ||
| assertEquals(2, result.size()); | ||
|
|
||
| List<Integer> ids = result.stream() | ||
| .map(Subscription::getId) | ||
| .toList(); | ||
| assertThat(ids).contains(subscriptionFirst.getId(), subscriptionSecond.getId()); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| void findByUserIdWhenIdNotExistsThenReturnEmptyList() { | ||
| var userid = 123; | ||
| subscriptionDao.insert(getSubscription("first").setUserId(userid)); | ||
| subscriptionDao.insert(getSubscription("second").setUserId(userid)); | ||
|
|
||
| var result = subscriptionDao.findByUserId(3212); | ||
|
|
||
| assertEquals(Collections.EMPTY_LIST, result); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. проверяется на empty list, а не сравнивается с константой. Ибо это может быть и не константа вернута тебе |
||
| } | ||
|
|
||
|
|
||
| private Subscription getSubscription(String name) { | ||
| return Subscription.builder() | ||
| .userId(123) | ||
| .name(name) | ||
| .provider(Provider.APPLE) | ||
| .status(Status.ACTIVE) | ||
| .expirationDate(Instant.parse("2025-11-12T10:00:00Z")) | ||
| .build(); | ||
| } | ||
|
|
||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
нет смысла в этом ассерте, когда есть assertEquals такой чуть выше