package com.redheap.selenium.domain;
import java.util.ArrayList;
/**
* This class acts as a domain object representing a single facesmessage.
* <p>
* It is tightly coupled with {@link com.redheap.selenium.domain.PageMessageWrapper PageMessageWrapper} as it needs to
* parse our JavaScript Array. (Move the constants here for more encapsulation?)
*/
public class FacesMessageDomain {
private static final int JS_ARRAY_MESSAGETYPE_INDEX = 0;
private static final int JS_ARRAY_SEVERITY_INDEX = 1;
private static final int JS_ARRAY_SUMMARY_INDEX = 2;
private static final int JS_ARRAY_DETAIL_INDEX = 3;
private String componentId;
private Long messageType;
private Long severity;
private String summary;
private String detail;
/**
* Javascript method that gets all messages from the AdfPage and converts it to arraytype.
* <p>
* This is needed as Selenium can't handle complex types.
*/
private static final String JS_FUNCTION_CONVERTPAGEMESSAGESTOARRAY =
"" + "function convertPageMessagesToArray(pageMessages)\n" + "{\n" + " var retPageMessages=[];\n" +
" var isEmpty = true;\n" + " \n" +
" //check if 'pageMessages' object is empty (there is no simple empty check in javascript)\n" +
" for(var prop in pageMessages) {\n" + " if(pageMessages.hasOwnProperty(prop)){\n" +
" isEmpty = false;\n" + " break;\n" + " } \n" + " }\n" + " if (!isEmpty) {\n" +
" var retPageProperties=[];\n" + " \n" + " for (var key in pageMessages){\n" +
" var componentMessages = convertComponentMessagesToArray(pageMessages[key]);\n" +
" //array position 0 = key\n" + " //array position 1 = array with facesMessages \n" +
" retPageMessages.push([key,componentMessages]);\n" + " }\n" + " }\n" + " \n" +
" return retPageMessages;\n" + "};";
/**
* Javascript method that gets all messages from the AdfPage and converts it to arraytype.
* <p>
* This is needed as Selenium can't handle complex types.
*/
private static final String JS_FUNCTION_CONVERTCOMPONENTMESSAGESTOARRAY =
"" + "function convertComponentMessagesToArray(compMessages)\n" + "{\n" + " var returnFacesMessages=[];\n" +
" if (compMessages) { \n" + " compMessages.forEach(function(compMessageItem) {\n" +
" var facesMessage=[];\n" + " //0 = messageType\n" + " var messageType;\n" +
" if (compMessageItem._messageType) {\n" + " messageType = compMessageItem.getMessageType();\n" +
" }\n" + " facesMessage.push(messageType);\n" + " //1 = severity\n" +
" facesMessage.push(compMessageItem.getSeverity());\n" + " //2 = summary\n" +
" facesMessage.push(compMessageItem.getSummary());\n" + " //3 = detail\n" +
" facesMessage.push(compMessageItem.getDetail());\n" + " returnFacesMessages.push(facesMessage);\n" +
" });\n" + " }\n" + " \n" + " return returnFacesMessages;\n" + "};";
public static final String JS_GET_PAGE_MESSAGES =
JS_FUNCTION_CONVERTPAGEMESSAGESTOARRAY + JS_FUNCTION_CONVERTCOMPONENTMESSAGESTOARRAY +
"var messagesPage = AdfPage.PAGE.getAllMessages();return convertPageMessagesToArray(messagesPage);";
/**
* Default constructor.
*/
public FacesMessageDomain() {
super();
}
/**
* Custom constructor that parses our custom ArrayList.
* <p>
* The constructor will fill the attributes based on the position in the list as defined by the constants.
* If the ArrayList of facesMessages doesn't contain the proper amount of items the method will throw an
* {@link IllegalArgumentException}
* @param componentId The component id of this facesmessage
* @param facesMessages The arraylist with a facesmessage generated by the execute of our JavaScript by Selenium
*/
public FacesMessageDomain(String componentId, ArrayList facesMessages) {
super();
if (facesMessages != null && facesMessages.size() == 4) {
this.componentId = componentId;
this.messageType = (Long) facesMessages.get(JS_ARRAY_MESSAGETYPE_INDEX);
this.severity = (Long) facesMessages.get(JS_ARRAY_SEVERITY_INDEX);
this.summary = (String) facesMessages.get(JS_ARRAY_SUMMARY_INDEX);
this.detail = (String) facesMessages.get(JS_ARRAY_DETAIL_INDEX);
} else {
throw new IllegalArgumentException("Javascript object has unsupported number of elements");
}
}
public void setComponentId(String componentId) {
this.componentId = componentId;
}
public String getComponentId() {
return componentId;
}
public void setSeverity(Long severity) {
this.severity = severity;
}
public Long getSeverity() {
return severity;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getSummary() {
return summary;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getDetail() {
return detail;
}
public void setMessageType(Long messageType) {
this.messageType = messageType;
}
public Long getMessageType() {
return messageType;
}
}