From 758e2df3a4fefaaf88ed2d54af5ea75221367a05 Mon Sep 17 00:00:00 2001
From: Yaroslav <6i4bbb@gmail.com>
Date: Sat, 21 Dec 2024 18:29:27 +0200
Subject: [PATCH] Add & Delete Testing
---
.idea/.gitignore | 3 +
.idea/encodings.xml | 7 ++
.idea/google-java-format.xml | 6 ++
.idea/misc.xml | 12 +++
.idea/vcs.xml | 6 ++
pom.xml | 8 ++
.../gnew/m2/cli/commands/AddContact.java | 6 +-
.../gnew/m2/cli/commands/AddContactTest.java | 81 +++++++++++++++++++
.../m2/cli/commands/DeleteContactTest.java | 73 +++++++++++++++++
9 files changed, 201 insertions(+), 1 deletion(-)
create mode 100644 .idea/.gitignore
create mode 100644 .idea/encodings.xml
create mode 100644 .idea/google-java-format.xml
create mode 100644 .idea/misc.xml
create mode 100644 .idea/vcs.xml
create mode 100644 src/test/java/ua/com/javarush/gnew/m2/cli/commands/AddContactTest.java
create mode 100644 src/test/java/ua/com/javarush/gnew/m2/cli/commands/DeleteContactTest.java
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..26d3352
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,3 @@
+# Default ignored files
+/shelf/
+/workspace.xml
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
new file mode 100644
index 0000000..aa00ffa
--- /dev/null
+++ b/.idea/encodings.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/google-java-format.xml b/.idea/google-java-format.xml
new file mode 100644
index 0000000..8b57f45
--- /dev/null
+++ b/.idea/google-java-format.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..9e0563e
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 4a1c083..58eb613 100644
--- a/pom.xml
+++ b/pom.xml
@@ -53,6 +53,14 @@
test
+
+
+ org.mockito
+ mockito-junit-jupiter
+ 5.14.2
+ test
+
+
org.mockito
mockito-core
diff --git a/src/main/java/ua/com/javarush/gnew/m2/cli/commands/AddContact.java b/src/main/java/ua/com/javarush/gnew/m2/cli/commands/AddContact.java
index fd53757..4b2ca36 100644
--- a/src/main/java/ua/com/javarush/gnew/m2/cli/commands/AddContact.java
+++ b/src/main/java/ua/com/javarush/gnew/m2/cli/commands/AddContact.java
@@ -16,9 +16,13 @@
aliases = {"-a", "--add"},
description = "Додає новий контакт до телефонної книги",
mixinStandardHelpOptions = true)
+
public class AddContact implements CliCommand {
+
+
private final PhoneBookInterface phoneBookInterface =
- PhoneBookContext.getBean(PhoneBookInterface.class);
+ PhoneBookContext.getBean(PhoneBookInterface.class);
+
@Option(
names = {"-n", "--name"},
diff --git a/src/test/java/ua/com/javarush/gnew/m2/cli/commands/AddContactTest.java b/src/test/java/ua/com/javarush/gnew/m2/cli/commands/AddContactTest.java
new file mode 100644
index 0000000..b6b1659
--- /dev/null
+++ b/src/test/java/ua/com/javarush/gnew/m2/cli/commands/AddContactTest.java
@@ -0,0 +1,81 @@
+package ua.com.javarush.gnew.m2.cli.commands;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import picocli.CommandLine;
+import ua.com.javarush.gnew.m2.dto.ContactDto;
+import ua.com.javarush.gnew.m2.service.PhoneBookInterface;
+
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.*;
+
+class AddContactTest {
+ @Mock
+ private PhoneBookInterface phoneBookInterface;
+
+ @InjectMocks
+ private AddContact addContact;
+
+ @BeforeEach
+ void setUp() throws NoSuchFieldException, IllegalAccessException {
+
+ MockitoAnnotations.openMocks(this);
+
+ addContact = new AddContact();
+
+ Field phoneBookInterfaceField = AddContact.class.getDeclaredField("phoneBookInterface");
+ phoneBookInterfaceField.setAccessible(true);
+ phoneBookInterfaceField.set(addContact, phoneBookInterface);
+ }
+
+ @Test
+ void testCall_addContactSuccessfully() throws Exception {
+
+ ContactDto savedContact = new ContactDto("Иван Иванов", List.of("123456789"), List.of("ivan@example.com"), "ivanGitHub");
+ when(phoneBookInterface.add(any(ContactDto.class))).thenReturn(savedContact);
+
+ String[] args = {
+ "--name", "Иван Иванов",
+ "--phone", "123456789",
+ "--email", "ivan@example.com",
+ "--github", "ivanGitHub"
+ };
+ CommandLine.populateCommand(addContact, args);
+
+ Integer result = addContact.call();
+
+ verify(phoneBookInterface, times(1)).add(any(ContactDto.class));
+
+ assertEquals(0, result);
+ }
+ @Test
+ void testCall_withException() throws IOException {
+
+ when(phoneBookInterface.add(any(ContactDto.class))).thenThrow(new RuntimeException("Ошибка добавления"));
+
+ String[] args = {
+ "--name", "Иван Иванов",
+ "--phone", "123456789"
+ };
+ CommandLine.populateCommand(addContact, args);
+
+ RuntimeException thrownException = null;
+ try {
+ addContact.call();
+ } catch (RuntimeException e) {
+ thrownException = e;
+ }
+
+ assertEquals("java.lang.RuntimeException: Ошибка добавления", thrownException.getMessage());
+
+ verify(phoneBookInterface, times(1)).add(any(ContactDto.class));
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/ua/com/javarush/gnew/m2/cli/commands/DeleteContactTest.java b/src/test/java/ua/com/javarush/gnew/m2/cli/commands/DeleteContactTest.java
new file mode 100644
index 0000000..0ddf57d
--- /dev/null
+++ b/src/test/java/ua/com/javarush/gnew/m2/cli/commands/DeleteContactTest.java
@@ -0,0 +1,73 @@
+package ua.com.javarush.gnew.m2.cli.commands;
+
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import picocli.CommandLine;
+import ua.com.javarush.gnew.m2.service.PhoneBookInterface;
+import ua.com.javarush.gnew.m2.utils.Utils;
+
+import java.io.IOException;
+import java.util.List;
+
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.*;
+
+
+class DeleteContactTest {
+ @Mock
+ private PhoneBookInterface phoneBookInterface;
+
+ private DeleteContact deleteContact;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ MockitoAnnotations.openMocks(this);
+
+ deleteContact = new DeleteContact();
+ var field = DeleteContact.class.getDeclaredField("phoneBookInterface");
+ field.setAccessible(true);
+ field.set(deleteContact, phoneBookInterface);
+ }
+
+ @Test
+ void testCall_successfulDeletion() throws Exception {
+
+ doNothing().when(phoneBookInterface).delete(anyLong());
+ when(phoneBookInterface.list()).thenReturn(List.of());
+
+ String[] args = {"123", "456", "789"};
+ CommandLine.populateCommand(deleteContact, args);
+
+ Integer result = deleteContact.call();
+
+ verify(phoneBookInterface, times(1)).delete(123L);
+ verify(phoneBookInterface, times(1)).delete(456L);
+ verify(phoneBookInterface, times(1)).delete(789L);
+
+ verify(phoneBookInterface, times(1)).list();
+
+ assert result == 0;
+ }
+
+ @Test
+ void testCall_deletionWithException() throws Exception {
+
+ doNothing().when(phoneBookInterface).delete(123L);
+ doThrow(new IOException("Ошибка удаления")).when(phoneBookInterface).delete(456L);
+
+ String[] args = {"123", "456"};
+ CommandLine.populateCommand(deleteContact, args);
+
+ Integer result = deleteContact.call();
+
+ verify(phoneBookInterface, times(1)).delete(123L);
+ verify(phoneBookInterface, times(1)).delete(456L);
+
+ verify(phoneBookInterface, times(1)).list();
+
+ assert result == 0;
+ }
+}
\ No newline at end of file