|
1 | 1 | /* |
2 | | - * Copyright (c) 2001-2024, Inversoft Inc., All Rights Reserved |
| 2 | + * Copyright (c) 2001-2025, Inversoft Inc., All Rights Reserved |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
41 | 41 | import org.primeframework.mvc.message.l10n.MessageProvider; |
42 | 42 | import org.primeframework.mvc.parameter.el.ExpressionEvaluator; |
43 | 43 | import org.primeframework.mvc.validation.ValidationException; |
| 44 | +import org.testng.annotations.DataProvider; |
44 | 45 | import org.testng.annotations.Test; |
45 | 46 | import static org.easymock.EasyMock.createNiceMock; |
46 | 47 | import static org.easymock.EasyMock.createStrictMock; |
|
62 | 63 | public class JacksonContentHandlerTest extends PrimeBaseTest { |
63 | 64 | @Inject public ExpressionEvaluator expressionEvaluator; |
64 | 65 |
|
| 66 | + @DataProvider(name = "trueFalse") |
| 67 | + private static Object[][] getTrueFalse() { |
| 68 | + return new Object[][]{ |
| 69 | + {true}, |
| 70 | + {false} |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + @Test(dataProvider = "trueFalse") |
| 75 | + public void enum_values(boolean nested) throws IOException { |
| 76 | + Map<Class<?>, Object> additionalConfig = new HashMap<>(); |
| 77 | + Map<HTTPMethod, RequestMember> requestMembers = new HashMap<>(); |
| 78 | + requestMembers.put(HTTPMethod.POST, new RequestMember("jsonRequest", UserField.class)); |
| 79 | + additionalConfig.put(JacksonActionConfiguration.class, new JacksonActionConfiguration(requestMembers, null, null)); |
| 80 | + |
| 81 | + KitchenSinkAction action = new KitchenSinkAction(null); |
| 82 | + ActionConfiguration config = new ActionConfiguration(KitchenSinkAction.class, false, null, null, null, null, null, null, null, null, null, null, null, null, null, null, Collections.emptyList(), null, additionalConfig, null, null, null, null, null); |
| 83 | + ActionInvocationStore store = createStrictMock(ActionInvocationStore.class); |
| 84 | + expect(store.getCurrent()).andReturn( |
| 85 | + new ActionInvocation(action, new ExecuteMethodConfiguration(HTTPMethod.POST, null, null), "/action", null, config)); |
| 86 | + replay(store); |
| 87 | + |
| 88 | + String expected = nested ? """ |
| 89 | + { |
| 90 | + "nested": { |
| 91 | + "fruit": "bar" |
| 92 | + } |
| 93 | + } |
| 94 | + """ : """ |
| 95 | + { |
| 96 | + "fruit": "foo" |
| 97 | + } |
| 98 | + """; |
| 99 | + |
| 100 | + HTTPRequest request = new HTTPRequest(); |
| 101 | + request.setInputStream(new ByteArrayInputStream(expected.getBytes())); |
| 102 | + request.setContentLength((long) expected.getBytes().length); |
| 103 | + request.setContentType("application/json"); |
| 104 | + |
| 105 | + MessageProvider messageProvider = createStrictMock(MessageProvider.class); |
| 106 | + expect(messageProvider.getMessage(eq(nested ? "[invalid]nested.fruit" : "[invalid]fruit"), |
| 107 | + eq(nested ? "bar" : "foo"), |
| 108 | + eq("Apple, Orange"))).andReturn("Bad value"); |
| 109 | + replay(messageProvider); |
| 110 | + |
| 111 | + MessageStore messageStore = createStrictMock(MessageStore.class); |
| 112 | + messageStore.add(new SimpleFieldMessage(MessageType.ERROR, |
| 113 | + nested ? "nested.fruit" : "fruit", |
| 114 | + nested ? "[invalid]nested.fruit" : "[invalid]fruit", |
| 115 | + "Bad value")); |
| 116 | + replay(messageStore); |
| 117 | + |
| 118 | + JacksonContentHandler handler = new JacksonContentHandler(request, store, new ObjectMapper(), expressionEvaluator, messageProvider, messageStore); |
| 119 | + try { |
| 120 | + handler.handle(); |
| 121 | + fail("Should have thrown"); |
| 122 | + } catch (ValidationException e) { |
| 123 | + // Expected |
| 124 | + } |
| 125 | + |
| 126 | + assertNull(action.jsonRequest); |
| 127 | + |
| 128 | + verify(store, messageProvider, messageStore); |
| 129 | + } |
| 130 | + |
65 | 131 | @Test |
66 | 132 | public void handle() throws IOException { |
67 | 133 | Map<Class<?>, Object> additionalConfig = new HashMap<>(); |
|
0 commit comments