How add some content to Mockito mocked Pageable class? - mockito

I want testing pagination.
I want add some fake content to my Page mocked class.
How can I?
This is my test (refactoring it from a previous test), I'm using Junit5 and of course I get that No valut at JSON path $.content.lenght() and so on (of course, Page is empty):
/**
* index
*/
#WithMockUser("username")
#Test
void testCanIndex() throws Exception {
var transaction01 = BankAccountTransactionsControllerTest.validBankAccountTransactionsEntity(1L,
new BigDecimal(99.99), "First Transaction");
var transaction02 = BankAccountTransactionsControllerTest.validBankAccountTransactionsEntity(2L,
new BigDecimal(150.00), "Second Transaction");
var result = new ArrayList<BankAccountTransactionsEntity>();
result.add(transaction01);
result.add(transaction02);
Page<BankAccountTransactionsEntity> items = mock(Page.class);
when(bankAccountTransactionsService.index(0, 1, "id", "desc")).thenReturn(items);
mvc.perform(get("/api/v1/bank-account-transactions/")).andExpect(status().isOk())
.andExpect(jsonPath("$.content.length()", is(2))).andExpect(jsonPath("$[0].id", is(1)))
.andExpect(jsonPath("$.content[1].id", is(2))).andExpect(jsonPath("$[0].amount", is(new BigDecimal(99.99))))
.andExpect(jsonPath("$.content[1].amount", is(150)))
.andExpect(jsonPath("$.content[0].purpose", is("First Transaction")))
.andExpect(jsonPath("$.content[1].purpose", is("Second Transaction")))
.andExpect(jsonPath("$.content[0]", Matchers.hasKey("transactionDate")))
.andExpect(jsonPath("$.content[1]", Matchers.hasKey("transactionDate")));
}
Edit
I made a change, calling directly a PageImpl
#Test
void testCanIndex() throws Exception {
var transaction01 = BankAccountTransactionsControllerTest.validBankAccountTransactionsEntity(1L,
new BigDecimal(99.99), "First Transaction");
var transaction02 = BankAccountTransactionsControllerTest.validBankAccountTransactionsEntity(2L,
new BigDecimal(150.00), "Second Transaction");
var result = new ArrayList<BankAccountTransactionsEntity>();
result.add(transaction01);
result.add(transaction02);
Page<BankAccountTransactionsEntity> items = new PageImpl<>(result);
when(bankAccountTransactionsService.index(0, 1, "id", "desc")).thenReturn(items);
mvc.perform(get("/api/v1/bank-account-transactions/")).andExpect(status().isOk())
.andExpect(jsonPath("$.content.length()", is(2)));
}
But body returned is empty
MockHttpServletRequest:
HTTP Method = GET
Request URI = /api/v1/bank-account-transactions/
Parameters = {}
Headers = []
Body = null
Session Attrs = {}
Handler:
Type = com.bitbank.controllers.BankAccountTransactionsController
Method = com.bitbank.controllers.BankAccountTransactionsController#index(Integer, Integer, String, String)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers", X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY"]
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []

Related

Optional param bool in Contains of CosmosDB is breaking query

