11package fi .helsinki .cs .tmc .cli ;
22
33import static org .junit .Assert .assertEquals ;
4- import static org .junit .Assert .assertNotSame ;
4+ import static org .junit .Assert .assertFalse ;
55import static org .junit .Assert .assertNull ;
66import static org .junit .Assert .assertTrue ;
7+ import static org .mockito .Matchers .anyString ;
8+ import static org .mockito .Matchers .eq ;
9+ import static org .mockito .Mockito .mock ;
10+ import static org .mockito .Mockito .when ;
11+ import static org .powermock .api .mockito .PowerMockito .mockStatic ;
712
8- import fi .helsinki .cs .tmc .cli .io .Io ;
913import fi .helsinki .cs .tmc .cli .io .TerminalIo ;
1014import fi .helsinki .cs .tmc .cli .io .TestIo ;
15+ import fi .helsinki .cs .tmc .cli .tmcstuff .CourseInfo ;
16+ import fi .helsinki .cs .tmc .cli .tmcstuff .CourseInfoIo ;
17+ import fi .helsinki .cs .tmc .cli .tmcstuff .Settings ;
18+ import fi .helsinki .cs .tmc .cli .tmcstuff .SettingsIo ;
1119import fi .helsinki .cs .tmc .cli .tmcstuff .WorkDir ;
1220
1321import org .junit .Before ;
14- import org .junit .Ignore ;
1522import org .junit .Test ;
23+ import org .junit .runner .RunWith ;
24+ import org .powermock .core .classloader .annotations .PrepareForTest ;
25+ import org .powermock .modules .junit4 .PowerMockRunner ;
1626
27+ import java .nio .file .Path ;
28+
29+ @ RunWith (PowerMockRunner .class )
30+ @ PrepareForTest ({CourseInfoIo .class , SettingsIo .class })
1731public class CliContextTest {
1832
1933 private TestIo io ;
@@ -25,7 +39,6 @@ public void setUp() {
2539
2640 @ Test
2741 public void getIoAfterItsSetInConstructor () {
28- Io io = new TestIo ();
2942 CliContext ctx = new CliContext (io );
3043 assertEquals (io , ctx .getIo ());
3144 }
@@ -57,36 +70,108 @@ public void setGetWorkDir() {
5770 assertEquals (workDir , ctx .getWorkDir ());
5871 }
5972
60-
61- @ Ignore
62- @ Test
6373 public void backendInitWithoutCourse () {
64- CliContext ctx = new CliContext (null );
74+ mockStatic (CourseInfoIo .class );
75+
76+ CourseInfo info = null ;
77+ WorkDir workDir = mock (WorkDir .class );
78+
79+ when (workDir .getConfigFile ()).thenReturn (null );
80+ CliContext ctx = new CliContext (io , null , workDir );
81+
6582 assertTrue (ctx .loadBackend ());
6683 assertEquals (null , ctx .getCourseInfo ());
6784 }
6885
69- @ Ignore
70- @ Test
7186 public void backendInitWithCourse () {
72- CliContext ctx = new CliContext (null );
87+ mockStatic (CourseInfoIo .class );
88+
89+ CourseInfo info = mock (CourseInfo .class );
90+ WorkDir workDir = mock (WorkDir .class );
91+ Path path = mock (Path .class );
92+
93+ when (CourseInfoIo .load (eq (path ))).thenReturn (info );
94+ when (workDir .getConfigFile ()).thenReturn (path );
95+ CliContext ctx = new CliContext (io , null , workDir );
96+
7397 assertTrue (ctx .loadBackend ());
74- assertNotSame ( null , ctx .getCourseInfo ());
98+ assertEquals ( info , ctx .getCourseInfo ());
7599 }
76100
77- @ Ignore
78- @ Test
79101 public void backendInitWithInternet () {
80- CliContext ctx = new CliContext (null );
102+ mockStatic (CourseInfoIo .class );
103+ mockStatic (SettingsIo .class );
104+
105+ CourseInfo info = mock (CourseInfo .class );
106+ WorkDir workDir = mock (WorkDir .class );
107+ Path path = mock (Path .class );
108+
109+ when (CourseInfoIo .load (eq (path ))).thenReturn (info );
110+ when (SettingsIo .load (anyString (), anyString ())).thenReturn (new Settings ());
111+ when (workDir .getConfigFile ()).thenReturn (path );
112+ CliContext ctx = new CliContext (io , null , workDir );
113+
81114 assertTrue (ctx .loadBackend ());
82115 assertEquals (true , ctx .hasLogin ());
83116 }
84117
85- @ Ignore
86- @ Test
118+ public void failBackendInitWithInternetButWithoutCourse () {
119+ mockStatic (CourseInfoIo .class );
120+ mockStatic (SettingsIo .class );
121+
122+ WorkDir workDir = mock (WorkDir .class );
123+ when (workDir .getConfigFile ()).thenReturn (null );
124+ CliContext ctx = new CliContext (io , null , workDir );
125+
126+ assertFalse (ctx .loadBackend ());
127+ assertEquals (false , ctx .hasLogin ());
128+ io .assertContains ("You are not logged in" );
129+ }
130+
131+ public void failBackendInitWithInternetButWithCorruptedCourse () {
132+ mockStatic (CourseInfoIo .class );
133+ mockStatic (SettingsIo .class );
134+
135+ WorkDir workDir = mock (WorkDir .class );
136+ Path path = mock (Path .class );
137+
138+ when (CourseInfoIo .load (eq (path ))).thenReturn (null );
139+ when (workDir .getConfigFile ()).thenReturn (path );
140+ CliContext ctx = new CliContext (io , null , workDir );
141+
142+ assertFalse (ctx .loadBackend ());
143+ assertEquals (false , ctx .hasLogin ());
144+ io .assertContains ("Course configuration file" );
145+ io .assertContains ("is invalid." );
146+ io .assertContains ("You are not logged in" );
147+ }
148+
87149 public void backendInitWithoutInternet () {
88- CliContext ctx = new CliContext (null );
150+ mockStatic (SettingsIo .class );
151+
152+ WorkDir workDir = mock (WorkDir .class );
153+ when (workDir .getConfigFile ()).thenReturn (null );
154+ when (SettingsIo .load ()).thenReturn (null );
155+ CliContext ctx = new CliContext (io , null , workDir );
156+
89157 assertTrue (ctx .loadBackendWithoutLogin ());
90158 assertEquals (false , ctx .hasLogin ());
91159 }
160+
161+ public void backendInitWithoutInternetWithCourse () {
162+ mockStatic (CourseInfoIo .class );
163+ mockStatic (SettingsIo .class );
164+
165+ WorkDir workDir = mock (WorkDir .class );
166+ CourseInfo info = mock (CourseInfo .class );
167+ Path path = mock (Path .class );
168+
169+ when (CourseInfoIo .load (eq (path ))).thenReturn (info );
170+ when (SettingsIo .load (anyString (), anyString ())).thenReturn (new Settings ());
171+ when (workDir .getConfigFile ()).thenReturn (path );
172+ CliContext ctx = new CliContext (io , null , workDir );
173+
174+ assertTrue (ctx .loadBackendWithoutLogin ());
175+ assertEquals (true , ctx .hasLogin ());
176+ }
92177}
0 commit comments