-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from SRGSSR/develop
2
- Loading branch information
Showing
7 changed files
with
284 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/com/example/pfff/controller/RecommendationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.example.pfff.controller; | ||
|
||
import com.example.pfff.service.RecommendationService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.servlet.ModelAndView; | ||
import org.springframework.web.servlet.view.RedirectView; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.util.List; | ||
|
||
/** | ||
* Copyright (c) SRG SSR. All rights reserved. | ||
* <p> | ||
* License information is available from the LICENSE file. | ||
*/ | ||
@Controller | ||
public class RecommendationController { | ||
@Autowired | ||
RecommendationService service; | ||
|
||
@RequestMapping("/api/v1/playlist/recommendation/{purpose}/{urn}") | ||
@ResponseBody | ||
Object recommendation( | ||
HttpServletRequest request, | ||
@PathVariable("purpose") String purpose, | ||
@PathVariable("urn") String urn, | ||
@RequestParam(value = "standalone", required = false, defaultValue = "false") boolean standalone, | ||
@RequestParam(value = "format", required = false, defaultValue = "media") String format) { | ||
List<String> urns = service.getRecommendedUrns(purpose, urn, standalone); | ||
urns.add(0, urn); | ||
if ("urn".equals(format)) { | ||
return urns; | ||
} else { //if ("media".equals(format)) { | ||
UriComponentsBuilder builder = UriComponentsBuilder.newInstance(); | ||
builder.scheme(request.getScheme()); | ||
builder.host("il.srgssr.ch"); | ||
builder.path("integrationlayer/2.0/mediaList/byUrns.json"); | ||
builder.queryParam("urns", String.join(",", urns)); | ||
return new ModelAndView(new RedirectView(builder.toUriString())); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/example/pfff/model/peach/RecommendationResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.example.pfff.model.peach; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class RecommendationResult { | ||
@JsonProperty("status") | ||
private String status; | ||
|
||
@JsonProperty("result") | ||
private Result result; | ||
|
||
private static class Result { | ||
@JsonProperty("items") | ||
private Item[] items; | ||
} | ||
|
||
private static class Item { | ||
@JsonProperty("id") | ||
private String id; | ||
@JsonProperty("urn") | ||
private String urn; | ||
} | ||
|
||
public List<String> getUrns() { | ||
ArrayList<String> urns = new ArrayList<>(); | ||
if (result != null) { | ||
if (result.items != null) { | ||
for (int i = 0; i < result.items.length; i++) { | ||
Item item = result.items[i]; | ||
if (item.urn != null) { | ||
urns.add(item.urn); | ||
} else { | ||
urns.add("urn:rts:video:" + item.id); | ||
} | ||
} | ||
return urns; | ||
} | ||
} | ||
return urns; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/example/pfff/service/RecommendationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.example.pfff.service; | ||
|
||
import com.example.pfff.model.peach.RecommendationResult; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.util.UriComponents; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
public class RecommendationService { | ||
|
||
private RestTemplate restTemplate; | ||
|
||
public RecommendationService() { | ||
restTemplate = new RestTemplate(); | ||
} | ||
|
||
public List<String> getRecommendedUrns(String purpose, String urn, boolean standalone) { | ||
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.newInstance().scheme("http").host("peach.ebu.io").path("api/v1/chrts/continuous_playback_mobile"); | ||
uriComponentsBuilder.queryParam("urn", urn); | ||
uriComponentsBuilder.queryParam("purpose", purpose); | ||
uriComponentsBuilder.queryParam("pageSize", 50); | ||
uriComponentsBuilder.queryParam("standalone", standalone); | ||
UriComponents url = uriComponentsBuilder.build(); | ||
|
||
System.out.println(url.toUriString()); | ||
|
||
return restTemplate.exchange(url.toUriString(), HttpMethod.GET, null, RecommendationResult.class).getBody().getUrns(); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/test/java/com/example/pfff/controller/RecommendationIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.example.pfff.controller; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.context.WebApplicationContext; | ||
|
||
import java.util.List; | ||
|
||
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
public class RecommendationIntegrationTest { | ||
private MockMvc mvc; | ||
|
||
@Autowired | ||
private WebApplicationContext context; | ||
|
||
@Before | ||
public void setup() { | ||
mvc = MockMvcBuilders | ||
.webAppContextSetup(context) | ||
.apply(springSecurity()) | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void getRecommendation() throws Exception { | ||
String purpose = "continuousplayback"; | ||
String mediaURN = "urn:rts:video:9691670"; | ||
|
||
mvc.perform(get("/api/v1/playlist/recommendation/" + purpose + "/" + mediaURN)).andExpect(status().isFound()).andExpect(content().string("")); | ||
|
||
checkRecommendationWithRedirect(purpose, mediaURN, "false"); | ||
|
||
checkRecommendationWithRedirect(purpose, mediaURN, "true"); | ||
} | ||
|
||
private void checkRecommendationWithRedirect(String purpose, String mediaURN, String standalone) throws Exception { | ||
MvcResult mvcResult = mvc.perform(get("/api/v1/playlist/recommendation/" + purpose + "/" + mediaURN).param("standalone", standalone)).andExpect(status().is3xxRedirection()).andExpect(content().string("")).andReturn(); | ||
String location = mvcResult.getResponse().getHeader("Location"); | ||
System.out.println(location); | ||
RestTemplate restTemplate = new RestTemplate(); | ||
MediaListResult result = restTemplate.exchange(location, HttpMethod.GET, null, MediaListResult.class).getBody(); | ||
Assert.assertNotNull(result.mediaList); | ||
} | ||
|
||
static class MediaListResult { | ||
@JsonProperty("mediaList") | ||
List<Object> mediaList; | ||
} | ||
|
||
@Test | ||
public void getRecommendationURNFormat() throws Exception { | ||
String purpose = "continuousplayback"; | ||
String mediaURN = "urn:rts:video:9691670"; | ||
String format = "urn"; | ||
|
||
mvc.perform(get("/api/v1/playlist/recommendation/" + purpose + "/" + mediaURN).param("format", format)).andExpect(status().isOk()).andExpect(jsonPath("$").isArray()); | ||
|
||
mvc.perform(get("/api/v1/playlist/recommendation/" + purpose + "/" + mediaURN).param("standalone", "false").param("format", format)).andExpect(status().isOk()).andExpect(jsonPath("$").isArray()); | ||
|
||
mvc.perform(get("/api/v1/playlist/recommendation/" + purpose + "/" + mediaURN).param("standalone", "true").param("format", format)).andExpect(status().isOk()).andExpect(jsonPath("$").isArray()); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/test/java/com/example/pfff/controller/RecommendationServiceTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.example.pfff.controller; | ||
|
||
import com.example.pfff.service.RecommendationService; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import java.util.List; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class RecommendationServiceTests { | ||
|
||
@Autowired | ||
private RecommendationService recommendationService; | ||
|
||
@Test | ||
public void getRecommendedUrnsContinuousplaybackRTSTest() { | ||
String purpose = "continuousplayback"; | ||
String mediaURN = "urn:rts:video:9691670"; | ||
boolean standalone = false; | ||
List<String> urns = recommendationService.getRecommendedUrns(purpose, mediaURN, standalone); | ||
|
||
Assert.assertNotNull(urns); | ||
} | ||
|
||
@Test | ||
public void getRecommendedUrnsContinuousplaybackStandaloneRTSTest() { | ||
String purpose = "continuousplayback"; | ||
String mediaURN = "urn:rts:video:9691670"; | ||
boolean standalone = true; | ||
List<String> urns = recommendationService.getRecommendedUrns(purpose, mediaURN, standalone); | ||
|
||
Assert.assertNotNull(urns); | ||
} | ||
|
||
@Test | ||
public void getRecommendedUrnsContinuousplaybackSRFTest() { | ||
String purpose = "continuousplayback"; | ||
String mediaURN = "urn:rts:video:859dc7e6-a155-41da-9d34-8f4eb800f73c"; | ||
boolean standalone = false; | ||
List<String> urns = recommendationService.getRecommendedUrns(purpose, mediaURN, standalone); | ||
|
||
Assert.assertNotNull(urns); | ||
} | ||
|
||
@Test | ||
public void getRecommendedUrnsContinuousplaybackStandaloneSRFTest() { | ||
String purpose = "continuousplayback"; | ||
String mediaURN = "urn:rts:video:859dc7e6-a155-41da-9d34-8f4eb800f73c"; | ||
boolean standalone = true; | ||
List<String> urns = recommendationService.getRecommendedUrns(purpose, mediaURN, standalone); | ||
|
||
Assert.assertNotNull(urns); | ||
} | ||
|
||
@Test | ||
public void getRecommendedUrnsContinuousplaybackSwissTxtTest() { | ||
String purpose = "continuousplayback"; | ||
String mediaURN = "urn:swisstxt:video:rts:288208"; | ||
boolean standalone = false; | ||
List<String> urns = recommendationService.getRecommendedUrns(purpose, mediaURN, standalone); | ||
|
||
Assert.assertNotNull(urns); | ||
} | ||
|
||
@Test | ||
public void getRecommendedUrnsContinuousplaybackStandaloneSwisstxtTest() { | ||
String purpose = "continuousplayback"; | ||
String mediaURN = "urn:swisstxt:video:rts:288208"; | ||
boolean standalone = true; | ||
List<String> urns = recommendationService.getRecommendedUrns(purpose, mediaURN, standalone); | ||
|
||
Assert.assertNotNull(urns); | ||
} | ||
} |