1111 */
1212package org .eclipse .che .api .factory .server .gitlab ;
1313
14+ import static com .github .tomakehurst .wiremock .client .WireMock .aResponse ;
15+ import static com .github .tomakehurst .wiremock .client .WireMock .get ;
16+ import static com .github .tomakehurst .wiremock .client .WireMock .stubFor ;
17+ import static com .github .tomakehurst .wiremock .client .WireMock .urlEqualTo ;
18+ import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .wireMockConfig ;
19+ import static java .lang .String .format ;
20+ import static java .net .HttpURLConnection .HTTP_MOVED_TEMP ;
21+ import static java .net .HttpURLConnection .HTTP_OK ;
1422import static org .mockito .ArgumentMatchers .*;
1523import static org .mockito .Mockito .verify ;
1624import static org .mockito .Mockito .when ;
1725
26+ import com .github .tomakehurst .wiremock .WireMockServer ;
27+ import com .github .tomakehurst .wiremock .client .WireMock ;
28+ import com .github .tomakehurst .wiremock .common .Slf4jNotifier ;
29+ import java .io .FileNotFoundException ;
30+ import java .net .URI ;
1831import org .eclipse .che .api .factory .server .scm .PersonalAccessToken ;
1932import org .eclipse .che .api .factory .server .scm .PersonalAccessTokenManager ;
33+ import org .eclipse .che .api .factory .server .scm .exception .UnknownScmProviderException ;
2034import org .eclipse .che .api .workspace .server .devfile .FileContentProvider ;
2135import org .eclipse .che .api .workspace .server .devfile .URLFetcher ;
36+ import org .eclipse .che .api .workspace .server .devfile .exception .DevfileException ;
2237import org .mockito .Mock ;
23- import org .mockito .Mockito ;
2438import org .mockito .testng .MockitoTestNGListener ;
39+ import org .testng .annotations .AfterMethod ;
40+ import org .testng .annotations .BeforeMethod ;
2541import org .testng .annotations .Listeners ;
2642import org .testng .annotations .Test ;
2743
2844@ Listeners (MockitoTestNGListener .class )
2945public class GitlabAuthorizingFileContentProviderTest {
3046 @ Mock private PersonalAccessTokenManager personalAccessTokenManager ;
47+ @ Mock private URLFetcher urlFetcher ;
48+
49+ private WireMockServer wireMockServer ;
50+ private WireMock wireMock ;
51+
52+ @ BeforeMethod
53+ public void start () {
54+ wireMockServer =
55+ new WireMockServer (wireMockConfig ().notifier (new Slf4jNotifier (false )).dynamicPort ());
56+ wireMockServer .start ();
57+ WireMock .configureFor ("localhost" , wireMockServer .port ());
58+ wireMock = new WireMock ("localhost" , wireMockServer .port ());
59+ }
60+
61+ @ AfterMethod
62+ void stop () {
63+ wireMockServer .stop ();
64+ }
3165
3266 @ Test
3367 public void shouldExpandRelativePaths () throws Exception {
34- URLFetcher urlFetcher = Mockito .mock (URLFetcher .class );
3568 GitlabUrl gitlabUrl = new GitlabUrl ().withHostName ("gitlab.net" ).withSubGroups ("eclipse/che" );
3669 FileContentProvider fileContentProvider =
3770 new GitlabAuthorizingFileContentProvider (gitlabUrl , urlFetcher , personalAccessTokenManager );
@@ -47,7 +80,6 @@ public void shouldExpandRelativePaths() throws Exception {
4780
4881 @ Test
4982 public void shouldPreserveAbsolutePaths () throws Exception {
50- URLFetcher urlFetcher = Mockito .mock (URLFetcher .class );
5183 GitlabUrl gitlabUrl = new GitlabUrl ().withHostName ("gitlab.net" ).withSubGroups ("eclipse/che" );
5284 FileContentProvider fileContentProvider =
5385 new GitlabAuthorizingFileContentProvider (gitlabUrl , urlFetcher , personalAccessTokenManager );
@@ -59,4 +91,56 @@ public void shouldPreserveAbsolutePaths() throws Exception {
5991 fileContentProvider .fetchContent (url );
6092 verify (urlFetcher ).fetch (eq (url ), eq ("Bearer my-token" ));
6193 }
94+
95+ @ Test (expectedExceptions = FileNotFoundException .class )
96+ public void shouldThrowFileNotFoundException () throws Exception {
97+ // given
98+ when (urlFetcher .fetch (
99+ eq (
100+ wireMockServer .url (
101+ "/api/v4/projects/eclipse%2Fche/repository/files/devfile.yaml/raw?ref=HEAD" ))))
102+ .thenThrow (new FileNotFoundException ());
103+ when (personalAccessTokenManager .getAndStore (anyString ()))
104+ .thenThrow (new UnknownScmProviderException ("" , "" ));
105+ URI uri = URI .create (wireMockServer .url ("/" ));
106+ GitlabUrl gitlabUrl =
107+ new GitlabUrl ()
108+ .withScheme ("http" )
109+ .withHostName (format ("%s:%s" , uri .getHost (), uri .getPort ()))
110+ .withSubGroups ("eclipse/che" );
111+ FileContentProvider fileContentProvider =
112+ new GitlabAuthorizingFileContentProvider (gitlabUrl , urlFetcher , personalAccessTokenManager );
113+
114+ stubFor (get (urlEqualTo ("/eclipse/che" )).willReturn (aResponse ().withStatus (HTTP_OK )));
115+
116+ // when
117+ fileContentProvider .fetchContent ("devfile.yaml" );
118+ }
119+
120+ @ Test (
121+ expectedExceptions = DevfileException .class ,
122+ expectedExceptionsMessageRegExp = "Could not reach devfile at test path" )
123+ public void shouldThrowDevfileException () throws Exception {
124+ // given
125+ when (urlFetcher .fetch (
126+ eq (
127+ wireMockServer .url (
128+ "/api/v4/projects/eclipse%2Fche/repository/files/devfile.yaml/raw?ref=HEAD" ))))
129+ .thenThrow (new FileNotFoundException ("test path" ));
130+ when (personalAccessTokenManager .getAndStore (anyString ()))
131+ .thenThrow (new UnknownScmProviderException ("" , "" ));
132+ URI uri = URI .create (wireMockServer .url ("/" ));
133+ GitlabUrl gitlabUrl =
134+ new GitlabUrl ()
135+ .withScheme ("http" )
136+ .withHostName (format ("%s:%s" , uri .getHost (), uri .getPort ()))
137+ .withSubGroups ("eclipse/che" );
138+ FileContentProvider fileContentProvider =
139+ new GitlabAuthorizingFileContentProvider (gitlabUrl , urlFetcher , personalAccessTokenManager );
140+
141+ stubFor (get (urlEqualTo ("/eclipse/che" )).willReturn (aResponse ().withStatus (HTTP_MOVED_TEMP )));
142+
143+ // when
144+ fileContentProvider .fetchContent ("devfile.yaml" );
145+ }
62146}
0 commit comments