Getting API name from Synapse handler - wso2

I am trying to implement a custom logger with Synapse handler, I can differentiate API and service but how can I get the API name from synapse handler code? Service name is __SynapseService for all APIs.
public static boolean isProxyService(MessageContext messageContext,
AxisService axisService) {
if (axisService != null) {
Parameter val = axisService.getParameter("serviceType");
if (val != null && val.getValue().toString().equalsIgnoreCase("Proxy")) {
if (logger.isDebugEnabled()) {
logger.debug("Parameter Value Service Type: "
+ val.getValue().toString());
}
return true;
}
}
return false;
}
public static boolean isAPI(MessageContext messageContext,
AxisService axisService) {
if(!isProxyService(messageContext, axisService) && axisService.getAxisServiceGroup().getServiceGroupName().equals(SynapseConstants.SYNAPSE_SERVICE_NAME)){
return true;
}
return false;
}
public static String getServiceName(MessageContext messageContext, AxisService axisService) {
org.apache.axis2.context.MessageContext msgContext = ((Axis2MessageContext) messageContext).getAxis2MessageContext();
return msgContext.getServiceContext().getName();
}

Try this.
String apiName = (String) messageContext.getProperty("SYNAPSE_REST_API");

Related

How to forward service?wsdl to service.wsdl in Spring WS

At the first sorry for my awful English. I have following piece of Spring WS configuration:
#Configuration
class WSConfig {
...
#Bean
Wsdl11Definition wsdlSchema() {
SimpleWsdl11Definition wsdl11Definition = new SimpleWsdl11Definition();
wsdl11Definition.setWsdl(new ClassPathResource("service.wsdl"));
return wsdl11Definition;
}
}
So I can get WSDL file using URL */service.wsdl.
Is it possible to add URL forwarding */service?wsdl --> */service.wsdl cause of some WS clients use URL */service?wsdl.
Possible solution is extending MessageDispatcherServlet
class CustomMessageDispatcherServlet extends MessageDispatcherServlet {
private static final String WSDL_SUFFIX_NAME = ".wsdl";
private Map<String, WsdlDefinition> wsdlDefinitions;
CustomMessageDispatcherServlet(ApplicationContext applicationContext) {
super();
setApplicationContext(applicationContext);
setTransformWsdlLocations(true);
setTransformSchemaLocations(false);
}
#Override
protected void initStrategies(ApplicationContext context) {
super.initStrategies(context);
initWsdlDefinitions(context);
}
private void initWsdlDefinitions(ApplicationContext context) {
wsdlDefinitions = BeanFactoryUtils
.beansOfTypeIncludingAncestors(
context, WsdlDefinition.class, true, false);
}
// here with dealing with "wsdl" parameter in HTTP GET request
#Override
protected WsdlDefinition getWsdlDefinition(HttpServletRequest request) {
if (HttpTransportConstants.METHOD_GET.equals(request.getMethod()) &&
(request.getRequestURI().endsWith(WSDL_SUFFIX_NAME) || request.getParameter("wsdl") != null)) {
String fileName = WebUtils.extractFilenameFromUrlPath(request.getRequestURI());
return wsdlDefinitions.get(fileName);
} else {
return null;
}
}
}

WSO25.3 Custom User store manager

