Java tutorial
/** * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com * * The software in this package is published under the terms of the CPAL v1.0 * license, a copy of which has been included with this distribution in the * LICENSE.md file. */ package org.mule.modules.sugarcrm.automation.system; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import com.sugarcrm.sugarcrm.EntryValue; import com.sugarcrm.sugarcrm.GetAvailableModulesRequestType; import com.sugarcrm.sugarcrm.GetEntriesCountRequestType; import com.sugarcrm.sugarcrm.GetEntriesCountResult; import com.sugarcrm.sugarcrm.GetEntriesRequestType; import com.sugarcrm.sugarcrm.GetEntryListResultVersion2; import com.sugarcrm.sugarcrm.GetEntryRequestType; import com.sugarcrm.sugarcrm.GetEntryResultVersion2; import com.sugarcrm.sugarcrm.GetModuleFieldsRequestType; import com.sugarcrm.sugarcrm.GetRelationshipsRequestType; import com.sugarcrm.sugarcrm.ListToSendByCxf; import com.sugarcrm.sugarcrm.ModuleList; import com.sugarcrm.sugarcrm.NameValue; import com.sugarcrm.sugarcrm.NewModuleFields; import com.sugarcrm.sugarcrm.NewSetEntriesResult; import com.sugarcrm.sugarcrm.NewSetEntryResult; import com.sugarcrm.sugarcrm.NewSetRelationshipListResult; import com.sugarcrm.sugarcrm.ReturnSearchResult; import com.sugarcrm.sugarcrm.SearchByModuleRequestType; import com.sugarcrm.sugarcrm.SearchLinkNameValue; import com.sugarcrm.sugarcrm.SelectFields; import com.sugarcrm.sugarcrm.SetEntriesRequestType; import com.sugarcrm.sugarcrm.SetEntryRequestType; import com.sugarcrm.sugarcrm.SetRelationshipRequestType; import com.sugarcrm.sugarcrm.SetRelationshipsRequestType; import java.util.Arrays; import java.util.List; import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xmlsoap.schemas.soap.encoding.Array; public class SugarCrmClientDriverTestCases extends AbstractSugarCrmTestDriver { private static final String RELATION_CONTACT_OPPORTUNITY = "opportunities"; private static final String OPORTUNITY_ID = "35911afd-977e-4ef1-7b14-4fd77e963c08"; private static final String CONTACT_ID = "108ae99e-64ae-9502-588c-4fd77e5454c9"; private static final String OTHER_OPPORTUNITY_ID = "1fbb06d3-87ce-f092-47ce-4fd77e846d73"; private static final String MODULE = "Contacts"; private static final Logger logger = LoggerFactory.getLogger(SugarCrmClientDriverTestCases.class); @Test @Ignore public void testGetEntryList() { GetEntryListResultVersion2 response = client.getEntryList(createRequestEntryList(MODULE)).getReturn(); assertNotNull(response); assertTrue(response.getResultCount() != 0); assertEquals(5, response.getResultCount()); logger.trace("response-getEntryList: {}", ReflectionToStringBuilder.toString(response)); for (Object entry : response.getEntryList().getAny()) { logger.trace("response-getEntryList-entryList-element: {}", ReflectionToStringBuilder.toString(entry)); } } @Test @Ignore public void testCount() { GetEntriesCountRequestType request = new GetEntriesCountRequestType(); request.setModuleName(MODULE); request.setSession(getSession()); GetEntriesCountResult countEntries = client.getCountEntries(request).getReturn(); assertNotNull(countEntries); assertTrue(countEntries.getResultCount() != 0); logger.trace("response-getCountEntries: {}", ReflectionToStringBuilder.toString(countEntries)); } @Test @Ignore public void testGetEntries() { SelectFields value = new SelectFields(); value.setArrayType(""); GetEntriesRequestType request = new GetEntriesRequestType(); request.setModuleName(MODULE); request.setSession(getSession()); ListToSendByCxf<String> fields = new ListToSendByCxf<String>(); fields.getElements().addAll(Arrays.asList("id", "name", "first_name", "last_name", "modified_by_name")); request.setSelectFields(fields); ListToSendByCxf<String> ids = new ListToSendByCxf<String>(); ids.getElements().addAll( Arrays.asList("108ae99e-64ae-9502-588c-4fd77e5454c9", "10d8cd10-e7b4-b817-1fc3-4fd77f69f9df")); request.setIds(ids); GetEntryResultVersion2 entries = client.getEntries(request).getReturn(); assertNotNull(entries); assertTrue(entries.getEntryList().getAny().size() != 0); logger.trace("response-getEntries: {}", ReflectionToStringBuilder.toString(entries)); } @Test @Ignore public void testGetModuleFields() { GetModuleFieldsRequestType request = new GetModuleFieldsRequestType(); request.setModuleName(MODULE); request.setSession(getSession()); request.setFields(new ListToSendByCxf<String>()); NewModuleFields response = client.getModuleFields(request).getReturn(); assertEquals(MODULE, response.getModuleName()); logger.trace("response-getModuleFields: {}", ReflectionToStringBuilder.toString(response)); for (Object element : response.getModuleFields().getAny()) { logger.trace("response-getModuleFields-fields: {}", ReflectionToStringBuilder.toString(element)); } } @Test @Ignore public void testGetAvailableModules() { GetAvailableModulesRequestType request = new GetAvailableModulesRequestType(); request.setSession(getSession()); ModuleList response = client.getAvailableModules(request).getReturn(); for (Object element : response.getModules().getAny()) { logger.trace("response-getAvailableModules: {}", ReflectionToStringBuilder.toString(element)); } } @Test @Ignore public void testSearchByModule() { SearchByModuleRequestType request = new SearchByModuleRequestType(); request.setSession(getSession()); request.setMaxResults(5); request.setAssignedUserId(""); request.setSearchString("%Doe%"); ListToSendByCxf<String> modules = new ListToSendByCxf<String>(); modules.getElements().addAll(Arrays.asList(MODULE, "OTHER")); request.setSelectFields(new ListToSendByCxf<String>()); request.setModules(modules); ReturnSearchResult response = client.searchByModule(request).getReturn(); SearchLinkNameValue searchLinkNameValueList = (SearchLinkNameValue) response.getEntryList().getAny().get(0); Assert.assertEquals(5, searchLinkNameValueList.getRecords().getAny().size()); Array array = (Array) searchLinkNameValueList.getRecords().getAny().get(0); Assert.assertNotNull(array.getAny().get(0)); logger.trace("response-testSearchByModule: {}", ReflectionToStringBuilder.toString(array)); } @Test @Ignore public void testCreateANewEntry() { SetEntryRequestType request = new SetEntryRequestType(); request.setModuleName(MODULE); request.setSession(getSession()); ListToSendByCxf<NameValue> myList = createDataPerson("Other John"); request.setNameValueList(myList); NewSetEntryResult response = client.setEntry(request).getReturn(); logger.trace("Response-setEntry: {}", ReflectionToStringBuilder.toString(response)); } @Test @Ignore public void testCreateMultipleEntries() { SetEntriesRequestType request = new SetEntriesRequestType(); request.setModuleName(MODULE); request.setSession(getSession()); ListToSendByCxf<ListToSendByCxf<NameValue>> nameValuesList = new ListToSendByCxf<ListToSendByCxf<NameValue>>(); nameValuesList.addElement(createDataPerson("First Joe")).addElement(createDataPerson("Second Joe")) .addElement(createDataPerson("Third Joe")); request.setNameValueLists(nameValuesList); NewSetEntriesResult response = client.setEntries(request).getReturn(); Assert.assertEquals(3, response.getIds().getAny().size()); logger.trace("Response-setEntries: {}", ReflectionToStringBuilder.toString(response.getIds())); } @Test @Ignore public void testSetRelationShip() { SetRelationshipRequestType request = new SetRelationshipRequestType(); request.setDelete(0); request.setLinkFieldName(RELATION_CONTACT_OPPORTUNITY); request.setSession(getSession()); request.setModuleId(CONTACT_ID); request.setModuleName(MODULE); request.setRelatedIds(OPORTUNITY_ID); NewSetRelationshipListResult response = client.setRelationship(request).getReturn(); Assert.assertEquals(1, response.getCreated()); logger.trace("Response-setRelationShip: {}", ReflectionToStringBuilder.toString(response)); } @Test @Ignore public void testSetRelationShips() { SetRelationshipsRequestType request = new SetRelationshipsRequestType(); request.setModuleIds(new ListToSendByCxf<String>().addElement(CONTACT_ID).addElement(CONTACT_ID)); request.setRelatedIds( new ListToSendByCxf<String>().addElement(OPORTUNITY_ID).addElement(OTHER_OPPORTUNITY_ID)); request.setLinkFieldNames(new ListToSendByCxf<String>().addElement(RELATION_CONTACT_OPPORTUNITY) .addElement(RELATION_CONTACT_OPPORTUNITY)); request.setModuleNames(new ListToSendByCxf<String>().addElement(MODULE).addElement(MODULE)); request.setSession(getSession()); NewSetRelationshipListResult response = client.setRelationships(request).getReturn(); Assert.assertEquals(2, response.getCreated()); logger.trace("Response-setRelationShip: {}", ReflectionToStringBuilder.toString(response)); } @Test @Ignore public void testGetEntry() { GetEntryRequestType req = new GetEntryRequestType(); req.setSession(getSession()); req.setId(CONTACT_ID); req.setModuleName(MODULE); req.setSelectFields(new ListToSendByCxf<String>().addElement("id").addElement("name")); GetEntryResultVersion2 response = client.getEntry(req).getReturn(); EntryValue entry = (EntryValue) response.getEntryList().getAny().get(0); List<Object> nameValues = entry.getNameValueList().getAny(); NameValue idValue = (NameValue) nameValues.get(0); assertEquals(CONTACT_ID, idValue.getValue()); logger.trace("Response-setRelationShip: {}", ReflectionToStringBuilder.toString(nameValues)); } @Test @Ignore public void testGetRelationShip() { GetRelationshipsRequestType request = new GetRelationshipsRequestType(); request.setSession(getSession()); request.setRelatedModuleQuery(""); request.setModuleName(MODULE); request.setModuleId(CONTACT_ID); request.setLinkFieldName(RELATION_CONTACT_OPPORTUNITY); request.setDeleted(0); SelectFields fields = new SelectFields(); fields.setArrayType(""); fields.getAny().add(newField("")); request.setRelatedFields(fields); GetEntryResultVersion2 response = client.getRelationships(request).getReturn(); logger.trace("Response-setRelationShip: {}", ReflectionToStringBuilder.toString(response)); } }