how to handle nested #DynamoDBDocument - java

I have a domain called UserAdvice which has a variable of type PriceWithPaf, which has 2 variables in it, which are again of custom-type/class-object. I have declared both the inner-objects/variables and the parent class which is a variable in the Domain as #DynamoDBDocument.
The best part is I am able to save the data exactly the way I want in DynamoDB Database but when I read the data back to object I get following error:
(I am using spring data for dynamoDB - from github & this error is shown in test case - where I use AmazonDynamoDBLocal)
com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: UserAdvise[averageEntryPrice]; could not unconvert attribute
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperTableModel.unconvert(DynamoDBMapperTableModel.java:271)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.privateMarshallIntoObject(DynamoDBMapper.java:456)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.marshallIntoObjects(DynamoDBMapper.java:484)
at com.amazonaws.services.dynamodbv2.datamodeling.PaginatedScanList.<init>(PaginatedScanList.java:64)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.scan(DynamoDBMapper.java:1458)
at com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.scan(AbstractDynamoDBMapper.java:216)
at org.socialsignin.spring.data.dynamodb.core.DynamoDBTemplate.scan(DynamoDBTemplate.java:112)
at org.socialsignin.spring.data.dynamodb.repository.support.SimpleDynamoDBCrudRepository.findAll(SimpleDynamoDBCrudRepository.java:166)
at org.socialsignin.spring.data.dynamodb.repository.support.SimpleDynamoDBCrudRepository.findAll(SimpleDynamoDBCrudRepository.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:503)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:488)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy68.findAll(Unknown Source)
at com.wt.api.write.WDAuthenticationShould.return_200_for_virtual_and_real_advises_and_wallet_amount(WDAuthenticationShould.java:1079)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.contrib.java.lang.system.EnvironmentVariables$EnvironmentVariablesStatement.evaluate(EnvironmentVariables.java:71)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: PriceWithPaf[nonDividendPaf]; could not unconvert attribute
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperTableModel.unconvert(DynamoDBMapperTableModel.java:271)
at com.amazonaws.services.dynamodbv2.datamodeling.StandardModelFactories$Rules$ObjectDocumentMap$1.unconvert(StandardModelFactories.java:603)
at com.amazonaws.services.dynamodbv2.datamodeling.StandardModelFactories$Rules$ObjectDocumentMap$1.unconvert(StandardModelFactories.java:598)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverter$DelegateConverter.unconvert(DynamoDBTypeConverter.java:109)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverter$NullSafeConverter.unconvert(DynamoDBTypeConverter.java:128)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverter$ExtendedConverter.unconvert(DynamoDBTypeConverter.java:88)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperFieldModel.unconvert(DynamoDBMapperFieldModel.java:146)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperFieldModel.unconvertAndSet(DynamoDBMapperFieldModel.java:164)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperTableModel.unconvert(DynamoDBMapperTableModel.java:267)
... 57 more
Caused by: com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: could not invoke null on class com.wt.domain.PriceWithPaf with value 1.0 of type class java.lang.Double
at com.amazonaws.services.dynamodbv2.datamodeling.StandardBeanProperties$MethodReflect.set(StandardBeanProperties.java:136)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperFieldModel.set(DynamoDBMapperFieldModel.java:111)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperFieldModel.unconvertAndSet(DynamoDBMapperFieldModel.java:164)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperTableModel.unconvert(DynamoDBMapperTableModel.java:267)
... 65 more
Caused by: java.lang.NullPointerException
at com.amazonaws.services.dynamodbv2.datamodeling.StandardBeanProperties$MethodReflect.set(StandardBeanProperties.java:133)
... 68 more

After facing this issue and trying to solve it for 3 days. Finally came across this article , it worked .
My mistake was :
Initial
#DynamoDBAttribute
public String getPeriod() {
return period;
}
public void set_period(String period) {
this.period = period;
}
Changed it to :
#DynamoDBAttribute
public String getPeriod() {
return period;
}
public void setPeriod(String period) {
this.period = period;
}

It worked - the error was due to wrong getter setup - one of the getter was suppose to return custom object (PriceWithPaf) instead of returning a double.
This made the unmapping of dynamoDBDocument go wrong.
using DynamoDBDocument is easy - just put this annotation on all the class which acts as a variable to the Domain Class. and make sure your getter and setter are default/not manipulated ;)