I am on WSO2 5.3
My usecase is to invoke the api to authenticate the user and return isAuthenticated as true/false based on credentials provided.
I have written a custom user store ( as MyAPIUserStoreManager which extends JDBCUserStoreManager) and placed into the droppins folder, and i can see this in drop down as well after restarting the server,
Then I also configured the travelocity app as a service provider as i want to login to this app by using credentials which will be validated using api(custom user store i.e MyAPIUserStoreManager).
This works fine in wso25.2 version :
but on WSO25.3 my problem is that clicking on login button after providing the username/pwd, in log it says the following :
TID: [-1234] [] [2017-01-23 16:05:10,673] ERROR {org.wso2.carbon.user.core.common.AbstractUserStoreManager} - java.lang.ClassCastException: org.wso2.carbon.utils.Secret cannot be cast to java.lang.String
TID: [-1234] [] [2017-01-23 16:05:10,676] DEBUG {org.wso2.carbon.identity.application.authenticator.basicauth.BasicAuthenticator} - User authentication failed due to invalid credentials
It is not going through my custom user store i.e MyAPIUserStoreManager somehow.
Am I missing any config ? I have followed the followed link :
https://docs.wso2.com/display/ADMIN44x/Writing+a+Custom+User+Store+Manager#WritingaCustomUserStoreManager-AbstractUserStoreManagerandimplementations
Here is the code for custom user store manager:
public class MyAPIUserStoreManager extends JDBCUserStoreManager{
private static Log log = LogFactory.getLog(MyAPIUserStoreManager.class);
Map<String,String> userProperties;
public MyAPIUserStoreManager() {
}
public MyAPIUserStoreManager(RealmConfiguration realmConfig, Map<String, Object> properties,
ClaimManager claimManager, ProfileConfigurationManager profileManager, UserRealm realm,
Integer tenantId) throws UserStoreException {
super(realmConfig, properties, claimManager, profileManager, realm, tenantId);
}
#Override
public boolean doAuthenticate(String userName, Object credential) throws UserStoreException {
boolean isAuthenticated = false;
if (userName == null || credential == null) {
return false;
}
userName = userName.trim();
String password = (String) credential;
password = password.trim();
if (userName.equals("") || password.equals("")) {
return false;
}
Map<String, String> properties = initUserProperties(userName, password);
if (userName.equals(properties.get("loginName"))) {
isAuthenticated = true;
}
return isAuthenticated;
}
#Override
public Map<String, String> getUserPropertyValues(String username, String[] propertyNames,
String profileName) throws UserStoreException {
Map<String,String> map = new HashMap<>();
if (userProperties == null) {
log.warn("User property values not initialized for " + username + ", returning null");
return null;
}
for (String propertyName : propertyNames) {
if ("accountId".equals(propertyName)) {
map.put(propertyName, userProperties.get("accountId"));
} else if ("userStatusID".equals(propertyName)) {
map.put(propertyName, userProperties.get("userStatusID"));
} else if ("loginName".equals(propertyName)) {
map.put(propertyName, userProperties.get("loginName"));
} else if ("firstName".equals(propertyName)) {
map.put(propertyName, userProperties.get("firstName"));
} else if ("lastName".equals(propertyName)) {
map.put(propertyName, userProperties.get("lastName"));
} else if ("email".equals(propertyName)) {
map.put(propertyName, userProperties.get("email"));
} else if ("phoneNumber".equals(propertyName)) {
map.put(propertyName, userProperties.get("phoneNumber"));
} else if ("role".equals(propertyName)) {
map.put(propertyName, userProperties.get("role"));
} else if ("roleId".equals(propertyName)) {
map.put(propertyName, userProperties.get("roleId"));
} else if ("secretQuestionId".equals(propertyName)) {
map.put(propertyName, userProperties.get("secretQuestionId"));
} else if ("secretAnswer".equals(propertyName)) {
map.put(propertyName, userProperties.get("secretAnswer"));
} else if ("dateLastUpdated".equals(propertyName)) {
map.put(propertyName, userProperties.get("dateLastUpdated"));
} else if ("lastUpdatedByUserId".equals(propertyName)) {
map.put(propertyName, userProperties.get("lastUpdatedByUserId"));
} else if ("password".equals(propertyName)) {
map.put(propertyName, userProperties.get("password"));
} else if ("existingsuperuser".equals(propertyName)) {
map.put(propertyName, userProperties.get("existingsuperuser"));
} else if ("updateMeWithAnnouncements".equals(propertyName)) {
map.put(propertyName, userProperties.get("updateMeWithAnnouncements"));
} else if ("blockAccess".equals(propertyName)) {
map.put(propertyName, userProperties.get("blockAccess"));
} else if ("allowEndUserOutboundCallerId".equals(propertyName)) {
map.put(propertyName, userProperties.get("allowEndUserOutboundCallerId"));
} else if ("allowCallBlocking".equals(propertyName)) {
map.put(propertyName, userProperties.get("allowCallBlocking"));
} else if ("passExpiry".equals(propertyName)) {
map.put(propertyName, userProperties.get("passExpiry"));
} else if ("passhash".equals(propertyName)) {
map.put(propertyName, userProperties.get("passhash"));
} else if ("salt".equals(propertyName)) {
map.put(propertyName, userProperties.get("salt"));
} else if ("passHistory".equals(propertyName)) {
map.put(propertyName, userProperties.get("passHistory"));
} else if ("passAlgo".equals(propertyName)) {
map.put(propertyName, userProperties.get("passAlgo"));
} else if ("sendEmail".equals(propertyName)) {
map.put(propertyName, userProperties.get("sendEmail"));
} else if ("contactnumbers".equals(propertyName)) {
map.put(propertyName, userProperties.get("contactnumbers"));
}
}
return map;
}
#Override
public org.wso2.carbon.user.api.Properties getDefaultUserStoreProperties() {
return MyAPIUserConstants.getDefaultUserStoreProperties();
}
#Override
public String[] getAllProfileNames() throws UserStoreException {
return new String[]{"default"};
}
#Override
public String[] getProfileNames(String userName) throws UserStoreException {
return new String[]{"default"};
}
public boolean isMultipleProfilesAllowed() {
return false;
}
public boolean isReadOnly() throws UserStoreException {
return true;
}
private Map<String,String> initUserProperties(String userName, String password) throws UserStoreException {
userProperties = new HashMap<>();
String url = realmConfig.getUserStoreProperty(MyAPIUserConstants.LOGIN_API);
if (url == null) {
throw new UserStoreException("Authentication API not defined");
}
String params = URLEncoder.encode(userName + "," + password);
url = String.format(url, params);
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
log.debug("Lets see headers");
request.addHeader("Authorization", "Basic ffgggggddddd"); //hard coding as of now
HttpResponse response;
String xmlResponse = null;
try {
log.debug("Authorization header is "+request.getFirstHeader("Authorization"));
response = client.execute(request);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity resEntity = response.getEntity();
xmlResponse = EntityUtils.toString(resEntity);
xmlResponse = MyApiUserStoreUtils.trim(xmlResponse);
}
} catch (IOException e) {
e.printStackTrace();
}
if (StringUtils.isNotEmpty(xmlResponse)) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
InputSource is;
try {
builder = factory.newDocumentBuilder();
is = new InputSource(new StringReader(xmlResponse));
Document doc = builder.parse(is);
userProperties.put("accountId", doc.getElementsByTagName("accountId").item(0).getTextContent());
userProperties.put("userStatusID", doc.getElementsByTagName("userStatusID").item(0).getTextContent());
userProperties.put("loginName", doc.getElementsByTagName("loginName").item(0).getTextContent());
userProperties.put("firstName", doc.getElementsByTagName("firstName").item(0).getTextContent());
userProperties.put("lastName", doc.getElementsByTagName("lastName").item(0).getTextContent());
userProperties.put("email", doc.getElementsByTagName("email").item(0).getTextContent());
userProperties.put("phoneNumber", doc.getElementsByTagName("phoneNumber").item(0).getTextContent());
userProperties.put("role", doc.getElementsByTagName("role").item(0).getTextContent());
userProperties.put("roleId", doc.getElementsByTagName("roleId").item(0).getTextContent());
userProperties.put("secretQuestionId", doc.getElementsByTagName("secretQuestionId").item(0).getTextContent());
userProperties.put("secretAnswer", doc.getElementsByTagName("secretAnswer").item(0).getTextContent());
userProperties.put("dateLastUpdated", doc.getElementsByTagName("dateLastUpdated").item(0).getTextContent());
userProperties.put("lastUpdatedByUserId", doc.getElementsByTagName("lastUpdatedByUserId").item(0).getTextContent());
userProperties.put("password", doc.getElementsByTagName("password").item(0).getTextContent());
userProperties.put("existingsuperuser", doc.getElementsByTagName("existingsuperuser").item(0).getTextContent());
userProperties.put("updateMeWithAnnouncements", doc.getElementsByTagName("updateMeWithAnnouncements").item(0).getTextContent());
userProperties.put("blockAccess", doc.getElementsByTagName("blockAccess").item(0).getTextContent());
userProperties.put("allowEndUserOutboundCallerId", doc.getElementsByTagName("allowEndUserOutboundCallerId").item(0).getTextContent());
userProperties.put("allowCallBlocking", doc.getElementsByTagName("allowCallBlocking").item(0).getTextContent());
userProperties.put("passExpiry", doc.getElementsByTagName("passExpiry").item(0).getTextContent());
userProperties.put("passhash", doc.getElementsByTagName("passhash").item(0).getTextContent());
userProperties.put("salt", doc.getElementsByTagName("salt").item(0).getTextContent());
userProperties.put("passHistory", doc.getElementsByTagName("passHistory").item(0).getTextContent());
userProperties.put("passAlgo", doc.getElementsByTagName("passAlgo").item(0).getTextContent());
userProperties.put("sendEmail", doc.getElementsByTagName("sendEmail").item(0).getTextContent());
String contactNumbers = "";
for (int i = 0 ; i < doc.getElementsByTagName("contactnumbers").getLength(); i++) {
if (StringUtils.isNotEmpty(contactNumbers)) {
contactNumbers += ",";
}
contactNumbers += doc.getElementsByTagName("contactnumbers").item(i).getTextContent();
}
userProperties.put("contactnumbers", contactNumbers);
} catch (ParserConfigurationException e) {
throw new UserStoreException("Error while initializing document builder", e);
} catch (IOException e) {
throw new UserStoreException("Error while parsing Input source", e);
} catch (org.xml.sax.SAXException e) {
throw new UserStoreException("Error while parsing Input source", e);
}
}
return userProperties;
}
protected Connection getDBConnection() throws SQLException, UserStoreException {
return null;
}
protected boolean isExistingJDBCRole(RoleContext context) throws UserStoreException {
return false;
}
public boolean doCheckExistingUser(String userName) throws UserStoreException {
if (userProperties != null) {
return true;
} else {
return false;
}
}
public String[] getUserListOfJDBCRole(RoleContext ctx, String filter) throws UserStoreException {
String [] user = null;
if (userProperties != null) {
user = new String[]{userProperties.get("loginName")};
}
return user;
}
public RoleDTO[] getRoleNamesWithDomain(boolean noHybridRoles) throws UserStoreException {
return null;
}
public String[] doGetExternalRoleListOfUser(String userName, String filter) throws UserStoreException {
return null;
}
#Override
protected String[] doGetSharedRoleListOfUser(String userName,
String tenantDomain, String filter) throws UserStoreException {
return null;
}
public String[] getRoleListOfUser(String userName) throws UserStoreException {
return new String[]{"Internal/everyone"};
}
public boolean isRecurssive() {
return false;
}
}
Please suggest.
Here is the log after i click on login button:
TID: [-1234] [] [2017-01-24 16:35:30,315] DEBUG {org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils} - Authentication Context is null
TID: [-1234] [] [2017-01-24 16:35:30,318] DEBUG {org.wso2.carbon.identity.application.authentication.framework.handler.request.impl.DefaultAuthenticationRequestHandler} - In authentication flow
TID: [-1234] [] [2017-01-24 16:35:30,318] DEBUG {org.wso2.carbon.identity.application.authentication.framework.handler.sequence.impl.DefaultStepBasedSequenceHandler} - Executing the Step Based Authentication...
TID: [-1234] [] [2017-01-24 16:35:30,318] DEBUG {org.wso2.carbon.identity.application.authentication.framework.handler.sequence.impl.DefaultStepBasedSequenceHandler} - Starting Step: 1
TID: [-1234] [] [2017-01-24 16:35:30,319] DEBUG {org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils} - Finding already authenticated IdPs of the Step
TID: [-1234] [] [2017-01-24 16:35:30,320] DEBUG {org.wso2.carbon.identity.application.authentication.framework.handler.step.impl.DefaultStepHandler} - Receive a response from the external party
TID: [-1234] [] [2017-01-24 16:35:30,320] DEBUG {org.wso2.carbon.identity.application.authentication.framework.handler.step.impl.DefaultStepHandler} - BasicAuthenticator can handle the request.
TID: [-1234] [] [2017-01-24 16:35:30,325] ERROR {org.wso2.carbon.user.core.common.AbstractUserStoreManager} - java.lang.ClassCastException: org.wso2.carbon.utils.Secret cannot be cast to java.lang.String
TID: [-1234] [] [2017-01-24 16:35:30,327] DEBUG {org.wso2.carbon.user.core.common.AbstractUserStoreManager} - Authentication failure. Wrong username or password is provided.
TID: [-1234] [] [2017-01-24 16:35:30,328] DEBUG {org.wso2.carbon.identity.application.authenticator.basicauth.BasicAuthenticator} - User authentication failed due to invalid credentials
You can add debug logs as follows in /repository/conf/log4j.properties file
log4j.logger.org.wso2.carbon.user.core=DEBUG
log4j.logger.org.wso2.carbon.identity.application.authenticator.basicauth.BasicAuthenticator=DEBUG
Attach the wso2carbon.log file.
Better if you can attach your custom as well.
I think you can write a custom authenticator for your use-case.
Thanks
Isura

