Skip to content

Commit

Permalink
Add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ralscha committed Nov 26, 2012
1 parent af91c0e commit d69aac7
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 5 deletions.
52 changes: 52 additions & 0 deletions src/test/java/ch/ralscha/extdirectspring_itest/InfoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
*/
package ch.ralscha.extdirectspring_itest;

import java.util.Locale;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import ch.ralscha.extdirectspring.annotation.ExtDirectMethod;
import ch.ralscha.extdirectspring.annotation.ExtDirectMethodType;
Expand All @@ -24,10 +31,55 @@
@Service
public class InfoService {

@Autowired
private MessageSource messageSource;

@ExtDirectMethod(value = ExtDirectMethodType.FORM_POST, group = "itest_info_service", streamResponse = true)
public ExtDirectFormPostResult updateInfo(Info info) {
ExtDirectFormPostResult resp = new ExtDirectFormPostResult(true);
resp.addResultProperty("user-name-lower-case", info.getUserName().toLowerCase());
return resp;
}

@ExtDirectMethod(value = ExtDirectMethodType.FORM_POST, group = "itest_info_service")
public ExtDirectFormPostResult updateInfo2nd(Info info) {
ExtDirectFormPostResult resp = new ExtDirectFormPostResult();
resp.addResultProperty("user-name-lower-case", info.getUserName().toLowerCase());
return resp;
}

@ExtDirectMethod(value = ExtDirectMethodType.FORM_POST, group = "itest_info_service")
public ExtDirectFormPostResult updateInfoUser1(@Valid User user, BindingResult bindingResult) {
ExtDirectFormPostResult resp = new ExtDirectFormPostResult(null, null, bindingResult);
resp.addResultProperty("lc", user.getName().toLowerCase());
return resp;
}

@ExtDirectMethod(value = ExtDirectMethodType.FORM_POST, group = "itest_info_service")
public ExtDirectFormPostResult updateInfoUser2(Locale locale, @Valid User user, BindingResult bindingResult) {
ExtDirectFormPostResult resp = new ExtDirectFormPostResult(locale, null, bindingResult);
resp.addResultProperty("lc", user.getName().toLowerCase());
return resp;
}

@ExtDirectMethod(value = ExtDirectMethodType.FORM_POST, group = "itest_info_service")
public ExtDirectFormPostResult updateInfoUser3(Locale locale, @Valid User user, BindingResult bindingResult) {
ExtDirectFormPostResult resp = new ExtDirectFormPostResult(locale, messageSource, bindingResult);
resp.addResultProperty("lc", user.getName().toLowerCase());
return resp;
}

@ExtDirectMethod(value = ExtDirectMethodType.FORM_POST, group = "itest_info_service")
public ExtDirectFormPostResult updateInfoUser4(Locale locale, @Valid User user, BindingResult bindingResult) {
ExtDirectFormPostResult resp = new ExtDirectFormPostResult(locale, messageSource, bindingResult, true);
resp.addResultProperty("lc", user.getName().toLowerCase());
return resp;
}

@ExtDirectMethod(value = ExtDirectMethodType.FORM_POST, group = "itest_info_service")
public ExtDirectFormPostResult updateInfoUser5(@Valid User user, BindingResult bindingResult) {
ExtDirectFormPostResult resp = new ExtDirectFormPostResult(null, messageSource, bindingResult);
resp.addResultProperty("lc", user.getName().toLowerCase());
return resp;
}
}
112 changes: 108 additions & 4 deletions src/test/java/ch/ralscha/extdirectspring_itest/InfoServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
package ch.ralscha.extdirectspring_itest;

import static org.fest.assertions.api.Assertions.assertThat;
import static org.fest.assertions.data.MapEntry.entry;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.http.HttpEntity;
Expand All @@ -33,6 +35,7 @@
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.fest.assertions.data.MapEntry;
import org.junit.Test;

