liujie
2026-08-04 e9e42b621afdf6ac94f5327ee843b32b10a401b4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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();
    }
}