1+ package com .objectcomputing .checkins .services .skill_record ;
2+
3+ import io .micronaut .test .extensions .junit5 .annotation .MicronautTest ;
4+ import org .apache .commons .csv .CSVFormat ;
5+ import org .apache .commons .csv .CSVParser ;
6+ import org .apache .commons .csv .CSVRecord ;
7+ import org .junit .jupiter .api .BeforeAll ;
8+ import org .junit .jupiter .api .BeforeEach ;
9+ import org .junit .jupiter .api .Test ;
10+ import org .junit .jupiter .api .TestInstance ;
11+ import org .mockito .InjectMocks ;
12+ import org .mockito .Mock ;
13+ import org .mockito .Mockito ;
14+ import org .mockito .MockitoAnnotations ;
15+
16+ import java .io .File ;
17+ import java .io .FileReader ;
18+ import java .io .IOException ;
19+ import java .io .Reader ;
20+ import java .util .Collections ;
21+ import java .util .List ;
22+
23+ import static org .junit .jupiter .api .Assertions .*;
24+ import static org .mockito .Mockito .*;
25+
26+ @ MicronautTest
27+ @ TestInstance (TestInstance .Lifecycle .PER_CLASS )
28+ class SkillRecordServicesImplTest {
29+
30+ @ Mock
31+ private SkillRecordRepository skillRecordRepository ;
32+
33+ @ InjectMocks
34+ private SkillRecordServicesImpl skillRecordServices ;
35+
36+ @ BeforeAll
37+ void initMocks () {
38+ MockitoAnnotations .openMocks (this );
39+ }
40+
41+ @ BeforeEach
42+ void resetMocks () {
43+ Mockito .reset (skillRecordRepository );
44+ }
45+
46+ @ Test
47+ void testFileGeneration () throws IOException {
48+ SkillRecord record1 = new SkillRecord ();
49+ record1 .setName ("Java" );
50+ record1 .setDescription ("Various technical skills" );
51+ record1 .setExtraneous (true );
52+ record1 .setPending (true );
53+ record1 .setCategoryName ("Languages, Libraries, and Frameworks" );
54+
55+ when (skillRecordRepository .findAll ()).thenReturn (Collections .singletonList (record1 ));
56+
57+ File file = skillRecordServices .generateFile ();
58+ assertNotNull (file );
59+
60+ Reader fileReader = new FileReader (file );
61+
62+ String [] headers = { "name" , "description" , "extraneous" , "pending" , "category_name" };
63+ CSVFormat csvFormat = CSVFormat .DEFAULT
64+ .withHeader (headers )
65+ .withQuote ('"' )
66+ .withSkipHeaderRecord ();
67+
68+ CSVParser parser = csvFormat .parse (fileReader );
69+ List <CSVRecord > records = parser .getRecords ();
70+
71+ assertEquals (1 , records .size ());
72+ CSVRecord csvRecord = records .get (0 );
73+ assertEquals (record1 .getName (), csvRecord .get ("name" ));
74+ assertEquals (record1 .getDescription (), csvRecord .get ("description" ));
75+ assertEquals (record1 .isExtraneous (), Boolean .valueOf (csvRecord .get ("extraneous" )));
76+ assertEquals (record1 .isPending (), Boolean .valueOf (csvRecord .get ("pending" )));
77+ assertEquals (record1 .getCategoryName (), csvRecord .get ("category_name" ));
78+ }
79+
80+ }
0 commit comments