1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.http.remote;
18
19 import static org.apache.juneau.TestUtils.*;
20 import static org.apache.juneau.common.utils.IOUtils.*;
21 import static org.junit.jupiter.api.Assertions.*;
22
23 import java.io.*;
24
25 import org.apache.juneau.*;
26 import org.apache.juneau.http.annotation.*;
27 import org.apache.juneau.marshaller.*;
28 import org.apache.juneau.rest.*;
29 import org.apache.juneau.rest.annotation.*;
30 import org.apache.juneau.rest.config.*;
31 import org.apache.juneau.rest.mock.*;
32 import org.junit.jupiter.api.*;
33
34 class Remote_ResponseAnnotation_Test extends TestBase {
35
36 public static class ABean {
37 public int f;
38 static ABean get() {
39 var x = new ABean();
40 x.f = 1;
41 return x;
42 }
43 @Override
44 public String toString() {
45 return Json5.of(this);
46 }
47 }
48
49 private static ABean bean = ABean.get();
50
51
52
53
54
55 @Rest
56 public static class A {
57 @RestOp
58 public String get(RestResponse res) {
59 res.setHeader("X","x");
60 res.setStatus(201);
61 return "foo";
62 }
63 }
64
65 @Response
66 public interface A1a {
67 @Content Reader getContent();
68 @Header("X") String getHeader();
69 @StatusCode int getStatus();
70 }
71
72 @Remote
73 public interface A1b {
74 @RemoteOp A1a get();
75 }
76
77 @Test void a01_basic() throws Exception {
78 var x = remote(A.class,A1b.class).get();
79 assertEquals("foo",read(x.getContent()));
80 assertEquals("x",x.getHeader());
81 assertEquals(201,x.getStatus());
82 }
83
84 @Response
85 public interface A2a {
86 @Content Reader getContent();
87 }
88
89 @Remote
90 public interface A2b {
91 @RemoteOp A2a get();
92 }
93
94 @Test void a02_unannotatedMethod() throws Exception {
95 var x = remote(A.class,A2b.class).get();
96 assertEquals("foo",read(x.getContent()));
97 }
98
99 @Rest
100 public static class A3 implements BasicJsonConfig {
101 @RestOp
102 public ABean get() {
103 return bean;
104 }
105 }
106
107 @Response
108 public interface A3a {
109 @Content ABean getContent();
110 }
111
112 @Remote
113 public interface A3b {
114 @RemoteOp A3a get();
115 }
116
117 @Test void a03_beanBody() {
118 var x = client(A3.class).json().build().getRemote(A3b.class).get();
119 assertEquals("{f:1}",x.getContent().toString());
120
121 var x2 = client(A3.class).build().getRemote(A3b.class).get();
122 assertThrowsWithMessage(Exception.class, "Unsupported media-type in request header 'Content-Type': 'application/json'", x2::getContent);
123 }
124
125
126
127
128
129 private static <T> T remote(Class<?> rest, Class<T> t) {
130 return MockRestClient.create(rest).build().getRemote(t);
131 }
132
133 private static <T> MockRestClient.Builder client(Class<?> rest) {
134 return MockRestClient.create(rest);
135 }
136 }