Interceptors doesn't work in Unit tests

I am get Interceptors working on the application server. I have annotated EJB:
#Stateless
#Named("accountsEJB")
public class AccountsEJB {
#PersistenceContext(unitName = "weducationPU")
private EntityManager em;
// . . . other methods
#WithLog
#Restricted(allowedRoles = {}) // Allowed only for admin
public Account save(Account item) {
if (item.getId() == 0) {
em.persist(item);
return item;
} else {
return em.merge(item);
}
}
#WithLog
#Restricted(allowedRoles = {}) // Allowed only for admin
public void delete(final Account item) {
Account a = em.find(Account.class, item.getId());
if (null != a) {
em.remove(a);
}
}
}
Empty list of roles means, that It's allowed only for role admin.
Here the unit test file for this EJB
public class AccountsEJBTest {
private static EJBContainer container;
private static AccountsEJB ejb;
#BeforeClass
public static void setUpClass() {
try {
Map<String, Object> properties = new HashMap<>();
properties.put(EJBContainer.MODULES, new File("target/classes"));
properties.put("org.glassfish.ejb.embedded.glassfish.installation.root", "glassfish");
properties.put(EJBContainer.APP_NAME, "weducation");
container = EJBContainer.createEJBContainer(properties);
ejb = (AccountsEJB) container.getContext().lookup("java:global/weducation/classes/AccountsEJB");
System.out.println("AccountsEJBTest running...");
} catch (NamingException e) {
fail("Container init error: " + e.getMessage());
}
}
#AfterClass
public static void tearDownClass() {
if (null != container) {
container.close();
}
System.out.println("AccountsEJBTest finished");
}
private boolean equals(Account source, Account result) {
if (!source.getFullName().contentEquals(result.getFullName())) return false;
if (!source.getLogin().contentEquals(result.getLogin())) return false;
return source.getRole() == result.getRole();
}
#Test
public void testOperations() {
try {
System.out.println("-->testOperations()");
Account testAccount = new Account();
testAccount.setFullName("Test Account");
testAccount.setLogin("test");
testAccount.setPassword("test");
testAccount.setConfirm("test");
testAccount.updatePassword();
testAccount.setRole(AccountRole.DEPOT);
Account savedAccount = ejb.save(testAccount);
assertTrue(equals(testAccount, savedAccount));
savedAccount.setFullName("Still Test Account");
savedAccount.setLogin("test1");
testAccount = ejb.save(savedAccount);
assertTrue(equals(testAccount, savedAccount));
testAccount.setPassword("testpwd");
testAccount.setConfirm("testpwd");
testAccount.updatePassword();
savedAccount = ejb.save(testAccount);
assertTrue(equals(testAccount, savedAccount));
ejb.delete(savedAccount);
} catch (Exception e) {
fail("Exception class " + e.getClass().getName() + " with message " + e.getMessage());
}
}
}
And this test working. I think, that is not correct, because there is no user with admin role logged in. But why this behavior happing?
UPDATED.
#Restricted interface:
#Inherited
#InterceptorBinding
#Target({METHOD, TYPE})
#Retention(RUNTIME)
public #interface Restricted {
#Nonbinding
AccountRole[] allowedRoles();
}
SecurityInterceptor class
#Interceptor
#Restricted(allowedRoles = {})
public class SecurityInterceptor implements Serializable {
#Inject
private transient SessionMB session;
#AroundInvoke
public Object checkSecurity(InvocationContext context) throws Exception {
//System.out.println("Security checker started.");
if ((session == null) || (session.getUser() == null)) {
throw new SecurityException("Can't get user info");
}
// Allow all to admin
if (session.isAdmin()) {
//System.out.println("It's admin.");
return context.proceed();
}
// walk non administrator roles
for (AccountRole r : getAllowedRoles(context.getMethod())) {
// if match - accept method invocation
if (session.getUser().getRole() == r) {
//System.out.println("It's " + r.getDescription());
return context.proceed();
}
}
throw new SecurityException(session.getUser().getFullName()
+ " has no souch privilegies ");
}
private AccountRole[] getAllowedRoles(Method m) {
if (null == m) {
throw new IllegalArgumentException("Method is null!");
}
// Walk all method annotations
for (Annotation a : m.getAnnotations()) {
if (a instanceof Restricted) {
return ((Restricted) a).allowedRoles();
}
}
// Now - walk all class annotations
if (null != m.getDeclaringClass()) {
for (Annotation a : m.getDeclaringClass().getAnnotations()) {
if (a instanceof Restricted) {
return ((Restricted) a).allowedRoles();
}
}
}
// if no annotaion found
throw new RuntimeException("Annotation #Restricted not found at method "
+ m.getName() + " or it's class.");
}
}
The beans.xml is placed in WEB-INF folder and looks like
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="annotated">
<interceptors>
<class>ru.edu.pgtk.weducation.interceptors.LogInterceptor</class>
<class>ru.edu.pgtk.weducation.interceptors.SecurityInterceptor</class>
</interceptors>
</beans>
Can someone help me to know:
How to get Interceptors working in Unit tests?
How to start authorized session in Unit tests (log in as admin, for example)?
How to test such operations as creation and deleting account with the different tests (one test for creating, one for deleting)? Is it correct - to test all operations in one test?
Thank you for your time and your questions.

