I have the following Servlet method which I want to create test for it using junit or Cactus.I've tried Junit but in testing implementation classes but I'm a newbie in testing WebServices so I will really appreciate any help.
public DSResponse executeFetch(DSRequest req) throws Exception {
DSResponse resp = new DSResponse();
String maID = (String) req.getCriteria().get("memberActivityID");
MemberActivityImpl memberImpl = new MemberActivityImpl();
MemberActivity memberAct = new MemberActivity();
if (req.getDataSourceName().equals("memberActivity")) {
if (maID != null) {
// Fetch the MemberActivity based on the memberActivityID criteria
memberAct = memberImpl.getMemberActivity(maID);
List<Map> resultList = new LinkedList<Map>();
if( memberAct != null && memberAct.getMemberID() != null )
// Pass the memberAct to the client
Map<String, Object> result = new HashMap<String, Object>();
result.put("name", memberAct.getName());
result.put("type", memberAct.getType());
result.put("memberID", memberAct.getMemberID());
if (memberAct.getGoal() != null) {
result.put("goal", memberAct.getGoal());}
resultList.add(result);
}
resp.setData(resultList);
} else {
resp.setFailure();
}
`
Related
Set<String> response = null;
Set<String> success = null;
for (String country : countrys) {
response = service.method(country);
if (response != null) {
success = response;
}
}
Here service.method returns a Set<String>. I want to add the response of each loop to the success Set.
Now, this code is just storing the response of the last loop in success.
Can someone please help with this at the earliest?
You could use the addAll(Collection<? extends E> c) method (see spec):
Set<String> response = null;
Set<String> success = new HashSet<>();
for (String country : countrys) {
response = service.method(country);
if (response != null) {
success.addAll(response);
}
}
Keep in mind that you will want to initialize success as an empty set (e.g. a HashSet) first. Otherwise you will run into a NullPointerException.
public Collection<OwnerDetail> getAllOwners() {
final MorphiaCursor<Listing> listings = datastore.find(Listing.class)
.iterator(new FindOptions()
.projection().include("ownerDetail"));
Map<String, OwnerDetail> map = new HashMap<>();
while (listings.hasNext()) {
final Listing next = listings.next();
if (next.getOwnerDetail() != null) {
map.put(next.getOwnerDetail().getEmail(), next.getOwnerDetail());
}
}
return map.values();
}
I want to remove java code that distinct values by email and handle it by mongodb morphia. How can I do that?
I have immutable objects as follows.
#Getter
#Builder
class MainDetail {
// 5 other String fields
private Data data;
}
#Getter
#Builder
class ImageUrl {
private final String dataOne; // looking to change these 2 values
private final String dataTwo; // if rest call returns null for these.
}
Information to fill these up is fetched from a rest call, working fine as follows.
List<MainDetail> featureBenefits = // value from a rest response
I wish to switch out the dataOne and dataTwo values in here if it is null for each MainDetail Object.
I can't just use a set method to do this cos it is immutable.
I end up with the following verbose way of doing it where I need to do multiple variations of the check to swap values.
I can't just check one at a time and switch cos Object becomes immutable. Can't add another if the second one is null too after that.
Is there a way to do this more elegantly, possibly via streams? Appreciate any help. Thanks.
List<MainDetail> mainDetails = new ArrayList<>();
for (MainDetail mainDetail : featureBenefits) {
if (mainDetail.getImageUrl().getDataOne() == null && mainDetail.getImageUrl().getdataTwo() == null) {
ImageUrl imageUrl = ImageUrl.builder()
.dataOne("default1")
.dataTwo("default12")
.build();
MainDetail detail = MainDetail.builder()
.imageUrl(imageUrl)
.build();
mainDetails.add(detail);
}
else if (mainDetail.getImageUrl().getdataOne() == null) {
ImageUrl imageUrl = ImageUrl.builder()
.dataOne("default1")
.build();
MainDetail detail = MainDetail.builder()
.imageUrl(imageUrl)
.build();
mainDetails.add(detail);
}
else if (mainDetail.getImageUrl().getDataTwo() == null) {
ImageUrl imageUrl = ImageUrl.builder()
.dataTwo("default2")
.build();
MainDetail detail = MainDetail.builder()
.imageUrl(imageUrl)
.build();
mainDetails.add(detail);
}
}
What about this one:
List<MainDetail> featureBenefits = Collections.emptyList();
List<MainDetail> mainDetails = new ArrayList<>();
for (MainDetail mainDetail : featureBenefits) {
ImageUrl imageUrl = mainDetail.getImageUrl();
mainDetails.add(MainDetail.builder()
.imageUrl(ImageUrl.builder()
.dataOne(Optional.ofNullable(imageUrl.getDataOne()).orElse("default1"))
.dataTwo(Optional.ofNullable(imageUrl.getDataTwo()).orElse("default2"))
.build())
.build());
}
If you are not limited to sticking with standard builders then you could add your own methods for providing default values:
class ImageUrl {
private final String dataOne; // looking to change these 2 values
private final String dataTwo; // if rest call returns null for these.
public ImageUrl withDefaultDataOne(String value) {
return dataOne == null ? new ImageUrl(value, dataTwo) : this;
}
public ImageUrl withDefaultDataTwo(String value) {
return dataTwo == null ? new ImageUrl(dataOne, value) : this;
}
}
Then your translation code becomes:
for (MainDetail mainDetail : featureBenefits) {
ImageUrl imageUrl = mainDetail.getImageUrl()
.withDefaultDataOne("default1")
.withDefaultDataTwo("default2");
mainDetails.add(MainDetail.builder().imageUrl(imageUrl).build());
}
I am able to create order using square(v2/locations/location_id/orders)api and getting order id. But I am not able to get this order details and also how I can see this created order on square dashboard? please help me.
I am using the below method for doing it:
public CreateOrderResponse createOrder(String locationId, CreateOrderRequest body) throws ApiException {
Object localVarPostBody = body;
// verify the required parameter 'locationId' is set
if (locationId == null) {
throw new ApiException(400, "Missing the required parameter 'locationId' when calling createOrder");
}
// verify the required parameter 'body' is set
if (body == null) {
throw new ApiException(400, "Missing the required parameter 'body' when calling createOrder");
}
// create path and map variables
String localVarPath = "/v2/locations/{location_id}/orders".replaceAll("\\{" + "location_id" + "\\}",
apiClient.escapeString(locationId.toString()));
// query params
List<Pair> localVarQueryParams = new ArrayList<Pair>();
Map<String, String> localVarHeaderParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();
final String[] localVarAccepts = { "application/json" };
final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
final String[] localVarContentTypes = { "application/json" };
final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
String[] localVarAuthNames = new String[] { "oauth2" };
GenericType<CreateOrderResponse> localVarReturnType = new GenericType<CreateOrderResponse>() {
};
CompleteResponse<CreateOrderResponse> completeResponse = (CompleteResponse<CreateOrderResponse>) apiClient
.invokeAPI(localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams,
localVarFormParams, localVarAccept, localVarContentType, localVarAuthNames,
localVarReturnType);
return completeResponse.getData();
}
Thanks
The orders endpoint is only for creating itemized orders for e-commerce transactions. You won't see them anywhere until you charge them, and then you'll see the itemizations for the order in your dashboard with the transaction.
I want to Search My Index in all Types and all fields for the query.
I know how to do it using REST way.
I want to know how to do it using JAVA API and more specifically in Spring Data Elasticsearch.
This is some code i have written but seams to be pretty messed in terms of return Type.
#Override
public Page<Object> searchInAll(String searchQuery) {
MatchQueryBuilder matchQueryBuilder =QueryBuilders.matchQuery("_all", "trump");
SearchQuery searchQueryFinal = new NativeSearchQueryBuilder().withQuery(matchQueryBuilder).withIndices("booksearchserver").withTypes("authors","books").build();
return elasticsearchTemplate.queryForPage(searchQueryFinal, Author.class, new SearchResultMapper(){
#SuppressWarnings("unchecked")
public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
long totalHits = response.getHits().getTotalHits();
List<Book> content = new ArrayList<Book>();
for (SearchHit searchHit : response.getHits()) {
if (response.getHits().getHits().length <= 0) {
return null;
}else if(searchHit.getType().equals("books")){
logger.info("Creating Book,Mapping Book,Adding Book to Page");
}
else if(searchHit.getType().equals("author")){
logger.info("Creating Author,Mapping Author,Adding Author to Page");
}
logger.info(searchHit.getType());
}
List<FacetResult> facets = new ArrayList<FacetResult>();
if (response.getFacets() != null) {
for (Facet facet : response.getFacets()) {
FacetResult facetResult = DefaultFacetMapper.parse(facet);
if (facetResult != null) {
facets.add(facetResult);
}
}
}
return new FacetedPageImpl<T>((List<T>)content, pageable, totalHits, facets);
}
});