import ch.ralscha.extdirectspring.bean.api.Action;
Expand All @@ -47,6 +50,14 @@ public class InfoServiceTest extends JettyTest {
private static RemotingApi api() {
RemotingApi remotingApi = new RemotingApi("remoting", "/controller/router", null);
remotingApi.addAction("infoService", new Action("updateInfo", 0, true));
remotingApi.addAction("infoService", new Action("updateInfo2nd", 0, true));

remotingApi.addAction("infoService", new Action("updateInfoUser1", 0, true));
remotingApi.addAction("infoService", new Action("updateInfoUser2", 0, true));
remotingApi.addAction("infoService", new Action("updateInfoUser3", 0, true));
remotingApi.addAction("infoService", new Action("updateInfoUser4", 0, true));
remotingApi.addAction("infoService", new Action("updateInfoUser5", 0, true));

return remotingApi;
}

Expand Down Expand Up @@ -87,14 +98,108 @@ public void testApiFingerprinted() throws ClientProtocolException, IOException {
}

@Test
public void testPost() throws ClientProtocolException, IOException {
public void testPostFirst() throws ClientProtocolException, IOException {
testInfoPost("updateInfo");
}

@Test
public void testPostSecond() throws ClientProtocolException, IOException {
testInfoPost("updateInfo2nd");
}

@Test
public void testUpdateInfoUser1() throws ClientProtocolException, IOException {

Locale.setDefault(Locale.US);

testUserPost("updateInfoUser1", "not a well-formed email address", entry("lc", "ralph"),
entry("success", false));
}

@Test
public void testUpdateInfoUser2() throws ClientProtocolException, IOException {
Locale.setDefault(Locale.GERMAN);
testUserPost("updateInfoUser2", "keine gültige E-Mail-Adresse", entry("lc", "ralph"), entry("success", false));

}

@Test
public void testUpdateInfoUser3() throws ClientProtocolException, IOException {
Locale.setDefault(Locale.US);
testUserPost("updateInfoUser3", "Wrong E-Mail", entry("lc", "ralph"), entry("success", false));
}

@Test
public void testUpdateInfoUser4() throws ClientProtocolException, IOException {
Locale.setDefault(Locale.US);
testUserPost("updateInfoUser4", "Wrong E-Mail", entry("lc", "ralph"), entry("success", true));
}

@Test
public void testUpdateInfoUser5() throws ClientProtocolException, IOException {
Locale.setDefault(Locale.US);
testUserPost("updateInfoUser5", "Wrong E-Mail", entry("lc", "ralph"), entry("success", false));
}

private static void testUserPost(String method, String errorMsg, MapEntry... entries)
throws ClientProtocolException, IOException {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://localhost:9998/controller/router");

List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("extTID", "1"));
formparams.add(new BasicNameValuePair("extAction", "infoService"));
formparams.add(new BasicNameValuePair("extMethod", "updateInfo"));
formparams.add(new BasicNameValuePair("extMethod", method));
formparams.add(new BasicNameValuePair("extType", "rpc"));
formparams.add(new BasicNameValuePair("extUpload", "false"));
formparams.add(new BasicNameValuePair("name", "RALPH"));
formparams.add(new BasicNameValuePair("firstName", "firstName"));
formparams.add(new BasicNameValuePair("age", "1"));
formparams.add(new BasicNameValuePair("email", "invalidEmail"));

UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(formparams, "UTF-8");

post.setEntity(postEntity);

HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
assertThat(entity).isNotNull();
String responseString = EntityUtils.toString(entity);

ObjectMapper mapper = new ObjectMapper();
Map<String, Object> rootAsMap = mapper.readValue(responseString, Map.class);
assertThat(rootAsMap).hasSize(5);
assertThat(rootAsMap.get("method")).isEqualTo(method);
assertThat(rootAsMap.get("type")).isEqualTo("rpc");
assertThat(rootAsMap.get("action")).isEqualTo("infoService");
assertThat(rootAsMap.get("tid")).isEqualTo(1);

Map<String, Object> result = (Map<String, Object>) rootAsMap.get("result");

int resultSize = entries.length;
if (errorMsg != null) {
resultSize += 1;
}
assertThat(result).hasSize(resultSize);
assertThat(result).contains(entries);

Map<String, Object> errors = (Map<String, Object>) result.get("errors");
if (errorMsg != null) {
assertThat(errors).isNotNull();
assertThat(((List<String>) errors.get("email")).get(0)).isEqualTo(errorMsg);
} else {
assertThat(errors).isNull();
}
}

private static void testInfoPost(String method) throws ClientProtocolException, IOException {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://localhost:9998/controller/router");

List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("extTID", "1"));
formparams.add(new BasicNameValuePair("extAction", "infoService"));
formparams.add(new BasicNameValuePair("extMethod", method));
formparams.add(new BasicNameValuePair("extType", "rpc"));
formparams.add(new BasicNameValuePair("extUpload", "false"));
formparams.add(new BasicNameValuePair("userName", "RALPH"));
Expand All @@ -110,7 +215,7 @@ public void testPost() throws ClientProtocolException, IOException {
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> rootAsMap = mapper.readValue(responseString, Map.class);
assertThat(rootAsMap).hasSize(5);
assertThat(rootAsMap.get("method")).isEqualTo("updateInfo");
assertThat(rootAsMap.get("method")).isEqualTo(method);
assertThat(rootAsMap.get("type")).isEqualTo("rpc");
assertThat(rootAsMap.get("action")).isEqualTo("infoService");
assertThat(rootAsMap.get("tid")).isEqualTo(1);
Expand All @@ -119,7 +224,6 @@ public void testPost() throws ClientProtocolException, IOException {
assertThat(result).hasSize(2);
assertThat(result.get("user-name-lower-case")).isEqualTo("ralph");
assertThat(result.get("success")).isEqualTo(true);

}

}
17 changes: 17 additions & 0 deletions src/test/resources/messages.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright 2010-2012 Ralph Schaer <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

Email=Wrong E-Mail
11 changes: 10 additions & 1 deletion src/test/webapp/WEB-INF/controller-servlet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@

<mvc:annotation-driven />

<tx:annotation-driven/>
<tx:annotation-driven/>

<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:messages</value>
</list>
</property>
</bean>

</beans>

0 comments on commit d69aac7

Please sign in to comment.