2323import org .apache .commons .io .IOUtils ;
2424import org .apache .http .HttpEntity ;
2525import org .apache .http .client .methods .CloseableHttpResponse ;
26+ import org .apache .http .client .methods .HttpGet ;
2627import org .apache .http .client .methods .HttpPost ;
2728import org .apache .http .entity .StringEntity ;
2829import org .apache .http .impl .client .CloseableHttpClient ;
@@ -65,7 +66,7 @@ public void callSetDate() throws IOException, JsonParseException,
6566 Map <String , Object > rootAsMap = mapper .readValue (
6667 responseString .substring (1 , responseString .length () - 1 ), Map .class );
6768 assertThat (rootAsMap ).hasSize (5 );
68- assertThat (rootAsMap .get ("result" )).isEqualTo ("102,26.04.2012" );
69+ assertThat (rootAsMap .get ("result" )).isEqualTo ("102,26.04.2012, " );
6970 assertThat (rootAsMap .get ("method" )).isEqualTo ("setDate" );
7071 assertThat (rootAsMap .get ("type" )).isEqualTo ("rpc" );
7172 assertThat (rootAsMap .get ("action" )).isEqualTo ("securedService" );
@@ -77,4 +78,93 @@ public void callSetDate() throws IOException, JsonParseException,
7778 }
7879 }
7980
81+ @ Test
82+ public void callSecretNotLoggedIn () throws IOException , JsonParseException ,
83+ JsonMappingException {
84+
85+ CloseableHttpClient client = HttpClientBuilder .create ().build ();
86+ CloseableHttpResponse response = null ;
87+ try {
88+
89+ HttpPost post = new HttpPost ("http://localhost:9998/controller/router" );
90+
91+ StringEntity postEntity = new StringEntity (
92+ "{\" action\" :\" securedService\" ,\" method\" :\" secret\" ,\" data\" :[\" ralph\" ],\" type\" :\" rpc\" ,\" tid\" :1}" ,
93+ "UTF-8" );
94+
95+ post .setEntity (postEntity );
96+ post .setHeader ("Content-Type" , "application/json; charset=UTF-8" );
97+
98+ response = client .execute (post );
99+ HttpEntity entity = response .getEntity ();
100+ assertThat (entity ).isNotNull ();
101+ String responseString = EntityUtils .toString (entity );
102+
103+ assertThat (responseString ).isNotNull ();
104+ assertThat (responseString .startsWith ("[" ) && responseString .endsWith ("]" ))
105+ .isTrue ();
106+ ObjectMapper mapper = new ObjectMapper ();
107+ Map <String , Object > rootAsMap = mapper .readValue (
108+ responseString .substring (1 , responseString .length () - 1 ), Map .class );
109+ assertThat (rootAsMap ).hasSize (5 );
110+ assertThat (rootAsMap .get ("message" )).isEqualTo ("Server Error" );
111+ assertThat (rootAsMap .get ("method" )).isEqualTo ("secret" );
112+ assertThat (rootAsMap .get ("type" )).isEqualTo ("exception" );
113+ assertThat (rootAsMap .get ("action" )).isEqualTo ("securedService" );
114+ assertThat (rootAsMap .get ("tid" )).isEqualTo (1 );
115+ }
116+ finally {
117+ IOUtils .closeQuietly (response );
118+ IOUtils .closeQuietly (client );
119+ }
120+ }
121+
122+ @ Test
123+ public void callSecretLoggedIn () throws IOException , JsonParseException ,
124+ JsonMappingException {
125+
126+ CloseableHttpClient client = HttpClientBuilder .create ().build ();
127+ CloseableHttpResponse response = null ;
128+ try {
129+
130+ HttpGet login = new HttpGet ("http://localhost:9998/controller/login" );
131+ response = client .execute (login );
132+ HttpEntity entity = response .getEntity ();
133+ String responseString = EntityUtils .toString (entity );
134+ System .out .println (responseString );
135+ response .close ();
136+
137+ HttpPost post = new HttpPost ("http://localhost:9998/controller/router" );
138+
139+ StringEntity postEntity = new StringEntity (
140+ "{\" action\" :\" securedService\" ,\" method\" :\" secret\" ,\" data\" :[\" ralph\" ],\" type\" :\" rpc\" ,\" tid\" :1}" ,
141+ "UTF-8" );
142+
143+ post .setEntity (postEntity );
144+ post .setHeader ("Content-Type" , "application/json; charset=UTF-8" );
145+
146+ response = client .execute (post );
147+ entity = response .getEntity ();
148+ assertThat (entity ).isNotNull ();
149+ responseString = EntityUtils .toString (entity );
150+
151+ assertThat (responseString ).isNotNull ();
152+ assertThat (responseString .startsWith ("[" ) && responseString .endsWith ("]" ))
153+ .isTrue ();
154+ ObjectMapper mapper = new ObjectMapper ();
155+ Map <String , Object > rootAsMap = mapper .readValue (
156+ responseString .substring (1 , responseString .length () - 1 ), Map .class );
157+ assertThat (rootAsMap ).hasSize (5 );
158+ assertThat (rootAsMap .get ("result" )).isEqualTo ("RALPH,jimi" );
159+ assertThat (rootAsMap .get ("method" )).isEqualTo ("secret" );
160+ assertThat (rootAsMap .get ("type" )).isEqualTo ("rpc" );
161+ assertThat (rootAsMap .get ("action" )).isEqualTo ("securedService" );
162+ assertThat (rootAsMap .get ("tid" )).isEqualTo (1 );
163+ }
164+ finally {
165+ IOUtils .closeQuietly (response );
166+ IOUtils .closeQuietly (client );
167+ }
168+ }
169+
80170}
0 commit comments