package com.tourguide.integration.unipush;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.tourguide.config.properties.UniPushProperties;
|
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.Test;
|
import org.springframework.http.MediaType;
|
import org.springframework.test.web.client.MockRestServiceServer;
|
import org.springframework.web.client.RestTemplate;
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.springframework.test.web.client.ExpectedCount.once;
|
import static org.springframework.test.web.client.match.MockRestRequestMatchers.header;
|
import static org.springframework.test.web.client.match.MockRestRequestMatchers.jsonPath;
|
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
|
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
|
import static org.springframework.http.HttpMethod.POST;
|
|
class UniPushClientTest {
|
private MockRestServiceServer server;
|
private UniPushClient client;
|
|
@BeforeEach
|
void setUp() {
|
UniPushProperties properties = new UniPushProperties();
|
properties.setEnabled(true);
|
properties.setAppId("app-id");
|
properties.setAppKey("app-key");
|
properties.setMasterSecret("master-secret");
|
properties.setBaseUrl("https://restapi.getui.com/v2");
|
RestTemplate restTemplate = new RestTemplate();
|
server = MockRestServiceServer.bindTo(restTemplate).build();
|
client = new UniPushClient(properties, restTemplate, new ObjectMapper());
|
}
|
|
@Test
|
void shouldAuthenticateAndPushNotificationToCid() {
|
server.expect(once(), requestTo("https://restapi.getui.com/v2/app-id/auth"))
|
.andExpect(method(POST))
|
.andExpect(jsonPath("$.appkey").value("app-key"))
|
.andExpect(jsonPath("$.timestamp").isNumber())
|
.andExpect(jsonPath("$.sign").isString())
|
.andRespond(withSuccess(
|
"{\"code\":0,\"data\":{\"token\":\"token-1\",\"expire_time\":4102444800000}}",
|
MediaType.APPLICATION_JSON));
|
server.expect(once(), requestTo("https://restapi.getui.com/v2/app-id/push/single/cid"))
|
.andExpect(method(POST))
|
.andExpect(header("token", "token-1"))
|
.andExpect(jsonPath("$.audience.cid[0]").value("cid-1"))
|
.andExpect(jsonPath("$.push_message.notification.title").value("订单已取消"))
|
.andExpect(jsonPath("$.push_message.notification.body")
|
.value("游客已取消订单,该订单无需再前往服务。"))
|
.andExpect(jsonPath("$.push_message.notification.click_type").value("payload"))
|
.andRespond(withSuccess("{\"code\":0,\"msg\":\"success\"}", MediaType.APPLICATION_JSON));
|
|
boolean success = client.pushToCid("cid-1", "订单已取消",
|
"游客已取消订单,该订单无需再前往服务。", "/pages/order-detail/index?id=99");
|
|
assertThat(success).isTrue();
|
server.verify();
|
}
|
}
|