JMockit - mock CXF webservice port object

I'm trying to unit test a webservice wrapper class which attempts to hide all the webservice implementation details. It's called from a scripting platform so all interfaces are simple String or integers. The class provides a static initialise method which takes the hostname and port number and uses this to create a private static Apache CXF IRemote port instance (generated from CXF wsdl2java). Subsequent calls to a static business method delegate to the port instance.
How can I use JMockit to mock the CXF port stub when unit testing the static wrapper class?
public class WebServiceWrapper {
private static final QName SERVICE_NAME = new QName("http://www.gwl.org/example/service",
"ExampleService");
private static IRemoteExample _port = null;
public static final String initialise(String url) {
if(_port != null) return STATUS_SUCCESS;
try {
URL wsdlURL = new URL(url);
ExampleService svc = new ExampleService(wsdlURL, SERVICE_NAME);
_port = svc.getIRemoteExamplePort();
BindingProvider bp = (BindingProvider)_port;
Map<String, Object> context = bp.getRequestContext();
context.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
return STATUS_SUCCESS;
}
catch(MalformedURLException ex) {
return STATUS_ERROR_PREFIX + ex.getMessage();
}
catch(WebServiceException ex) {
return STATUS_ERROR_PREFIX + ex.getMessage();
}
}
public static final String businessMethod(String arg) {
if(_port == null) {
return STATUS_ERROR_PREFIX + "Attempted to call businessMethod before connection is initialised. Pease call initialise first.";
}
try {
BusinessMethodRequest request = new BusinessMethodRequest ();
BusinessThing thing = new BusinessThing();
thing.setValue(arg);
request.setThing(thing);
BusinessMethodResponse response = _port.businessMethod(request);
String result = response.getResult();
if(result == null) {
return STATUS_ERROR_PREFIX + "Null returned!";
}
return STATUS_SUCCESS;
}
catch(MyBusinessException_Exception ex) {
return STATUS_ERROR_PREFIX + ex.getFaultInfo().getReason();
}
catch(WebServiceException ex) {
return STATUS_ERROR_PREFIX + ex.getMessage();
}
}
Example behaviour of the webservice would be, if I pass the value "OK" then it returns a success message, but if I call with a value of "DUPLICATE" then the webservice would throw a MyBusinessException_Exception.
I think I have managed to mock the _port object, but the business call always returns a null Response object so I suspect that my Expectations does not define the "BusinessThing" object correctly. My Test method so far.
#Test
public void testBusinessMethod(#Mocked final IRemoteExample port) {
new NonStrictExpectations() {
#Capturing IRemoteExample port2;
{
BusinessThing thing = new BusinessThing();
thing.setValue("OK");
BusinessMethodRequest req = new BusinessMeothdRequest();
req.setThing(thing);
BusinessMethodResponse resp = new BusinessMethodResponse ();
resp.setResult("SUCCESS");
try {
port.businessMethod(req);
returns(resp);
}
catch(MyBusinessException_Exception ex) {
returns(null);
}
Deencapsulation.setField(WebServiceWrapper.class, "_port", port);
}
};
String actual = WebServiceWrapper.businessMethod("OK");
assertEquals(WebServiceWrapper.STATUS_SUCCESS, actual);
}
Seems to be working as follows.
Added a custom Matcher class for my BusinessMethodRequest
class BusinessMethodRequestMatcher extends TypeSafeMatcher<BusinessMethodRequest> {
private final BusinessMethodRequestexpected;
public BusinessMethodRequestMatcher(BusinessMethodRequest expected) {
this.expected. = expected;
}
#Override
public boolean matchesSafely(BusinessMethodRequest actual) {
// could improve with null checks
return expected.getThing().getValue().equals(actual.getThing().getValue());
}
#Override
public void describeTo(Description description) {
description.appendText(expected == null ? null : expected.toString());
}
}
Then use "with" in my Expectation.
try {
port.createResource(with(req, new BusinessMethodRequestMatcher(req)));
returns(resp);
}
The mock object now recognises the business method call with the correct parameter and returns the expected response object.

Is it possible to validate a value before it is set with Eclipse EMF validation framework?

The way I understand how eclipse validation framework works:
generate an object with a factory
set a value for an attribute in this object
check validation
For example:
public class ValidateNameTest {
public static void main(String[] args) {
ExtlibraryPackageImpl.init();
ExtlibraryFactory factory = ExtlibraryFactory.eINSTANCE;
Writer writer = factory.createWriter();
// comment next line for false result in console
writer.setName("test");
writer.setFirstName("test");
writer.setLastName("test");
boolean isNull = (writer.getName() == null) ? true : false;
System.out.println("writer name is null : " + isNull);
boolean result = validateObject(writer);
System.err.println("result = " + result);
boolean result2 = validateObject2(writer);
System.err.println("result2 = " + result2);
boolean result3 = validateObject3(writer);
System.err.println("result3 = " + result3);
boolean result4 = validateObject5(writer);
System.out.println("result4 = " + result4);
}
public static boolean validateObject(Writer writer) {
ExtlibraryValidator validator = ExtlibraryValidator.INSTANCE;
if (!validator.validateWriter_hasValidName(writer, null, null)) {
return false;
}
return true;
}
public static boolean validateObject2(EObject eObject) {
EValidator validator = EValidator.Registry.INSTANCE
.getEValidator(eObject.eClass().getEPackage());
if (validator != null) {
if (!validator.validate(eObject, null, null)) {
return false;
}
}
return true;
}
public static boolean validateObject3(EObject eObject) {
Diagnostic diagnostic = Diagnostician.INSTANCE.validate(eObject);
return diagnostic.getSeverity() == Diagnostic.OK;
}
public static boolean validateObject5(EObject eObject)
{
Diagnostic diagnostic = Diagnostician.INSTANCE.validate(eObject);
if (diagnostic.getSeverity() == Diagnostic.ERROR || diagnostic.getSeverity() == Diagnostic.WARNING)
{
System.err.println(diagnostic.getMessage());
for (Diagnostic childDiagnostic : diagnostic.getChildren())
{
switch (childDiagnostic.getSeverity())
{
case Diagnostic.ERROR:
case Diagnostic.WARNING:
System.err.println("\t" + childDiagnostic.getMessage());
}
}
return false;
}
return true;
}
}
But I want to check if a value is valid for the model before I call the setter for the attribute. Is this possible with Eclipse EMF validation framework? Can somebody give an example please?
I know of one common use case where this is possible: Data binding between model and UI controls.
When you establish the EMF data binding between your model and the user interface, you can validate user input as follows. Create an update strategy (target to model) and override the method validateBeforeSet(Object). Here is an example:
EMFDataBindingContext ctx = new EMFDataBindingContext();
ISWTObservableValue notesObservableValue = prop.observe(swtTextViewer);
IEMFValueProperty notesValueProperty = EMFProperties.value(ModelPackage.Literals.THING__NOTES);
UpdateValueStrategy targetToModel = new UpdateValueStrategy() {
#Override
public IStatus validateBeforeSet(Object value) {
if ("".equals(value)) {
MessageDialog.openError(Display.getCurrent()
.getActiveShell(), "Error",
"You should supply a description");
return ValidationStatus
.error("You should supply a description");
}
return super.validateBeforeSet(value);
}
};
ctx.bindValue(notesObservableValue,
notesValueProperty.observe(thing), targetToModel,
new UpdateValueStrategy());