1+ package input ;
2+
3+ import dto .Client ;
4+ import input .strategy .ClientInputStrategy ;
5+ import input .strategy .FileReaderStrategy ;
6+ import input .strategy .ManualInputReaderStrategy ;
7+ import input .strategy .RandomDataGeneratorStrategy ;
8+ import org .junit .jupiter .api .BeforeEach ;
9+ import org .junit .jupiter .api .DisplayName ;
10+ import org .junit .jupiter .api .Test ;
11+
12+ import java .io .IOException ;
13+
14+ import static org .junit .jupiter .api .Assertions .*;
15+
16+ class InputManagerTest {
17+
18+ private InputManager inputManager ;
19+
20+ @ BeforeEach
21+ void setUp () {
22+ inputManager = new InputManager ();
23+ }
24+
25+ @ Test
26+ @ DisplayName ("Конструктор без стратегии должен создавать InputManager с null-стратегией" )
27+ void testConstructorWithoutStrategy () {
28+ assertNull (inputManager .getCurrentStrategy ());
29+ }
30+
31+ @ Test
32+ @ DisplayName ("Конструктор с null-стратегией должен выбрасывать IllegalArgumentException" )
33+ void testConstructorWithNullStrategyThrowsException () {
34+ assertThrows (IllegalArgumentException .class , () -> new InputManager (null ));
35+ }
36+
37+ @ Test
38+ @ DisplayName ("Конструктор с валидной стратегией должен устанавливать её как текущую" )
39+ void testConstructorWithValidStrategy () {
40+ ClientInputStrategy strategy = new TestClientInputStrategy ();
41+ InputManager manager = new InputManager (strategy );
42+ assertEquals (strategy , manager .getCurrentStrategy ());
43+ }
44+
45+ @ Test
46+ @ DisplayName ("setStrategy с null должен выбрасывать IllegalArgumentException" )
47+ void testSetStrategyWithNullThrowsException () {
48+ assertThrows (IllegalArgumentException .class , () -> inputManager .setStrategy (null ));
49+ }
50+
51+ @ Test
52+ @ DisplayName ("setStrategy должен корректно устанавливать новую стратегию" )
53+ void testSetStrategy () {
54+ ClientInputStrategy strategy = new TestClientInputStrategy ();
55+ inputManager .setStrategy (strategy );
56+ assertEquals (strategy , inputManager .getCurrentStrategy ());
57+ }
58+
59+ @ Test
60+ @ DisplayName ("loadData без установленной стратегии должен выбрасывать IllegalStateException" )
61+ void testLoadDataWithoutStrategyThrowsException () throws IOException {
62+ assertThrows (IllegalStateException .class , inputManager ::loadData );
63+ }
64+
65+ @ Test
66+ @ DisplayName ("loadData должен возвращать данные от текущей стратегии" )
67+ void testLoadDataReturnsDataFromStrategy () throws IOException {
68+ ClientInputStrategy strategy = new TestClientInputStrategy ();
69+ inputManager .setStrategy (strategy );
70+
71+ CustomCollection <Client > result = inputManager .loadData ();
72+
73+ assertNotNull (result );
74+ assertEquals (1 , result .size ()); // Ожидаем 1 клиента от тестовой стратегии
75+ }
76+
77+ @ Test
78+ @ DisplayName ("loadData должен пробрасывать IOException от стратегии" )
79+ void testLoadDataPropagatesIOException () throws IOException {
80+ ClientInputStrategy strategy = new ClientInputStrategy () {
81+ @ Override
82+ public CustomCollection <Client > getData () throws IOException {
83+ throw new IOException ("Ошибка ввода-вывода" );
84+ }
85+ };
86+ inputManager .setStrategy (strategy );
87+
88+ assertThrows (IOException .class , inputManager ::loadData );
89+ }
90+
91+ @ Test
92+ @ DisplayName ("createFileStrategy с null-путём должен выбрасывать IllegalArgumentException" )
93+ void testCreateFileStrategyWithNullPathThrowsException () {
94+ assertThrows (IllegalArgumentException .class ,
95+ () -> new FileReaderStrategy (null ));
96+ }
97+
98+ @ Test
99+ @ DisplayName ("createFileStrategy с пустым путём должен выбрасывать IllegalArgumentException" )
100+ void testCreateFileStrategyWithEmptyPathThrowsException () {
101+ assertThrows (IllegalArgumentException .class ,
102+ () -> new FileReaderStrategy ("" ));
103+ }
104+
105+ @ Test
106+ @ DisplayName ("createFileStrategy должен создавать FileReaderStrategy с указанным путём" )
107+ void testCreateFileStrategy () {
108+ String filePath = "test.txt" ;
109+ FileReaderStrategy strategy = new FileReaderStrategy (filePath );
110+
111+ assertNotNull (strategy );
112+ // Если у FileReaderStrategy есть геттер для пути:
113+ // assertEquals(filePath, strategy.getFilePath());
114+ }
115+
116+ @ Test
117+ @ DisplayName ("createManualStrategy должен создавать ManualInputReaderStrategy" )
118+ void testCreateManualStrategy () {
119+ ManualInputReaderStrategy strategy = new ManualInputReaderStrategy ();
120+
121+ assertNotNull (strategy );
122+ }
123+
124+ @ Test
125+ @ DisplayName ("createRandomStrategy с отрицательным count должен выбрасывать IllegalArgumentException" )
126+ void testCreateRandomStrategyWithNegativeCountThrowsException () {
127+ assertThrows (IllegalArgumentException .class ,
128+ () -> new RandomDataGeneratorStrategy (-1 ));
129+ }
130+
131+ @ Test
132+ @ DisplayName ("createRandomStrategy с нулевым count должен выбрасывать IllegalArgumentException" )
133+ void testCreateRandomStrategyWithZeroCountThrowsException () {
134+ assertThrows (IllegalArgumentException .class ,
135+ () -> new RandomDataGeneratorStrategy (0 ));
136+ }
137+
138+ @ Test
139+ @ DisplayName ("createRandomStrategy должен создавать RandomDataGeneratorStrategy с указанным количеством" )
140+ void testCreateRandomStrategy () {
141+ int count = 5 ;
142+ RandomDataGeneratorStrategy strategy =new RandomDataGeneratorStrategy (count );
143+
144+ assertNotNull (strategy );
145+ // Если у RandomDataGeneratorStrategy есть геттер для count:
146+ // assertEquals(count, strategy.getCount());
147+ }
148+ }
149+
150+ // Тестовая реализация ClientInputStrategy для использования в тестах
151+ class TestClientInputStrategy implements ClientInputStrategy {
152+ @ Override
153+ public CustomCollection <Client > getData () throws IOException {
154+ CustomCollection <Client > clients = new CustomCollection <>();
155+ clients .add (new Client .ClientBuilder ().name ("Test Client" )
156+ .phoneNumber ("+79991234567" ).idNumber (1 ).build ());
157+
158+ // Предполагаем, что CustomCollection имеет конструктор от Collection<Client>
159+ return clients ;
160+ }
161+ }
0 commit comments