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 ;
1829import java .io .FileNotFoundException ;
30+ import java .net .URI ;
1931import org .eclipse .che .api .factory .server .scm .PersonalAccessToken ;
2032import org .eclipse .che .api .factory .server .scm .PersonalAccessTokenManager ;
2133import org .eclipse .che .api .factory .server .scm .exception .UnknownScmProviderException ;
2234import org .eclipse .che .api .workspace .server .devfile .FileContentProvider ;
2335import org .eclipse .che .api .workspace .server .devfile .URLFetcher ;
36+ import org .eclipse .che .api .workspace .server .devfile .exception .DevfileException ;
2437import org .mockito .Mock ;
25- import org .mockito .Mockito ;
2638import org .mockito .testng .MockitoTestNGListener ;
39+ import org .testng .annotations .AfterMethod ;
40+ import org .testng .annotations .BeforeMethod ;
2741import org .testng .annotations .Listeners ;
2842import org .testng .annotations .Test ;
2943
3044@ Listeners (MockitoTestNGListener .class )
3145public class GitlabAuthorizingFileContentProviderTest {
3246 @ 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+ }
3365
3466 @ Test
3567 public void shouldExpandRelativePaths () throws Exception {
36- URLFetcher urlFetcher = Mockito .mock (URLFetcher .class );
3768 GitlabUrl gitlabUrl = new GitlabUrl ().withHostName ("gitlab.net" ).withSubGroups ("eclipse/che" );
3869 FileContentProvider fileContentProvider =
3970 new GitlabAuthorizingFileContentProvider (gitlabUrl , urlFetcher , personalAccessTokenManager );
@@ -49,7 +80,6 @@ public void shouldExpandRelativePaths() throws Exception {
4980
5081 @ Test
5182 public void shouldPreserveAbsolutePaths () throws Exception {
52- URLFetcher urlFetcher = Mockito .mock (URLFetcher .class );
5383 GitlabUrl gitlabUrl = new GitlabUrl ().withHostName ("gitlab.net" ).withSubGroups ("eclipse/che" );
5484 FileContentProvider fileContentProvider =
5585 new GitlabAuthorizingFileContentProvider (gitlabUrl , urlFetcher , personalAccessTokenManager );
@@ -65,18 +95,51 @@ public void shouldPreserveAbsolutePaths() throws Exception {
6595 @ Test (expectedExceptions = FileNotFoundException .class )
6696 public void shouldThrowFileNotFoundException () throws Exception {
6797 // given
68- URLFetcher urlFetcher = Mockito .mock (URLFetcher .class );
6998 when (urlFetcher .fetch (
7099 eq (
71- "https://gitlab.com/api/v4/projects/eclipse%2Fche/repository/files/devfile.yaml/raw?ref=HEAD" )))
100+ wireMockServer .url (
101+ "/api/v4/projects/eclipse%2Fche/repository/files/devfile.yaml/raw?ref=HEAD" ))))
72102 .thenThrow (new FileNotFoundException ());
73- when (urlFetcher .fetch (eq ("https://gitlab.com/eclipse/che" ))).thenReturn ("content" );
74103 when (personalAccessTokenManager .getAndStore (anyString ()))
75104 .thenThrow (new UnknownScmProviderException ("" , "" ));
76- GitlabUrl gitlabUrl = new GitlabUrl ().withHostName ("gitlab.com" ).withSubGroups ("eclipse/che" );
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" );
77111 FileContentProvider fileContentProvider =
78112 new GitlabAuthorizingFileContentProvider (gitlabUrl , urlFetcher , personalAccessTokenManager );
79113
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+
80143 // when
81144 fileContentProvider .fetchContent ("devfile.yaml" );
82145 }
0 commit comments