select * from c
where c.__type = #type
and CONTAINS(c.title, #search, #ignoretest)
and (NOT IS_DEFINED(c.archived) or c.archived = false)
and (NOT IS_DEFINED(c.deleted) or c.deleted = null)
var sqlParams = new SqlParameterCollection();
sqlParams.Add(new SqlParameter { Name = "#ignoretest", Value = true });
If I have boolean #ignoreTest param then my query is not working. What are the reasons? documentation is saying I can use this optional param https://learn.microsoft.com/en-us/azure/cosmos-db/sql/sql-query-contains

No suitable HttpMessageConverter found error while executing rest service that takes multipart parameters

I am using Spring Integration in my project. I am trying to execute a rest service which takes multipart/formdata input parameters. I am using int-http:outbound-gateway to execute rest service. The following is the code:
<int:channel id="PQcreateAttachment-Rest-Channel" />
<int:chain input-channel="PQcreateAttachment-Rest-Channel" output-channel="PQcreateAttachment-StoredProcedure-Router" >
<int:header-filter header-names="accept-encoding"/>
<int:service-activator ref="httpOutboundGatewayHandler" method="buildMultipartHttpOutboundGatewayRequest" />
<int-http:outbound-gateway url-expression="headers.restResourceUrl"
http-method-expression="headers.httpMethod"
extract-request-payload="true"
>
</int-http:outbound-gateway>
<int:service-activator ref="msgHandler" method="buildMessageFromExtSysResponse" />
</int:chain>
But I am getting the following error when I execute the above code.
Caused by: org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.integration.message.GenericMessage] and content type [application/x-java-serialized-object]
at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:665)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:481)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:460)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:409)
at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:372)
... 121 more
Here is the java code that prepares my multipart request:
public Message<?> buildMultipartHttpOutboundGatewayRequest(Message<?> inMessage) throws Exception{
logger.debug(" ************** buildMultipartHttpOutboundGatewayRequest Start *************************");
String inMsgPayload = (String)inMessage.getPayload();
SOAXml soaXml = parseSOAXml(inMsgPayload);
String restURL = null;
String contentType = null;
String acceptHdr = null;
String userId = null;
String password = null;
String businessAreaName = null;
String typeName = null;
String attachmentLocation = null;
String httpMethod = null;
Message<?> outMessage = null;
MessageHeaders inMsgHdrs = null;
MessageBuilder<?> msgBuild = null;
String authorization = null;
//TODO: File location needs to be changed to standard one
String fileLocation = "C:\\source.xml";
//if we reach here means, it is AWD system
restURL = getAwdSOAService(soaXml);
Document document = XmlParserUtil.convertString2Document(inMsgPayload);
userId = XmlParserUtil.getNodeValue(document,"//userId");
password = XmlParserUtil.getNodeValue(document,"//PQcreateAttachment/password");
businessAreaName = XmlParserUtil.getNodeValue(document,"//businessAreaName");
typeName = XmlParserUtil.getNodeValue(document,"//typeName");
httpMethod = XmlParserUtil.getNodeValue(document,"//METHOD");
attachmentLocation = XmlParserUtil.getNodeValue(document,"//attachmentLocation");
//Construct source xml
//Creating document
Document sourceDocument = DocumentHelper.createDocument();
Element sourceInstance = sourceDocument.addElement("createSourceInstance");
sourceInstance.addAttribute("xmlns", "http://www.dsttechnologies.com/awd/rest/v1");
Element orderItem=sourceInstance.addElement("businessAreaName");
orderItem.setText("SAMPLEBA");
Element orderItemDesc=sourceInstance.addElement("typeName");
orderItemDesc.setText("SAMPLEST");
// create source xml file
XmlParserUtil.createXMLFileUsingDOM4J(sourceDocument, fileLocation);
authorization = getBasicAuthorization(userId,password);
Resource source = new ClassPathResource(fileLocation);
Resource attachment = new ClassPathResource(attachmentLocation);
Map<String, Object> multipartMap = new HashMap<String, Object>();
multipartMap.put("source", source);
multipartMap.put("attachment", attachment);
logger.info("Created multipart request: " + multipartMap);
inMessage = buildMessageForMultipart(multipartMap);
// contentType = csProps.getHttpAwdContentTypeValue();
acceptHdr = csProps.getHttpAwdAcceptTypeValue() ;
// authorization = getBasicAuthorization(soaXml.getUserid(),decriptPassword(soaXml.getPassword()));
inMsgHdrs = inMessage.getHeaders();
msgBuild = MessageBuilder.withPayload(inMessage).copyHeaders(inMsgHdrs);
msgBuild.removeHeader("Content-Encoding");
msgBuild.removeHeader("accept-encoding");
msgBuild.setHeader(csProps.getHttpUrlHdr(), restURL);
msgBuild.setHeader(csProps.getHttpMethodHdr(), httpMethod);
msgBuild.setHeader(csProps.getHttpAuthorizatonHdr(),authorization );
// msgBuild.setHeader(csProps.getHttpContentTypeHdr(), contentType);
// msgBuild.setHeader(csProps.getHttpAcceptTypeHdr(),acceptHdr);
outMessage = msgBuild.build();
logger.debug(" ************** buildHttpOutboundGatewayRequest End*************************");
logger.debug(outMessage);
logger.debug(" ************************************************************************");
return outMessage;
}
Any ideas on what's wrong here?
Your problem is because you wrap one message to another.
What your buildMessageForMultipart(multipartMap); does?
I'm sure the simple map as payload and those header would be enough.
Not sure what is the point to wrap one message to another.

