Skip to content

Commit 9a48701

Browse files
eleonora2687jzheng2017
authored andcommitted
feat: fix whitespace formatting and adding test for the functionality
1 parent 5e064ca commit 9a48701

File tree

4 files changed

+89
-11
lines changed

4 files changed

+89
-11
lines changed

src/main/java/spotify/api/impl/MarketApiRetrofit.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
7-
87
import retrofit2.Call;
98
import retrofit2.Response;
109
import spotify.api.enums.HttpStatusCode;
@@ -18,12 +17,10 @@
1817

1918

2019
public class MarketApiRetrofit implements MarketApi {
21-
2220
private final Logger logger = LoggerFactory.getLogger(MarketApiRetrofit.class);
2321
private final String accessToken;
2422
private final MarketService marketService;
2523

26-
2724
public MarketApiRetrofit(final String accessToken) {
2825
this(accessToken, RetrofitHttpServiceFactory.getMarketService());
2926
}
@@ -33,15 +30,13 @@ public MarketApiRetrofit(final String accessToken, final MarketService marketSer
3330
this.marketService = marketService;
3431
}
3532

36-
3733
@Override
3834
public MarketFull getMarkets(){
3935
logger.trace("Constructing HTTP call to fetch markets.");
4036
Call<MarketFull> httpCall = marketService.getMarkets("Bearer " + this.accessToken);
4137

4238
try {
43-
44-
logger.info("Executing HTTP call to fetch markets");
39+
logger.info("Executing HTTP call to fetch markets.");
4540
logger.debug("Fetching markets....");
4641
LoggingUtil.logHttpCall(logger, httpCall);
4742
Response<MarketFull> response = httpCall.execute();

src/main/java/spotify/api/spotify/SpotifyApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public AlbumFull getAlbum(String albumId, Map<String, String> options) {
105105
}
106106

107107
public MarketFull getMarket() {
108-
logger.info("Requesting all available markets");
108+
logger.info("Requesting all available markets.");
109109
return marketApi.getMarkets();
110110
}
111111

src/main/java/spotify/models/markets/MarketFull.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package spotify.models.markets;
22

3-
import java.util.ArrayList;
43
import java.util.List;
54

65
public class MarketFull {
7-
8-
private List<String> markets = new ArrayList<String>() ;
9-
public List<String> getAllMarkets() {
6+
private List<String> markets;
7+
public List<String> getMarkets() {
108
return markets;
119

1210
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package spotify.api.impl;
2+
3+
import okhttp3.MediaType;
4+
import okhttp3.Request;
5+
import okhttp3.ResponseBody;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
import org.mockito.Mock;
10+
import org.mockito.MockitoAnnotations;
11+
import retrofit2.Call;
12+
import retrofit2.Response;
13+
import spotify.exceptions.HttpRequestFailedException;
14+
import spotify.exceptions.SpotifyActionFailedException;
15+
import spotify.models.markets.MarketFull;
16+
import spotify.retrofit.services.MarketService;
17+
18+
import java.io.IOException;
19+
20+
import static org.mockito.ArgumentMatchers.anyString;
21+
import static org.mockito.Mockito.verify;
22+
import static org.mockito.Mockito.when;
23+
24+
25+
public class MarketApiRetrofitTest extends AbstractApiRetrofitTest {
26+
private MarketApiRetrofit sut;
27+
@Mock
28+
private MarketService mockedMarketService;
29+
@Mock
30+
private Call<MarketFull> mockedMarketFullCall;
31+
32+
@BeforeEach
33+
void setUp() {
34+
MockitoAnnotations.openMocks(this);
35+
36+
sut = new MarketApiRetrofit(fakeAccessToken, mockedMarketService);
37+
38+
when(mockedMarketService.getMarkets(anyString())).thenReturn(mockedMarketFullCall);
39+
40+
when(mockedMarketFullCall.request()).thenReturn(new Request.Builder().url(fakeUrl).build());
41+
}
42+
43+
@Test
44+
void getMarketsUsesCorrectValuesToCreatHttpCall() throws IOException {
45+
when(mockedMarketFullCall.execute()).thenReturn(Response.success(new MarketFull()));
46+
47+
sut.getMarkets();
48+
verify(mockedMarketService).getMarkets(fakeAccessTokenWithBearer);
49+
}
50+
51+
@Test
52+
void getMarketsExecutesHttpCall() throws IOException {
53+
when(mockedMarketFullCall.execute()).thenReturn(Response.success(new MarketFull()));
54+
55+
sut.getMarkets();
56+
verify(mockedMarketFullCall).execute();
57+
}
58+
59+
@Test
60+
void getMarketsThrowsSpotifyActionFailedExceptionWhenError() throws IOException {
61+
when(mockedMarketFullCall.execute())
62+
.thenReturn(
63+
Response.error(
64+
400,
65+
ResponseBody.create(MediaType.get("application/json"), getJson("error.json"))
66+
)
67+
);
68+
69+
Assertions.assertThrows(SpotifyActionFailedException.class, () -> sut.getMarkets());
70+
}
71+
72+
@Test
73+
void getMarketsThrowsHttpRequestFailedWhenHttpFails() throws IOException {
74+
when(mockedMarketFullCall.execute()).thenThrow(IOException.class);
75+
76+
Assertions.assertThrows(HttpRequestFailedException.class, () -> sut.getMarkets());
77+
}
78+
79+
@Test
80+
void getMarketsReturnsMarketFullWhenSuccessful() throws IOException {
81+
when(mockedMarketFullCall.execute()).thenReturn(Response.success(new MarketFull()));
82+
83+
Assertions.assertNotNull(sut.getMarkets());
84+
}
85+
}

0 commit comments

Comments
 (0)