Related

Getting IllegalStateException while calling restTemplate.exchange()

Getting the java.lang.IllegalStateException while trying to read the data from json file using RestTemplate.exchange(). Tried many ways, but no success. Any suggestion would be appreciate.
Code:
ResponseEntity<GetCustomerResponse> customerResponse = restTemplate.exchange("J:\Backup\FILWORKSPACE\tapp100997_accountopeningservice\target\classes\testdata\CustomerEnquiryApiResponse.json",HttpMethod.POST, request, GetCustomerResponse.class);
Exception :
java.lang.IllegalStateException: Could not create URI object: Illegal character in opaque part at index 2: J:\Backup\FILWORKSPACE\tapp100997_accountopeningservice\target\classes\testdata\CustomerEnquiryApiResponse.json
at org.springframework.web.util.DefaultUriTemplateHandler.createUri(DefaultUriTemplateHandler.java:168)
at org.springframework.web.util.DefaultUriTemplateHandler.expandInternal(DefaultUriTemplateHandler.java:105)
at org.springframework.web.util.AbstractUriTemplateHandler.expand(AbstractUriTemplateHandler.java:106)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:612)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)
at com.fidintl.bs.accountopening.rulengine.rules.CustomerRule.getCustomerDetails(CustomerRule.java:169)
at com.fidintl.bs.accountopening.rulengine.rules.CustomerRule.execute(CustomerRule.java:55)
at com.fidintl.bs.accountopening.rulengine.rules.CustomerRuleTest.testCustomerEligibilityWithBlankData(CustomerRuleTest.java:122)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.net.URISyntaxException: Illegal character in opaque part at index 2: J:\Backup\FILWORKSPACE\tapp100997_accountopeningservice\target\classes\testdata\CustomerEnquiryApiResponse.json
at java.net.URI$Parser.fail(URI.java:2848)
at java.net.URI$Parser.checkChars(URI.java:3021)
at java.net.URI$Parser.parse(URI.java:3058)
at java.net.URI.<init>(URI.java:588)
at org.springframework.web.util.DefaultUriTemplateHandler.createUri(DefaultUriTemplateHandler.java:165)
... 33 more
Json file Data :
{"success":true,"errors":[],"customerDetails":[{"customerReference":{"type":"PARTY_REFERENCE_NUMBER","identifier":"500008713684"},"success":true,"errors":[],"customers":[{"customerType":"INDIVIDUAL_INVESTOR","individualClientDetails":{"personalDetails":{"personName":{"title":"Miss","initials":null,"firstName":"User","lastName":"Payroll","fullName":"Miss User Payroll","printName":"Miss User Payroll"},"dateOfBirth":"1970-12-01","dateOfDeath":null,"gender":"UNKNOWN","maritalStatus":"UNKNOWN","townOfBirth":null,"countryOfBirth":"GB","nationalityDetails":[{"countryCode":"GB","primaryNationalityFlag":null,"reportingNationalityFlag":"Y","reportingDetails":{"nationalIdType":"TAX_IDENTIFIER","nationalId":"AB109999D"}},{"countryCode":"GB","primaryNationalityFlag":"Y","reportingNationalityFlag":null,"reportingDetails":null}],"countriesOfResidence":[{"countryCode":"GB","countryName":"UNITED KINGDOM","primaryCountryOfResidence":"Y"}],"age":"48","niNoIssuedFlag":"Y","anticipatedRetirementAge":null},"crownEmployeeFlag":"N","planParticipantDetails":null,"employerDetails":null,"occupation":null},"institutionalClientDetails":null,"partyReferenceNumber":"500008713684","alternativeReferences":[{"identifierType":"SONATA_CLIENT_ID","identifier":"100668431"},{"identifierType":"INVESTOR_REFERENCE_NUMBER","identifier":"1004664408"}],"lifeCycleStatus":"INVESTOR","inceptionDate":"2019-03-19","dormancyDate":null,"directAdvisedIndicator":"DIRECT_AND_ADVISED","lostContactFlag":"N","probateFlag":"N","courtOfProtectionFlag":"N","languagePreference":"ENG","postalAddresses":[{"contactRole":"CORRESPONDENCE_ADDRESS","nonStructuredAddressDetails":{"line1":"30 Benedict Drive","line2":null,"line3":"FELTHAM","line4":null},"structuredAddressDetails":null,"overrideAddress":"N","administrativeArea":null,"postalTown":"FELTHAM","postalCode":"TW14 8JL","region":"UK","countryCode":"GB","setBy":"500008713684","status":"ACTIVE","validity":{"lastValidatedDate":null,"validFrom":"2019-03-19 18:05:07.000+0000","validTo":null},"countryName":"UNITED KINGDOM"},{"contactRole":"REGISTERED_ADDRESS","nonStructuredAddressDetails":{"line1":"30 Benedict Drive","line2":null,"line3":"FELTHAM","line4":null},"structuredAddressDetails":null,"overrideAddress":"N","administrativeArea":null,"postalTown":"FELTHAM","postalCode":"TW14 8JL","region":"UK","countryCode":"GB","setBy":"500008713684","status":"ACTIVE","validity":{"lastValidatedDate":null,"validFrom":"2019-03-19 18:05:07.000+0000","validTo":null},"countryName":"UNITED KINGDOM"}],"classifications":[{"classificationScheme":"FATCA_US","classificationCode":"INUS05","classificationDescription":"Non US Status"},{"classificationScheme":"OFFICE_CODE","classificationCode":"OAKH","classificationDescription":"FIL Investments International"},{"classificationScheme":"MIFID","classificationCode":"RE","classificationDescription":"Retail"},{"classificationScheme":"GFAS_CUSTOMER_TYPE","classificationCode":"P","classificationDescription":"Private Individual"}],"externalIdentifiers":[{"identifierName":"TAX_IDENTIFICATION_NUMBER","value":"AB109999D","country":"GB","tinNotIssued":"NOT_CONFIRMED"},{"identifierName":"NATIONAL_INSURANCE_NUMBER","value":"AB109999D","country":"GB","tinNotIssued":null}],"salutations":null,"documentDeliveryPreferenceDetails":{"documentDeliveryPreferences":[{"documentType":"COT","deliveryMethod":"POST_AND_ONLINE"},{"documentType":"SAV","deliveryMethod":"POST_AND_ONLINE"},{"documentType":"PLC","deliveryMethod":"POST_AND_ONLINE"},{"documentType":"RRL","deliveryMethod":"POST_AND_ONLINE"},{"documentType":"WPL","deliveryMethod":"POST_AND_ONLINE"},{"documentType":"DTL","deliveryMethod":"POST_AND_ONLINE"},{"documentType":"ITR","deliveryMethod":"POST_AND_ONLINE"},{"documentType":"WL","deliveryMethod":"POST_AND_ONLINE"},{"documentType":"MLC","deliveryMethod":"POST_AND_ONLINE"}],"emailNotificationFlag":"N"},"electronicAddresses":null,"telephones":null,"partyAssociations":null,"amlStatusDetails":null,"bankAccountDetails":null,"w8BenDetails":null,"reportingCurrency":null,"associatedFILEmployee":null,"employmentStatus":null,"marketingSource":null,"partyGenericVariables":{"genericVariables":[{"name":"PLATFORM","values":["SONATA"]},{"name":"PDP_CONSENT","values":["YES"]}]},"restrictions":null,"roleToFIL":null,"marketingPreferences":[],"applicationTerms":[{"applicationTermsAndCondition":{"applicationName":"ISA Declaration","applicationVersion":"1.0","applicationTermsAndConditionsVersion":"2.0","dateAccepted":"2015-03-12 00:00:00.000+0000","applicationType":"DOCUMENT"}},{"applicationTermsAndCondition":{"applicationName":"Doing Business with Fidelity","applicationVersion":"1.0","applicationTermsAndConditionsVersion":"2.0","dateAccepted":"2015-03-12 00:00:00.000+0000","applicationType":"DOCUMENT"}},{"applicationTermsAndCondition":{"applicationName":"Payroll ISA","applicationVersion":"1.0","applicationTermsAndConditionsVersion":"2.0","dateAccepted":"2015-03-12 00:00:00.000+0000","applicationType":"DOCUMENT"}},{"applicationTermsAndCondition":{"applicationName":"Fidelity's Privacy Policy","applicationVersion":"1.0","applicationTermsAndConditionsVersion":"2.0","dateAccepted":"2015-03-12 00:00:00.000+0000","applicationType":"DOCUMENT"}},{"applicationTermsAndCondition":{"applicationName":"US Person","applicationVersion":"1.0","applicationTermsAndConditionsVersion":"2.0","dateAccepted":"2015-03-12 00:00:00.000+0000","applicationType":"DOCUMENT"}},{"applicationTermsAndCondition":{"applicationName":"Client Terms","applicationVersion":"1.0","applicationTermsAndConditionsVersion":"2.0","dateAccepted":"2015-03-12 00:00:00.000+0000","applicationType":"DOCUMENT"}},{"applicationTermsAndCondition":{"applicationName":"Online Terms","applicationVersion":"1.0","applicationTermsAndConditionsVersion":"2.0","dateAccepted":"2015-03-12 00:00:00.000+0000","applicationType":"DOCUMENT"}}],"createdBy":null,"assignedTo":null,"updatedBy":null,"auditDetails":null,"standingPreferences":null,"literaturePreferences":null,"documentDeliveryPreferences":null,"pensionDetails":null,"migration":null,"pensionPlatform":null,"clientRegionDetails":null}]}]}
From docs https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html#exchange-java.lang.String-org.springframework.http.HttpMethod-org.springframework.http.HttpEntity-java.lang.Class-java.lang.Object...-,
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity.
You need to use valid hhtp url.