How to batch get items using servicestack.aws PocoDynamo?

With Amazon native .net lib, batchget is like this
var batch = context.CreateBatch<MyClass>();
batch.AddKey("hashkey1");
batch.AddKey("hashkey2");
batch.AddKey("hashkey3");
batch.Execute();
var result = batch.results;
Now I'm testing to use servicestack.aws, however I couldn't find how to do it. I've tried the following, both failed.
//1st try
var q1 = db.FromQueryIndex<MyClass>(x => x.room_id == "hashkey1" || x.room_id == "hashkey2"||x.room_id == "hashkey3");
var result = db.Query(q1);
//2nd try
var result = db.GetItems<MyClass>(new string[]{"hashkey1","hashkey2","hashkey3"});
In both cases, it threw an exception that says
Additional information: Invalid operator used in KeyConditionExpression: OR
Please help me. Thanks!
Using GetItems should work as seen with this Live Example on Gistlyn:
public class MyClass
{
public string Id { get; set; }
public string Content { get; set; }
}
db.RegisterTable<MyClass>();
db.DeleteTable<MyClass>(); // Delete existing MyClass Table (if any)
db.InitSchema(); // Creates MyClass DynamoDB Table
var items = 5.Times(i => new MyClass { Id = $"hashkey{i}", Content = $"Content {i}" });
db.PutItems(items);
var dbItems = db.GetItems<MyClass>(new[]{ "hashkey1","hashkey2","hashkey3" });
"Saved Items: {0}".Print(dbItems.Dump());
If your Item has both a Hash and a Range Key you'll need to use the GetItems<T>(IEnumerable<DynamoId> ids) API, e.g:
var dbItems = db.GetItems<MyClass>(new[]{
new DynamoId("hashkey1","rangekey1"),
new DynamoId("hashkey2","rangekey3"),
new DynamoId("hashkey3","rangekey4"),
});
Query all Items with same HashKey
If you want to fetch all items with the same HashKey you need to create a DynamoDB Query as seen with this Live Gistlyn Example:
var items = 5.Times(i => new MyClass {
Id = $"hashkey{i%2}", RangeKey = $"rangekey{i}", Content = $"Content {i}" });
db.PutItems(items);
var rows = db.FromQuery<MyClass>(x => x.Id == "hashkey1").Exec().ToArray();
rows.PrintDump();

Issue with SqlScalar<T> and SqlList<T> when calling stored procedure with parameters

