23
23
import org .zowe .apiml .security .common .token .TokenAuthentication ;
24
24
25
25
import java .io .IOException ;
26
+ import java .nio .file .FileAlreadyExistsException ;
26
27
27
- import static org .junit .jupiter .api .Assertions .*;
28
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
29
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
28
30
29
31
@ ExtendWith (MockitoExtension .class )
30
32
@ MockitoSettings (strictness = Strictness .LENIENT )
@@ -33,42 +35,75 @@ class StaticDefinitionGeneratorTest {
33
35
@ InjectMocks
34
36
private StaticDefinitionGenerator staticDefinitionGenerator ;
35
37
38
+ String configFileLocation = "../config/local/api-defs" ;
39
+ String testServiceId = "test-static-def" ;
40
+
36
41
@ Nested
37
42
class WhenStaticDefinitionGenerationResponse {
38
43
39
44
@ BeforeEach
40
45
void setUp () {
41
- ReflectionTestUtils .setField (staticDefinitionGenerator ,"staticApiDefinitionsDirectories" ,"config/local/api-defs" );
46
+ TokenAuthentication authentication = new TokenAuthentication ("token" );
47
+ authentication .setAuthenticated (true );
48
+ SecurityContextHolder .setContext (new SecurityContextImpl (authentication ));
49
+ ReflectionTestUtils .setField (staticDefinitionGenerator , "staticApiDefinitionsDirectories" , configFileLocation );
42
50
}
43
51
44
52
@ Test
45
53
void givenRequestWithInvalidServiceId_thenThrow400 () throws IOException {
46
- TokenAuthentication authentication = new TokenAuthentication ("token" );
47
- authentication .setAuthenticated (true );
48
- SecurityContextHolder .setContext (new SecurityContextImpl (authentication ));
54
+
49
55
StaticAPIResponse actualResponse = staticDefinitionGenerator .generateFile ("services: \\ n " , "" );
50
56
StaticAPIResponse expectedResponse = new StaticAPIResponse (400 , "The service ID format is not valid." );
51
57
assertEquals (expectedResponse , actualResponse );
52
58
}
53
59
54
60
@ Test
55
- void givenValidRequest_thenThrowExceptionWithCorrectPath () {
56
- TokenAuthentication authentication = new TokenAuthentication ("token" );
57
- authentication .setAuthenticated (true );
58
- SecurityContextHolder .setContext (new SecurityContextImpl (authentication ));
59
- Exception exception = assertThrows (IOException .class , () ->
60
- staticDefinitionGenerator .generateFile ("services: \\ n serviceId: service\\ n " , "service" ));
61
- assertEquals ("./config/local/api-defs/service.yml (No such file or directory)" , exception .getMessage ());
61
+ void givenDeleteRequestWithInvalidServiceId_thenThrow400 () throws IOException {
62
+
63
+ StaticAPIResponse actualResponse = staticDefinitionGenerator .deleteFile ("" );
64
+ StaticAPIResponse expectedResponse = new StaticAPIResponse (400 , "The service ID format is not valid." );
65
+ assertEquals (expectedResponse , actualResponse );
62
66
}
63
67
64
68
@ Test
65
- void givenHttpValidRequest_thenThrowExceptionWithCorrectPath () {
66
- TokenAuthentication authentication = new TokenAuthentication ("token" );
67
- authentication .setAuthenticated (true );
68
- SecurityContextHolder .setContext (new SecurityContextImpl (authentication ));
69
- Exception exception = assertThrows (IOException .class , () ->
70
- staticDefinitionGenerator .generateFile ("services: \\ n serviceId: service\\ n " , "service" ));
71
- assertEquals ("./config/local/api-defs/service.yml (No such file or directory)" , exception .getMessage ());
69
+ void givenCreateRequestWithValidServiceId_thenStatusOK () throws IOException {
70
+ StaticAPIResponse actualResponse = staticDefinitionGenerator .generateFile ("services: \\ n " , testServiceId );
71
+ try {
72
+ assertEquals (201 , actualResponse .getStatusCode ());
73
+ } finally {
74
+ // cleanup
75
+ staticDefinitionGenerator .deleteFile (testServiceId );
76
+ }
77
+
78
+ }
79
+
80
+ @ Test
81
+ void givenDeleteRequestWithValidServiceId_thenStatusOK () throws IOException {
82
+ //create file before deletion
83
+ staticDefinitionGenerator .generateFile ("services: \\ n " , testServiceId );
84
+ StaticAPIResponse actualResponse = staticDefinitionGenerator .deleteFile (testServiceId );
85
+ StaticAPIResponse expectedResponse = new StaticAPIResponse (200 , "The static definition file %s has been deleted by the user!" );
86
+ assertEquals (expectedResponse , actualResponse );
87
+ }
88
+
89
+ @ Test
90
+ void givenDeleteNonExistingFileRequest_thenStatusNotFound () throws IOException {
91
+ //create file before deletion
92
+ StaticAPIResponse actualResponse = staticDefinitionGenerator .deleteFile (testServiceId );
93
+ StaticAPIResponse expectedResponse = new StaticAPIResponse (404 , "The static definition file %s does not exist!" );
94
+ assertEquals (expectedResponse , actualResponse );
95
+ }
96
+
97
+ @ Test
98
+ void givenFileAlreadyExists_thenThrowException () throws IOException {
99
+ staticDefinitionGenerator .generateFile ("services: \\ n " , testServiceId );
100
+ try {
101
+ assertThrows (FileAlreadyExistsException .class , () ->
102
+ staticDefinitionGenerator .generateFile ("services: \\ n serviceId: service\\ n " , testServiceId ));
103
+ } finally {
104
+ // cleanup
105
+ staticDefinitionGenerator .deleteFile (testServiceId );
106
+ }
72
107
}
73
108
74
109
}
@@ -79,27 +114,29 @@ class WhenStaticDefinitionOverrideResponse {
79
114
80
115
@ BeforeEach
81
116
void setUp () {
82
- ReflectionTestUtils .setField (staticDefinitionGenerator ,"staticApiDefinitionsDirectories" ,"config/local/api-defs" );
117
+ TokenAuthentication authentication = new TokenAuthentication ("token" );
118
+ authentication .setAuthenticated (true );
119
+ SecurityContextHolder .setContext (new SecurityContextImpl (authentication ));
120
+ ReflectionTestUtils .setField (staticDefinitionGenerator , "staticApiDefinitionsDirectories" , "../config/local/api-defs" );
83
121
}
84
122
85
123
@ Test
86
124
void givenInvalidRequest_thenThrowException () throws IOException {
87
- TokenAuthentication authentication = new TokenAuthentication ("token" );
88
- authentication .setAuthenticated (true );
89
- SecurityContextHolder .setContext (new SecurityContextImpl (authentication ));
90
125
StaticAPIResponse actualResponse = staticDefinitionGenerator .overrideFile ("services: \\ n " , "" );
91
126
StaticAPIResponse expectedResponse = new StaticAPIResponse (400 , "The service ID format is not valid." );
92
127
assertEquals (expectedResponse , actualResponse );
93
128
}
94
129
95
130
@ Test
96
- void givenInvalidRequest_thenThrowExceptionWithCorrectPath () {
97
- TokenAuthentication authentication = new TokenAuthentication ("token" );
98
- authentication .setAuthenticated (true );
99
- SecurityContextHolder .setContext (new SecurityContextImpl (authentication ));
100
- Exception exception = assertThrows (IOException .class , () ->
101
- staticDefinitionGenerator .overrideFile ("services: \\ n serviceId: service\\ n " , "service" ));
102
- assertEquals ("./config/local/api-defs/service.yml (No such file or directory)" , exception .getMessage ());
131
+ void givenValidServiceId_thenResponseIsOK () throws IOException {
132
+
133
+ StaticAPIResponse actualResponse = staticDefinitionGenerator .overrideFile ("services: \\ n " , testServiceId );
134
+ try {
135
+ assertEquals (201 , actualResponse .getStatusCode ());
136
+ } finally {
137
+ // cleanup
138
+ staticDefinitionGenerator .deleteFile (testServiceId );
139
+ }
103
140
}
104
141
}
105
142
0 commit comments