Error when testing secured spring repository

I'm having problem with a Unit Test for a spring JPA repository.
I added another method to the repository which should return only entities for the logged in user:
#Query("select budget from Budget budget where budget.user.login = ?#{principal.username}")
List<Budget> findByUserIsCurrentUser();
Budget Resource:
/**
* GET /budgets : get all the budgets.
*
* #return the ResponseEntity with status 200 (OK) and the list of budgets in body
*/
#GetMapping("/budgets")
#Timed
public List<Budget> getAllBudgets() {
log.debug("REST request to get all Budgets");
return budgetRepository.findByUserIsCurrentUser();
}
I adjusted the test for the getAllBudgets and added the .with(user) part:
#Test
#Transactional
public void getAllBudgets() throws Exception {
budgetRepository.saveAndFlush(budget);
// Create security-aware mockMvc
restBudgetMockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
budgetRepository.saveAndFlush(budget);
// Get all the points
restBudgetMockMvc.perform(get("/api/budgets?sort=id,desc")
.with(user("user").roles("user")))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.[*].id").value(hasItem(budget.getId().intValue())))
.andExpect(jsonPath("$.[*].name").value(hasItem(DEFAULT_NAME.toString())));
}
But now the test Fails with https://pastebin.com/8aLDTauC:
<testcase name="getAllBudgets" classname="com.pahofmann.budget.web.rest.BudgetResourceIntTest" time="0.19">
<failure message="java.lang.AssertionError: JSON path "$.[*].id" Expected: a collection containing <7> but: " type="java.lang.AssertionError">
java.lang.AssertionError: JSON path "$.[*].id" Expected: a collection containing <7> but: at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20) at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:74) at org.springframework.test.web.servlet.result.JsonPathResultMatchers$1.match(JsonPathResultMatchers.java:87) at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171) at com.pahofmann.budget.web.rest.BudgetResourceIntTest.getAllBudgets(BudgetResourceIntTest.java:177) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:114) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:57) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:66) at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32) at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93) at com.sun.proxy.$Proxy1.processTestClass(Unknown Source) at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:109) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:146) at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:128) at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404) at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63) at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:46) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55) at java.lang.Thread.run(Thread.java:748)
</failure>
It seems that there is no value in the array, but it is working fine without the user part. The application itself is working fine, what do I need to change to get the test working with the User?
Your test initialization is wrong: budget is not attached to any user, that's why the repository method returns an empty list.
User user = userRepository.findOneByLogin("user").get();
budget.setUser(user);
budgetRepository.saveAndFlush(budget);
Using a #Query with principal makes it difficult to debug, I prefer a more explicit approach where the login is provided by the resource controller:
#GetMapping("/budgets")
public ResponseEntity<List<Budget>> getMyBudgets() {
List<Budget> budgets = budgetRepository.findByUserLogin(SecurityUtils.getCurrentUserLogin());
return ResponseEntity.ok().body(budgets);
}
and then the repository no longer needs a #Query:
public interface BudgetRepository extends JpaRepository<Budget,Long> {
List<Budget> findAll();
List<Budget> findByUserLogin(String login);
}
As a bonus, you can use same method for an administrator that would want to find the budgets of any user.

