I have a hybris website with a single mapping. Each time I try to access a particular url:
Is there any way to debug such an error?
This is the controller of the page:
#Controller
#RequestMapping(value = "/cart")
public class CartPageController extends AbstractPageController
{
private static final String CART_CMS_PAGE = "cartPage";
private static final Integer DEFAULT_COOKIE_EXPIRY_AGE = 5184000;
private static final String DEFAULT_CART_IDENTIFIER_COOKIE_NAME = "cart.identifier.cookie.name";
private static final Logger LOG = Logger.getLogger(CartPageController.class);
#Resource(name = "cartFacade")
private CartFacade cartFacade;
#Resource(name = "userService")
private UserService userService;
#Resource(name = "baseStoreService")
private BaseStoreService baseStoreService;
#Resource(name = "catalogVersionService")
private CatalogVersionService catalogVersionService;
#RequestMapping(method = RequestMethod.GET)
public String showCart(HttpServletRequest request, HttpServletResponse response, final Model model)
throws CMSItemNotFoundException
{
CartData cartData = cartFacade.getSessionCartWithEntryOrdering(true);
final String cartCookieIdentifier = getCartCookieIdentifier();
if (!cartFacade.hasEntries())
{
final String cartId = getCookieValue(request, cartCookieIdentifier);
final Optional<CartData> cartDataOptional = cartFacade.getCartsForCurrentUser().stream()
.filter(c -> c.getCode().equals(cartId)).findFirst();
if (cartDataOptional.isPresent())
{
cartData = cartDataOptional.get();
}
}
setCookieValue(response, cartCookieIdentifier, cartData.getCode());
model.addAttribute("cart", cartData);
setupPageModel(model);
String model1 = getViewForPage(model);
return model1;
}
protected void setupPageModel(Model model) throws CMSItemNotFoundException
{
storeCmsPageInModel(model, getContentPageForLabelOrId(CART_CMS_PAGE));
setUpMetaDataForContentPage(model, getContentPageForLabelOrId(CART_CMS_PAGE));
}
protected String getCookieValue(final HttpServletRequest request, final String cookieName)
{
return Arrays.stream(request.getCookies())
.filter(c -> c.getName().equals(cookieName))
.findFirst()
.map(Cookie::getValue)
.orElse(null);
}
protected void setCookieValue(final HttpServletResponse response, final String cookieName, final String cookieValue)
{
final Cookie cookie = new Cookie(cookieName, cookieValue);
cookie.setMaxAge(DEFAULT_COOKIE_EXPIRY_AGE);
response.addCookie(cookie);
}
protected String getCartCookieIdentifier()
{
final String baseStoreId = getCurrentBaseStoreId();
final String catalogId = getCurrentProductCatalogId();
if (StringUtils.isNotEmpty(baseStoreId) && StringUtils.isNotEmpty(catalogId))
{
return baseStoreId + "-" + catalogId;
}
return DEFAULT_CART_IDENTIFIER_COOKIE_NAME;
}
protected String getCurrentBaseStoreId()
{
final BaseStoreModel baseStore = baseStoreService.getCurrentBaseStore();
return baseStore == null ? StringUtils.EMPTY : baseStore.getUid();
}
protected String getCurrentProductCatalogId()
{
for (final CatalogVersionModel catalogVersionModel : catalogVersionService.getSessionCatalogVersions())
{
if (!((catalogVersionModel.getCatalog() instanceof ContentCatalogModel) || (catalogVersionModel
.getCatalog() instanceof ClassificationSystemModel)))
{
return catalogVersionModel.getCatalog().getId();
}
}
return StringUtils.EMPTY;
}
}
The content of the jsp page is not really that important since it can be empty and this behaviour still happens. I do not know exactly what could be the root of this. Is there any effective way to debug such issues?
This is a usual bug when creating B2B Sites. A workaround is to open the /smartedit and login to your site from there. Hybris will create a proper session and you should be able to open the site.
Possible long time solution:
If you are creating a B2B site, check spring-filter-config.xml in your Storefront extension and check this section. It should look like this:
<alias name="b2cAcceleratorSiteChannels" alias="acceleratorSiteChannels"/>
<util:set id="b2cAcceleratorSiteChannels" value-type="de.hybris.platform.commerceservices.enums.SiteChannel">
<ref bean="SiteChannel.B2C"/>
<ref bean="SiteChannel.B2B"/>
</util:set>
You can remove the SiteChannel.B2C if everything is fine
It looks like this behaviour was due to a bad argument when importing an impex related to this page:
INSERT_UPDATE PageTemplate;$contentCV[unique = true];uid[unique = true];name;frontendTemplateName;restrictedPageTypes(code);active[default = true]
;;CartPageTemplate;Cart Page Template;"cartPage";ContentPage;false;
should've been
INSERT_UPDATE PageTemplate;$contentCV[unique = true];uid[unique = true];name;frontendTemplateName;restrictedPageTypes(code);active[default = true]
;;CartPageTemplate;Cart Page Template;"cart/cartPage";ContentPage;false;
Replacing "cartPage" with "cart/cartPage" did all the magic.
Related
I have a problem when adding new test. And the problem is I think related to #DirtiesContext. I tried removing and adding it but nothing works in combination. Test 1 is using Application Context as well.
the following two are running together and no issue.
Test 1
#ActiveProfiles({"aws", "local"})
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UnauthorizedControllerTest {
private static final Logger LOGGER = LoggerFactory.getLogger(UnauthorizedControllerTest.class);
#Autowired
private TestRestTemplate testRestTemplate;
#LocalServerPort
private int port;
#Autowired
private ApplicationContext context;
private Map<Class<?>, List<String>> excludedMethodsPerController;
#Before
public void setUp() {
excludedMethodsPerController = excludedMethodsPerController();
}
#Test
public void contextStarts() {
assertNotNull(context);
}
#Test
public void controllerCall_WithoutAuthorization_ReturnsUnauthorized() {
Map<String, Object> controllerBeans = context.getBeansWithAnnotation(Controller.class);
for (Object controllerInstance : controllerBeans.values()) {
LOGGER.info("Checking controller {}", controllerInstance);
checkController(controllerInstance);
}
}
public void checkController(Object controllerInstance) {
// Use AopUtils for the case that spring wraps the controller in a proxy
Class<?> controllerClass = AopProxyUtils.ultimateTargetClass(controllerInstance);
Method[] allMethods = controllerClass.getDeclaredMethods();
for (Method method : allMethods) {
LOGGER.info("Checking method: {}", method.getName());
if (!isCallable(controllerClass, method)) {
continue;
}
String urlPrefix = urlPrefix(controllerClass);
Mapping mapping = Mapping.of(method.getAnnotations());
for (String url : mapping.urls) {
for (RequestMethod requestMethod : mapping.requestMethods) {
ResponseEntity<String> exchange = exchange(urlPrefix + url, requestMethod);
String message = String.format("Failing %s.%s", controllerClass.getName(), method.getName());
assertEquals(message, HttpStatus.UNAUTHORIZED, exchange.getStatusCode());
}
}
}
}
private ResponseEntity<String> exchange(String apiEndpoint, RequestMethod requestMethod) {
return testRestTemplate.exchange(url(replacePathVariables(apiEndpoint)), HttpMethod.resolve(requestMethod.name()), null, String.class);
}
private String urlPrefix(Class<?> aClass) {
if (!aClass.isAnnotationPresent(RequestMapping.class)) {
return "";
}
RequestMapping annotation = aClass.getAnnotation(RequestMapping.class);
return annotation.value()[0];
}
private String url(String url) {
return "http://localhost:" + port + url;
}
private boolean isCallable(Class<?> controller, Method method) {
return Modifier.isPublic(method.getModifiers())
&& !isExcluded(controller, method)
&& !isExternal(controller);
}
private boolean isExcluded(Class<?> controller, Method method) {
List<String> excludedMethodsPerController = this.excludedMethodsPerController.getOrDefault(controller, new ArrayList<>());
return excludedMethodsPerController.contains(method.getName());
}
private boolean isExternal(Class<?> controller) {
return controller.getName().startsWith("org.spring");
}
private String replacePathVariables(String url) {
return url.replaceAll("\\{[^\\/]+}", "someValue");
}
/**
* There must be a really good reason to exclude the method from being checked.
*
* #return The list of urls that must not be checked by the security
*/
private static Map<Class<?>, List<String>> excludedMethodsPerController() {
Map<Class<?>, List<String>> methodPerController = new HashMap<>();
methodPerController.put(AuthenticationController.class, Collections.singletonList("generateAuthorizationToken"));
methodPerController.put(SystemUserLoginController.class, Arrays.asList("systemUserLogin", "handleException"));
methodPerController.put(ValidationController.class, Collections.singletonList("isValid"));
return methodPerController;
}
}
Test 2
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
#ActiveProfiles({"aws", "local"})
public class RoleAdminControllerAuditTest {
private static final String DOMAIN_NAME = "testDomain";
private static final String APP_NAME_1 = "testApp_1";
private static final String APP_NAME_2 = "testApp_2";
private static final String ROLE_NAME = "testRole";
private static final String USER_NAME = "testUser";
#Autowired
AuditRepository auditRepository;
#Autowired
RoleAdminController roleAdminController;
#MockBean
RoleAdminService roleAdminService;
#MockBean
RoleAdminInfoBuilder infoBuilder;
#MockBean
AppInfoBuilder appInfoBuilder;
#MockBean
BoundaryValueService boundaryValueService;
#MockBean
RoleService roleService;
#MockBean
private SecurityService securityService;
private static final String URS_USER = "loggedInUser";
private static final String BOUNDARY_VALUE_KEY = "11";
private static final String BOUNDARY_VALUE_NAME = "Schulenberg";
private String auditEventDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
#BeforeClass
public static void setupTestEnv() {
// https://github.com/localstack/localstack/issues/592
System.setProperty("com.amazonaws.sdk.disableCbor", "true");
}
#Before
public void setUp() throws Exception {
auditRepository.clean();
when(securityService.getLoggedInUser()).thenReturn(new TestHelper.FakeUser(URS_USER));
//when(roleService.addRoleToApp(any(), any(), eq(ROLE_NAME))).thenReturn(TestHelper.initRole(ROLE_NAME));
when(boundaryValueService.findBoundaryValueById(eq(123L))).thenReturn(initBoundaryValue(BOUNDARY_VALUE_KEY, BOUNDARY_VALUE_NAME));
when(boundaryValueService.findBoundaryValueById(eq(666L))).thenReturn(initBoundaryValue(BOUNDARY_VALUE_KEY, BOUNDARY_VALUE_NAME));
}
#Test
public void addUserAsRoleAdminLogged() throws UserIsAlreadyRoleAdminException, RoleNotFoundException, BoundaryValueNotFoundException {
User user = initUser(USER_NAME);
List<RoleAdminInfo> roleAdminInfos = getRoleAdminInfos();
roleAdminController.addUserAsRoleAdmin(user, roleAdminInfos);
List<String> result = auditRepository.readAll();
assertEquals("some data", result.toString());
}
#Test
public void removeUserAsRoleAdminLogged() throws RoleNotFoundException, BoundaryValueNotFoundException {
User user = initUser(USER_NAME);
Long roleId = Long.valueOf(444);
Role role = initRole("test-role");
role.setApp(initApp("test-app"));
role.setDomain(initDomain("test-domain"));
when(roleService.getRoleByIdOrThrow(roleId)).thenReturn(role);
roleAdminController.removeUserAsRoleAdmin(user, roleId, Long.valueOf(666));
List<String> result = auditRepository.readAll();
assertEquals("some data", result.toString());
}
#Test
public void removeRoleAdminPermission() throws RoleNotFoundException, BoundaryValueNotFoundException {
User user = initUser(USER_NAME);
List<RoleAdminInfo> roleAdminInfos = getRoleAdminInfos();
roleAdminController.removeRoleAdminPermission(user, roleAdminInfos);
List<String> result = auditRepository.readAll();
assertEquals(1, result.size());
assertEquals("some data", result.toString());
}
private List<RoleAdminInfo> getRoleAdminInfos() {
RoleAdminInfo info1 = initRoleAdminInfo(DOMAIN_NAME, ROLE_NAME, APP_NAME_1);
RoleAdminInfo info2 = initRoleAdminInfo(DOMAIN_NAME, ROLE_NAME, APP_NAME_2);
info1.setBoundaryValueId(123L);
info1.setBoundaryValueKey(BOUNDARY_VALUE_KEY);
info1.setBoundaryValueName(BOUNDARY_VALUE_NAME);
return Arrays.asList(info1, info2);
}
}
Test 3 (newly added one)
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = FlywayConfig.class)
#DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
#ActiveProfiles({"aws", "local"})
public class BoundaryValueDeltaControllerTest {
private static final String API_V1 = "/api/v1/";
#Autowired
TestRestTemplate testRestTemplate;
#Autowired
private DomainBuilder domainBuilder;
#Autowired
private AppBuilder appBuilder;
#Autowired
private UserBuilder userBuilder;
#Autowired
private DomainAdminBuilder domainAdminBuilder;
#Autowired
private BoundarySetBuilder boundarySetBuilder;
#MockBean
private LoginUserProvider loginUserProvider;
#MockBean
private LoginTokenService loginTokenService;
#MockBean
private BoundaryServiceAdapter serviceAdapter;
#LocalServerPort
private int port;
LoginUserInfo loggedInUser;
#Before
public void setUp() {
clear();
}
#After
public void tearDown() {
clear();
}
#Test
public void updateBoundaryValuesFromApi() throws UrsBusinessException {
Domain domain = domainBuilder.persist();
App app = appBuilder.persist(domain);
BoundarySet boundarySet = boundarySetBuilder.persist(domain);
User user = userBuilder.persist(domain.getAuthor().getUsername());
aLoggedInUser(domain.getAuthor().getUsername());
domainAdminBuilder.persist(user, domain);
mockReadInfoFromApiUsingApp();
ResponseEntity<String> response = callUpdateBoundaryValuesFromApi(domain, boundarySet, app);
assertEquals(HttpStatus.OK, response.getStatusCode());
assertNotNull(response.getBody());
}
private void mockReadInfoFromApiUsingApp() throws UrsBusinessException {
BoundaryValueInfo boundaryValueInfo = new BoundaryValueInfo();
boundaryValueInfo.setBoundaryValueId(10L);
boundaryValueInfo.setBoundaryValueKey("boundaryValueKey");
boundaryValueInfo.setBoundaryValueName("boundaryValuename");
when(serviceAdapter.readInfoFromApiUsingApp(any(), any(), any())).thenReturn(new BoundaryValueInfo[]{boundaryValueInfo});
}
private ResponseEntity<String> callUpdateBoundaryValuesFromApi(Domain domain, BoundarySet boundarySet, App app) {
String url = url(API_V1 + "domains/" + domain.getName() + "/boundarysets/" + boundarySet.getBoundarySetName() + "/app/" + app.getName()+ "/updatefromapi/");
return testRestTemplate.exchange(url,HttpMethod.GET, null, String.class);
}
private String url(String url) {
return "http://localhost:" + port + url;
}
private void aLoggedInUser(String username) {
Claims claims = Jwts.claims();
claims.put("username", username);
loggedInUser = LoginUserInfo.parse(claims);
when(loginUserProvider.getLoggedInUser()).thenReturn(loggedInUser);
when(loginTokenService.parseToken(any())).thenReturn(loggedInUser);
}
private void clear() {
appBuilder.deleteAll();
boundarySetBuilder.deleteAll();
domainAdminBuilder.deleteAll();
domainBuilder.deleteAll();
userBuilder.deleteAll();
}
}
Flyway config
#TestConfiguration
public class FlywayConfig {
#Bean
public FlywayMigrationStrategy clean() {
return flyway -> {
flyway.clean();
flyway.migrate();
};
}
}
And I am getting below exception while running all together.
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java.....
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.internal.exception.FlywaySqlException:
Unable to obtain connection from database: Too many connections
---------------------------------------------------------------
SQL State : 08004
Error Code : 1040
Message : Too many connections
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1762)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
... 49 more
Caused by: org.flywaydb.core.internal.exception.FlywaySqlException:
Unable to obtain connection from database: Too many connections
I am struggling since yesterday's and you might find duplicate but I tried to add the more details today. please guide me here.
you must add the configuration for flyway
flyway.url=jdbc:postgresql://xxx.eu-west-2.rds.amazonaws.com:5432/xxx
flyway.user=postgres
flyway.password=xxx
I have a project that currently uses Spring Cloud Streams and RabbitMQ underneath. I've implemented a logic based on the documentation. See below:
#Component
public class ReRouteDlq {
private static final String ORIGINAL_QUEUE = "so8400in.so8400";
private static final String DLQ = ORIGINAL_QUEUE + ".dlq";
private static final String PARKING_LOT = ORIGINAL_QUEUE + ".parkingLot";
private static final String X_RETRIES_HEADER = "x-retries";
private static final String X_ORIGINAL_EXCHANGE_HEADER = RepublishMessageRecoverer.X_ORIGINAL_EXCHANGE;
private static final String X_ORIGINAL_ROUTING_KEY_HEADER = RepublishMessageRecoverer.X_ORIGINAL_ROUTING_KEY;
#Autowired
private RabbitTemplate rabbitTemplate;
#RabbitListener(queues = DLQ)
public void rePublish(Message failedMessage) {
Map<String, Object> headers = failedMessage.getMessageProperties().getHeaders();
Integer retriesHeader = (Integer) headers.get(X_RETRIES_HEADER);
if (retriesHeader == null) {
retriesHeader = Integer.valueOf(0);
}
if (retriesHeader < 3) {
headers.put(X_RETRIES_HEADER, retriesHeader + 1);
String exchange = (String) headers.get(X_ORIGINAL_EXCHANGE_HEADER);
String originalRoutingKey = (String) headers.get(X_ORIGINAL_ROUTING_KEY_HEADER);
this.rabbitTemplate.send(exchange, originalRoutingKey, failedMessage);
}
else {
this.rabbitTemplate.send(PARKING_LOT, failedMessage);
}
}
#Bean
public Queue parkingLot() {
return new Queue(PARKING_LOT);
}
}
It does what it is expected, however, it is binded to RabbitMQ, and my company is planning to stop using this message broker in one year or two (don't know why, must be some crazy business). So, I want to implement the same thing, but detach it from any message broker.
I tried changing the rePublish method this way, but it does not work:
#StreamListener(Sync.DLQ)
public void rePublish(Message failedMessage) {
Map<String, Object> headers = failedMessage.getHeaders();
Integer retriesHeader = (Integer) headers.get(X_RETRIES_HEADER);
if (retriesHeader == null) {
retriesHeader = Integer.valueOf(0);
}
if (retriesHeader < 3) {
headers.put(X_RETRIES_HEADER, retriesHeader + 1);
String exchange = (String) headers.get(X_ORIGINAL_EXCHANGE_HEADER);
String originalRoutingKey = (String) headers.get(X_ORIGINAL_ROUTING_KEY_HEADER);
this.rabbitTemplate.send(exchange, originalRoutingKey, failedMessage);
}
else {
this.rabbitTemplate.send(PARKING_LOT, failedMessage);
}
}
It fails because the Message class has immutable Headers - throws exception on the put attempt saying you can't change its values (uses org.springframework.messaging.Message class).
Is there a way to implement this dead-letter queue handler in a message broker independent way?
Use
MessageBuilder.fromMessage(message)
.setHeader("foo", "bar")
...
.build();
Note that the message in #StreamListener is a spring-messaging Message<?>, not a spring-amqp Message and can't be sent using the template that way; you need an output binding to send the message to.
I created app for getting info from upwork.com. I use java lib and Upwork OAuth 1.0. The problem is local request to API works fine, but when I do deploy to Google Cloud, my code does not work. I get ({"error":{"code":"503","message":"Exception: IOException"}}).
I create UpworkAuthClient for return OAuthClient and next it is used for requests in JobClient.
run() {
UpworkAuthClient upworkClient = new UpworkAuthClient();
upworkClient.setTokenWithSecret("USER TOKEN", "USER SECRET");
OAuthClient client = upworkClient.getOAuthClient();
//set query
JobQuery jobQuery = new JobQuery();
jobQuery.setQuery("query");
List<JobQuery> jobQueries = new ArrayList<>();
jobQueries.add(jobQuery);
// Get request of job
JobClient jobClient = new JobClient(client, jobQuery);
List<Job> result = jobClient.getJob();
}
public class UpworkAuthClient {
public static final String CONSUMERKEY = "UPWORK KEY";
public static final String CONSUMERSECRET = "UPWORK SECRET";
public static final String OAYTŠ CALLBACK = "https://my-app.com/main";
OAuthClient client ;
public UpworkAuthClient() {
Properties keys = new Properties();
keys.setProperty("consumerKey", CONSUMERKEY);
keys.setProperty("consumerSecret", CONSUMERSECRET);
Config config = new Config(keys);
client = new OAuthClient(config);
}
public void setTokenWithSecret (String token, String secret){
client.setTokenWithSecret(token, secret);
}
public OAuthClient getOAuthClient() {
return client;
}
public String getAuthorizationUrl() {
return this.client.getAuthorizationUrl(OAYTŠ CALLBACK);
}
}
public class JobClient {
private JobQuery jobQuery;
private Search jobs;
public JobClient(OAuthClient oAuthClient, JobQuery jobQuery) {
jobs = new Search(oAuthClient);
this.jobQuery = jobQuery;
}
public List<Job> getJob() throws JSONException {
JSONObject job = jobs.find(jobQuery.getQueryParam());
jobList = parseResponse(job);
return jobList;
}
}
Local dev server works fine, I get resilts on local machine, but in Cloud not.
I will be glad to any ideas, thanks!
{"error":{"code":"503","message":"Exception: IOException"}}
doesn't seem like a response return by Upwork API. Could you please provide the full response including the returned headers? So, we will take a more precise look into it.
Below is my test method.
#Test
public void testSaveUserPreference() throws Exception {
final long userId = 1L;
final String category = "category";
final UserPreference userPreference = createMockedUserPreference(userId, category);
final UserPreferenceDTO userPreferenceDTO = createMockedUserPreferenceDTO(userId, category);
final Date lastAccessed = userPreference.getLastAccessedDate();
userPreferenceDTO.setLastAccessedDate(lastAccessed);
//override the convert method to control the object being "saved"
final UserPreference document = new UserPreference();
new MockUp<UserPreferenceUtils>() {
#Mock
UserPreference convertFromDTO(UserPreferenceDTO dto) {
document.setUserId(userPreference.getUserId());
document.setCategory(userPreference.getCategory());
document.setProperties(userPreference.getProperties());
return document;
}
};
new Expectations() {{
component.getUserPreference(userId, category);
returns(userPreference);
component.saveUserPreference(userPreference);
returns(userPreference);
}};
UserPreferenceDTO actual = service.savePreference(userPreferenceDTO);
assertNotNull(actual);
assertEquals(userPreference.getUserId(), actual.getUserId());
assertEquals(userPreference.getCategory(), actual.getCategory());
assertEquals(userPreference.getProperties(), actual.getProperties());
assertNotNull(actual.getCreatedDate());
assertTrue(lastAccessed.before(actual.getLastAccessedDate()));
}
Below is the service method in which I am facing the error.
#Transactional
public UserPreferenceDTO savePreference(UserPreferenceDTO userPreference) {
UserPreference preference = UserPreferenceUtils.convertFromDTO(userPreference);
UserPreference existingPreference = userPreferenceComponent.getUserPreference(userPreference.getUserId(), userPreference.getCategory());
if(existingPreference!=null && !CollectionUtils.isEmpty(existingPreference.getProperties())) {
Map<String,Object> report = (Map<String, Object>) preference.getProperties().get("favoriteFilters");
Map<String,Object> existingRep = (Map<String, Object>) existingPreference.getProperties().get("favoriteFilters");
existingRep.putAll(report);
existingPreference.getProperties().put("favoriteFilters",existingRep);
} else {
existingPreference = preference;
}
if (existingPreference.getCreatedDate() == null) {
existingPreference.setCreatedDate(new Date());
}
existingPreference.setLastAccessedDate(new Date());
UserPreferenceDTO savedPreference = UserPreferenceUtils.convertFromDocument(userPreferenceComponent.saveUserPreference(existingPreference));
return savedPreference;
}
When calling the save method in the second last line, it is giving this error.
mockit.internal.UnexpectedInvocation: Parameter "userPreference" of com.curaspan.platformsupportservice.components.preference.UserPreferenceComponent#saveUserPreference(com.curaspan.platformsupportservice.mongo.document.UserPreference userPreference) expected com.curaspan.platformsupportservice.mongo.document.UserPreference#3d3fcdb0, got com.curaspan.platformsupportservice.mongo.document.UserPreference#636be97c
The userPreference object in the Expectation and the actual method is not matching, though I have all the parameters same. Is there anything I am missing out?
I want to generate HTML5 valid document, but I have a problem with forms in my Tapestry app. I am using tapestry textfields like below:
<t:textfield t:id="specId" value="val" />
Tapestry generates html input element:
<input id="specId" name="specId" type="text"></input>
But element input is not valid in pair (with end tag </input>) and html validator yells: "Error: Stray end tag input.".
Is any way how to generate input tags in single form like
<input .../> ?
You can override MarkupWriterFactory service with its own MarkupModel that will abbreviate html5 void elements instead of rendering end tag.
public class Html5MarkupModel extends AbstractMarkupModel {
private static final Set<String> VOID_ELEMENTS = new HashSet<String>(Arrays.asList(
"area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"
));
public Html5MarkupModel(boolean useApostropheForAttributes) {
super(useApostropheForAttributes);
}
public EndTagStyle getEndTagStyle(String element) {
return VOID_ELEMENTS.contains(element) ? EndTagStyle.ABBREVIATE : EndTagStyle.REQUIRE;
}
public boolean isXML() {
return false;
}
}
public class Html5MarkupWriterFactory implements MarkupWriterFactory {
private final PageContentTypeAnalyzer analyzer;
private final RequestPageCache cache;
private final MarkupModel htmlModel = new Html5MarkupModel(false);
private final MarkupModel htmlPartialModel = new Html5MarkupModel(true);
private final MarkupModel xmlModel = new XMLMarkupModel();
private final MarkupModel xmlPartialModel = new XMLMarkupModel(true);
public Html5MarkupWriterFactory(PageContentTypeAnalyzer analyzer, RequestPageCache cache) {
this.analyzer = analyzer;
this.cache = cache;
}
public MarkupWriter newMarkupWriter(ContentType contentType) {
return newMarkupWriter(contentType, false);
}
public MarkupWriter newPartialMarkupWriter(ContentType contentType) {
return newMarkupWriter(contentType, true);
}
public MarkupWriter newMarkupWriter(String pageName) {
return newMarkupWriter(analyzer.findContentType(cache.get(pageName)));
}
private MarkupWriter newMarkupWriter(ContentType contentType, boolean partial) {
boolean isHTML = contentType.getMimeType().equalsIgnoreCase("text/html");
MarkupModel model = partial
? (isHTML ? htmlPartialModel : xmlPartialModel)
: (isHTML ? htmlModel : xmlModel);
// The charset parameter sets the encoding attribute of the XML declaration, if
// not null and if using the XML model.
return new MarkupWriterImpl(model, contentType.getCharset());
}
}
And service override contribution:
#Contribute(ServiceOverride.class)
public void contributeServiceOverrides(MappedConfiguration<Class, Object> configuration,
ObjectLocator objectLocator) {
// use proxy instead of real service instance
// to prevent recursion on initialization cycle
configuration.add(MarkupWriterFactory.class,
objectLocator.proxy(MarkupWriterFactory.class, Html5MarkupWriterFactory.class));
}