The new API for Servicestack.OrmLite dictates that when calling fx a stored procedure you should use either SqlScalar or SqlList like this:
List<Poco> results = db.SqlList<Poco>("EXEC GetAnalyticsForWeek 1");
List<Poco> results = db.SqlList<Poco>("EXEC GetAnalyticsForWeek #weekNo", new { weekNo = 1 });
List<int> results = db.SqlList<int>("EXEC GetTotalsForWeek 1");
List<int> results = db.SqlList<int>("EXEC GetTotalsForWeek #weekNo", new { weekNo = 1 });
However the named parameters doesn't work. You HAVE to respect the order of the parameters in the SP. I think it is because the SP is executed using CommandType=CommandType.Text instead of CommandType.StoredProcedure, and the parameters are added as dbCmd.Parameters.Add(). It seems that because the CommandType is Text it expects the parameters to be added in the SQL querystring, and not as Parameters.Add(), because it ignores the naming.
An example:
CREATE PROCEDURE [dbo].[sproc_WS_SelectScanFeedScanRecords]
#JobNo int = 0
,#SyncStatus int = -1
AS
BEGIN
SET NOCOUNT ON;
SELECT
FSR.ScanId
, FSR.JobNo
, FSR.BatchNo
, FSR.BagNo
, FSR.ScanType
, FSR.ScanDate
, FSR.ScanTime
, FSR.ScanStatus
, FSR.SyncStatus
, FSR.JobId
FROM dbo.SCAN_FeedScanRecords FSR
WHERE ((FSR.JobNo = #JobNo) OR (#JobNo = 0) OR (ISNULL(#JobNo,1) = 1))
AND ((FSR.SyncStatus = #SyncStatus) OR (#SyncStatus = -1) OR (ISNULL(#SyncStatus,-1) = -1))
END
When calling this SP as this:
db.SqlList<ScanRecord>("EXEC sproc_WS_SelectScanFeedScanRecords #SyncStatus",new {SyncStatus = 1});
It returns all records with JobNo = 1 instead of SyncStatus=1 because it ignores the named parameter and add by the order in which they are defined in the SP.
I have to call it like this:
db.SqlList<ScanRecord>("EXEC sproc_WS_SelectScanFeedScanRecords #SyncStatus=1");
Is this expected behavior? I think it defeats the anonymous type parameters if I can't trust it
TIA
Bo
My solution was to roll my own methods for stored procedures. If people finds them handy, I could add them to the project
public static void StoredProcedure(this IDbConnection dbConn, string storedprocedure, object anonType = null)
{
dbConn.Exec(dbCmd =>
{
dbCmd.CommandType = CommandType.StoredProcedure;
dbCmd.CommandText = storedprocedure;
dbCmd.SetParameters(anonType, true);
dbCmd.ExecuteNonQuery();
});
}
public static T StoredProcedureScalar<T>(this IDbConnection dbConn, string storedprocedure, object anonType = null)
{
return dbConn.Exec(dbCmd =>
{
dbCmd.CommandType = CommandType.StoredProcedure;
dbCmd.CommandText = storedprocedure;
dbCmd.SetParameters(anonType, true);
using (IDataReader reader = dbCmd.ExecuteReader())
return GetScalar<T>(reader);
});
}
public static List<T> StoredProcedureList<T>(this IDbConnection dbConn, string storedprocedure, object anonType = null)
{
return dbConn.Exec(dbCmd =>
{
dbCmd.CommandType = CommandType.StoredProcedure;
dbCmd.CommandText = storedprocedure;
dbCmd.SetParameters(anonType, true);
using (var dbReader = dbCmd.ExecuteReader())
return IsScalar<T>()
? dbReader.GetFirstColumn<T>()
: dbReader.ConvertToList<T>();
});
}
They are just modified versions of the SqlScalar and SqlList plus the ExecuteNonQuery

Mark an appointment as Scheduled in CRM 2011

I have a Silverlight application that creates an appointment but since it's not marked as "Scheduled" it will not show up in the Calendar. I am using the SOAP service for this, and here is some sample code that fires on a button click that creates an appointment but it does not show up in the Calendar:
TestCommand = new RelayCommand(() =>
{
var soapCtx = ServiceHelper.GetSingletonSoapService();
var activityId = Guid.NewGuid();
var appt = new Entity()
{
Id = activityId,
LogicalName = "appointment"
};
appt["activityid"] = activityId;
appt["scheduledstart"] = DateTime.Now.Date;
appt["scheduledend"] = DateTime.Now.Date.AddHours(1);
appt["description"] = "Test 1 2 3";
appt["subject"] = "Test Command Button Appointment";
appt["location"] = "Location 1 2 3";
soapCtx.BeginCreate(appt, ar =>
{
var response = soapCtx.EndCreate(ar);
var bookRequest = new OrganizationRequest();
bookRequest.RequestName = "Book";
bookRequest["Target"] = appt;
soapCtx.BeginExecute(bookRequest,
new AsyncCallback(OnBookRequestedCompleted), soapCtx);
}, null);
});
void OnBookRequestedCompleted(IAsyncResult ar)
{
var soapCtx = (IOrganizationService)ar.AsyncState;
var response = soapCtx.EndExecute(ar);
}
I keep getting "NotFound" exception in the OnBookRequestedCompleted method. Is there a different way of doing this?
Use Fiddler to debug your HTTP request/response and this will give you details about the 'Not Found' exception.
You should not be setting the ActivityId field it is returned by the BookRequest. if BookResponse.ValidationResult.ValidationSuccess == true, apptId = BookResponse.ValidationResult.ActivityId
You need to call SetStateRequest with the id returned by the BookRequest to set the state to Scheduled
For example:
SetStateRequest state = new SetStateRequest();
state.State = new OptionSetValue(3); // Scheduled
state.Status = new OptionSetValue(5); // Busy
state.EntityMoniker = new EntityReference("appointment", apptId);
SetStateResponse stateSet = (SetStateResponse)this.orgService.Execute(state);

Resources