RxJava 2 doesn't tell the error line

It seems to me that RxJava doesn't give good exception tracing.
One example is this which I got from Eclipse when running my unit test. It is not clear which code using the map function.
Is there any special trick to increase the tracing ability? Or is this considered as a bug?
2017-02-28 19:12:44/SGT INFO c.bt.nmdb.ndac.adapter.Bnmpv5Adapter : Current time stamp - Tue Dec 23 23:56:00 SGT 2014
2017-02-28 19:12:45/SGT TRACE c.bt.nmdb.ndac.adapter.Bnmpv5Adapter : #findAndSendDeviceCredentials()
io.reactivex.exceptions.OnErrorNotImplementedException: The mapper function returned a null value.
at io.reactivex.internal.functions.Functions$14.accept(Functions.java:229)
at io.reactivex.internal.functions.Functions$14.accept(Functions.java:226)
at io.reactivex.internal.subscribers.LambdaSubscriber.onError(LambdaSubscriber.java:75)
at io.reactivex.internal.operators.flowable.FlowableDoOnEach$DoOnEachSubscriber.onError(FlowableDoOnEach.java:109)
at io.reactivex.internal.subscribers.BasicFuseableSubscriber.onError(BasicFuseableSubscriber.java:100)
at io.reactivex.internal.subscribers.BasicFuseableSubscriber.fail(BasicFuseableSubscriber.java:110)
at io.reactivex.internal.operators.flowable.FlowableMap$MapSubscriber.onNext(FlowableMap.java:65)
at io.reactivex.internal.operators.flowable.FlowableFromArray$ArraySubscription.fastPath(FlowableFromArray.java:134)
at io.reactivex.internal.operators.flowable.FlowableFromArray$BaseArraySubscription.request(FlowableFromArray.java:87)
at io.reactivex.internal.subscribers.BasicFuseableSubscriber.request(BasicFuseableSubscriber.java:152)
at io.reactivex.internal.subscribers.BasicFuseableSubscriber.request(BasicFuseableSubscriber.java:152)
at io.reactivex.internal.subscribers.LambdaSubscriber.request(LambdaSubscriber.java:110)
at io.reactivex.internal.operators.flowable.FlowableInternalHelper$RequestMax.accept(FlowableInternalHelper.java:244)
at io.reactivex.internal.operators.flowable.FlowableInternalHelper$RequestMax.accept(FlowableInternalHelper.java:240)
at io.reactivex.internal.subscribers.LambdaSubscriber.onSubscribe(LambdaSubscriber.java:48)
at io.reactivex.internal.subscribers.BasicFuseableSubscriber.onSubscribe(BasicFuseableSubscriber.java:66)
at io.reactivex.internal.subscribers.BasicFuseableSubscriber.onSubscribe(BasicFuseableSubscriber.java:66)
at io.reactivex.internal.operators.flowable.FlowableFromArray.subscribeActual(FlowableFromArray.java:37)
at io.reactivex.Flowable.subscribe(Flowable.java:12901)
at io.reactivex.internal.operators.flowable.FlowableMap.subscribeActual(FlowableMap.java:37)
at io.reactivex.Flowable.subscribe(Flowable.java:12901)
at io.reactivex.internal.operators.flowable.FlowableDoOnEach.subscribeActual(FlowableDoOnEach.java:48)
at io.reactivex.Flowable.subscribe(Flowable.java:12901)
at io.reactivex.Flowable.subscribe(Flowable.java:12886)
at io.reactivex.Flowable.subscribe(Flowable.java:12746)
at michsan.adapter.Bnmpv5Adapter_On_FindAndSendDeviceCredentials_TestCase.shouldFindDeviceCredentialsAndBuildResponses(Bnmpv5Adapter_On_FindAndSendDeviceCredentials_TestCase.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.NullPointerException: The mapper function returned a null value.
at io.reactivex.internal.functions.ObjectHelper.requireNonNull(ObjectHelper.java:39)
at io.reactivex.internal.operators.flowable.FlowableMap$MapSubscriber.onNext(FlowableMap.java:63)
... 43 more
Exception in thread "main" io.reactivex.exceptions.OnErrorNotImplementedException: The mapper function returned a null value.
at io.reactivex.internal.functions.Functions$14.accept(Functions.java:229)
at io.reactivex.internal.functions.Functions$14.accept(Functions.java:226)
at io.reactivex.internal.subscribers.LambdaSubscriber.onError(LambdaSubscriber.java:75)
at io.reactivex.internal.operators.flowable.FlowableDoOnEach$DoOnEachSubscriber.onError(FlowableDoOnEach.java:109)
at io.reactivex.internal.subscribers.BasicFuseableSubscriber.onError(BasicFuseableSubscriber.java:100)
at io.reactivex.internal.subscribers.BasicFuseableSubscriber.fail(BasicFuseableSubscriber.java:110)
at io.reactivex.internal.operators.flowable.FlowableMap$MapSubscriber.onNext(FlowableMap.java:65)
at io.reactivex.internal.operators.flowable.FlowableFromArray$ArraySubscription.fastPath(FlowableFromArray.java:134)
at io.reactivex.internal.operators.flowable.FlowableFromArray$BaseArraySubscription.request(FlowableFromArray.java:87)
at io.reactivex.internal.subscribers.BasicFuseableSubscriber.request(BasicFuseableSubscriber.java:152)
at io.reactivex.internal.subscribers.BasicFuseableSubscriber.request(BasicFuseableSubscriber.java:152)
at io.reactivex.internal.subscribers.LambdaSubscriber.request(LambdaSubscriber.java:110)
at io.reactivex.internal.operators.flowable.FlowableInternalHelper$RequestMax.accept(FlowableInternalHelper.java:244)
at io.reactivex.internal.operators.flowable.FlowableInternalHelper$RequestMax.accept(FlowableInternalHelper.java:240)
at io.reactivex.internal.subscribers.LambdaSubscriber.onSubscribe(LambdaSubscriber.java:48)
at io.reactivex.internal.subscribers.BasicFuseableSubscriber.onSubscribe(BasicFuseableSubscriber.java:66)
at io.reactivex.internal.subscribers.BasicFuseableSubscriber.onSubscribe(BasicFuseableSubscriber.java:66)
at io.reactivex.internal.operators.flowable.FlowableFromArray.subscribeActual(FlowableFromArray.java:37)
at io.reactivex.Flowable.subscribe(Flowable.java:12901)
at io.reactivex.internal.operators.flowable.FlowableMap.subscribeActual(FlowableMap.java:37)
at io.reactivex.Flowable.subscribe(Flowable.java:12901)
at io.reactivex.internal.operators.flowable.FlowableDoOnEach.subscribeActual(FlowableDoOnEach.java:48)
at io.reactivex.Flowable.subscribe(Flowable.java:12901)
at io.reactivex.Flowable.subscribe(Flowable.java:12886)
at io.reactivex.Flowable.subscribe(Flowable.java:12746)
at michsan.adapter.Bnmpv5Adapter_On_FindAndSendDeviceCredentials_TestCase.shouldFindDeviceCredentialsAndBuildResponses(Bnmpv5Adapter_On_FindAndSendDeviceCredentials_TestCase.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.NullPointerException: The mapper function returned a null value.
at io.reactivex.internal.functions.ObjectHelper.requireNonNull(ObjectHelper.java:39)
at io.reactivex.internal.operators.flowable.FlowableMap$MapSubscriber.onNext(FlowableMap.java:63)
... 43 more
Building up on akarnokd's answer, you want to use RxJavaPlugins to add a hook for each Flowable, Observable,... For instance, as akarnokd does in his RxJava2Extensions library with Flowables:
RxJavaPlugins.setOnFlowableAssembly(new Function<Flowable, Flowable>() {
#Override
public Flowable apply(Flowable f) throws Exception {
// Play with the Stack, collect info
return new FlowableOnAssembly(f);
}
});
This way, you can collect info (the stack, for instance) in that FlowableOnAssembly. At this point, if you can capture crashes, you can already access that info you added (stacktrace, and whatever else you put into FlowableOnAssembly).
For easier debugging, even in remote devices, you can set a DefaultUncaughtExceptionHandler to capture any crash and print the info you collected in FlowableOnAssembly, for instance:
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
#Override
public void uncaughtException(Thread t, Throwable e) {
if (e instanceOf FlowableOnAssembly) {
// Print the info you captured
}
}
};
This mechanism will be intercepting everything RxJava2 does, so you probably want to put/remove enable/disable it easily. You can go use a small library I wrote for this purpose in:
RxJava2Debug
RxJava2Debug will create a nice simple StackTrace for you. Optionally, it will also filter it so the first entry of the root cause points to your code (this way services like Crashlytics can collate different errors for you).

Spring Boot #CacheEvict save(Iterable<S> entities);

I would like to add caching to my Spring Boot 4 application. As a key I would like to use id (type Long) of my entity, value is my entity.
My repository extends CrudRepository.
I cache data for findOne method:
public interface SampleEntityService extends CrudRepository<SampleEntity, Long> {
#Cacheable(key = "#a0", value = "findOne")
SampleEntity findOne(Long id);
So I would like to invalidate caches for method save iterable of entities (to be consistent):
#CacheEvict(value = FIND_ONE, key = "#entities.id")
<S extends SampleEntity> Iterable<S> save(Iterable<S> entities);
but when I call this method:
sampleEntityService.save(ImmutableList.of(entity1));
I got an exception:
org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 10): Property or field 'id' cannot be found on object of type 'com.google.common.collect.SingletonImmutableList' - maybe not public?
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:224)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:46)
at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:375)
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:88)
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120)
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:242)
at org.springframework.cache.interceptor.ExpressionEvaluator.key(ExpressionEvaluator.java:115)
at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.generateKey(CacheAspectSupport.java:621)
at org.springframework.cache.interceptor.CacheAspectSupport.performCacheEvict(CacheAspectSupport.java:406)
at org.springframework.cache.interceptor.CacheAspectSupport.processCacheEvicts(CacheAspectSupport.java:392)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:362)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:299)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy75.save(Unknown Source)
at my.springboot.dao.sampleentity.SampleEntityCacheTest.testSampleEntityCaching_saveMany(SampleEntityCacheTest.java:119)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
-How can I fix this EL expression to invalidate all items in the cache which id is in the collection ?
-If this is not possible, how to make this method not supported (without writing specific implementation - I like interface magic) ?
You cannot use #CacheEvict the way you mentioned (with Iterable).
When using "key" attribute of #CacheEvict, the SpEL should point to a single id of the entity that is to be removed from specified cache(s).
It is not directly mentioned in the docs though (http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html#cache-annotations-evict)

How do I access a package protected class in Maven build?

Some code returns a package protected type which I am trying to mock. I was doing so using Class.forName, but Maven throws an IllegalAccessError now.
Here is a sample:
Class<?> itemSupportClasss = Class.forName("com.amazonaws.services.dynamodbv2.document.internal.IteratorSupport");
Iterator<Item> mockIterator = (Iterator<Item>) createMock(itemSupportClasss);
expect(mockIterator.hasNext()).andReturn(false);
ItemCollection<QueryOutcome> itemCollection = createMock(ItemCollection.class);
expect(((Iterable<Item>) itemCollection).iterator()).andReturn(mockIterator);
// And so on
When itemCollection.iterator() is called, I get the following error:
java.lang.IllegalAccessError: com/amazonaws/services/dynamodbv2/document/internal/IteratorSupport
at com.amazonaws.services.dynamodbv2.document.ItemCollection$$EnhancerByCGLIB$$14504e4e.iterator(<generated>)
at com.mycom.MyClass(MyClass.java:77)
at com.mycom.MyClass(MyClass.java:230)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Of course, the root issue is that Dynamo DB returns a package protected class as an iterator, but what can you do?
The real problem here is that you are trying to mock ItemCollection and have it return a real object. The only way you're going to be able to solve this problem, unfortunately, is using PowerMock:
https://code.google.com/p/powermock/wiki/Motivation
https://code.google.com/p/powermock/wiki/BypassEncapsulation
All you have to do, when using the PowerMockRunner, is put the package private class in #PrepareForTest and it should handle it for you. If this is not enough for you let me know in the comments.

Categories

Resources