下面列出了javax.ws.rs.core.MultivaluedHashMap#put() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* API Tests
*/
private ApiEntity initApiMocks() {
ApiEntity api = new ApiEntity();
api.setId(API_ID);
Principal user = () -> USERNAME;
when(apiService.findById(api.getId())).thenReturn(api);
when(securityContext.getUserPrincipal()).thenReturn(user);
Permission perm = mock(Permission.class);
when(perm.value()).thenReturn(RolePermission.API_ANALYTICS);
when(perm.acls()).thenReturn(new RolePermissionAction[]{RolePermissionAction.UPDATE});
when(permissions.value()).thenReturn(new Permission[]{perm});
UriInfo uriInfo = mock(UriInfo.class);
MultivaluedHashMap<String, String> map = new MultivaluedHashMap<>();
map.put("api", Collections.singletonList(api.getId()));
when(uriInfo.getPathParameters()).thenReturn(map);
when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);
return api;
}
/**
* APPLICATION Tests
*/
private ApplicationEntity initApplicationMocks() {
ApplicationEntity application = new ApplicationEntity();
application.setId(APPLICATION_ID);
Principal user = () -> USERNAME;
when(applicationService.findById(application.getId())).thenReturn(application);
when(securityContext.getUserPrincipal()).thenReturn(user);
Permission perm = mock(Permission.class);
when(perm.value()).thenReturn(RolePermission.APPLICATION_ANALYTICS);
when(perm.acls()).thenReturn(new RolePermissionAction[]{RolePermissionAction.UPDATE});
when(permissions.value()).thenReturn(new Permission[]{perm});
UriInfo uriInfo = mock(UriInfo.class);
MultivaluedHashMap<String, String> map = new MultivaluedHashMap<>();
map.put("application", Collections.singletonList(application.getId()));
when(uriInfo.getPathParameters()).thenReturn(map);
when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);
return application;
}
/**
* API Tests
*/
private ApiEntity initApiMocks() {
ApiEntity api = new ApiEntity();
api.setId(API_ID);
Principal user = () -> USERNAME;
when(apiService.findById(api.getId())).thenReturn(api);
when(securityContext.getUserPrincipal()).thenReturn(user);
Permission perm = mock(Permission.class);
when(perm.value()).thenReturn(RolePermission.API_ANALYTICS);
when(perm.acls()).thenReturn(new RolePermissionAction[] { RolePermissionAction.UPDATE });
when(permissions.value()).thenReturn(new Permission[] { perm });
UriInfo uriInfo = mock(UriInfo.class);
MultivaluedHashMap<String, String> map = new MultivaluedHashMap<>();
map.put("apiId", Collections.singletonList(api.getId()));
when(uriInfo.getPathParameters()).thenReturn(map);
when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);
return api;
}
/**
* APPLICATION Tests
*/
private ApplicationEntity initApplicationMocks() {
ApplicationEntity application = new ApplicationEntity();
application.setId(APPLICATION_ID);
Principal user = () -> USERNAME;
when(applicationService.findById(application.getId())).thenReturn(application);
when(securityContext.getUserPrincipal()).thenReturn(user);
Permission perm = mock(Permission.class);
when(perm.value()).thenReturn(RolePermission.APPLICATION_ANALYTICS);
when(perm.acls()).thenReturn(new RolePermissionAction[] { RolePermissionAction.UPDATE });
when(permissions.value()).thenReturn(new Permission[] { perm });
UriInfo uriInfo = mock(UriInfo.class);
MultivaluedHashMap<String, String> map = new MultivaluedHashMap<>();
map.put("applicationId", Collections.singletonList(application.getId()));
when(uriInfo.getPathParameters()).thenReturn(map);
when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);
return application;
}
@Before
public void setup() {
CypherUtil cypherUtil = new CypherUtil(graphDb, curieUtil);
addRelationship("http://x.org/#foo", "http://x.org/#fizz", OwlRelationships.RDFS_SUB_PROPERTY_OF);
addRelationship("http://x.org/#bar", "http://x.org/#baz", OwlRelationships.RDFS_SUB_PROPERTY_OF);
addRelationship("http://x.org/#1", "http://x.org/#2", RelationshipType.withName("http://x.org/#fizz"));
when(context.getUriInfo()).thenReturn(uriInfo);
MultivaluedHashMap<String, String> map = new MultivaluedHashMap<>();
map.put("rel_id", newArrayList("http://x.org/#fizz"));
when(uriInfo.getQueryParameters()).thenReturn(map);
map = new MultivaluedHashMap<>();
map.put("pathParam", newArrayList("pathValue"));
when(uriInfo.getPathParameters()).thenReturn(map);
when(curieUtil.getIri(anyString())).thenReturn(Optional.<String>empty());
when(curieUtil.getCurie(anyString())).thenReturn(Optional.<String>empty());
when(curieUtil.getIri("X:foo")).thenReturn(Optional.of("http://x.org/#foo"));
inflector = new CypherInflector(graphDb, cypherUtil, curieUtil, "dynamic", path, new HashMap<String, GraphAspect>());
}
@Override
public Entity getEntity(MultiValuedMap fields, String mediaType) {
MultivaluedHashMap<String, Object> map = new MultivaluedHashMap<>();
for (Entry<String, List> entry : fields.entrySet()) {
map.put(entry.getKey(), entry.getValue());
}
// special handling, charset is not valid in content-type header here
int pos = mediaType.indexOf(';');
if (pos != -1) {
mediaType = mediaType.substring(0, pos);
}
MediaType mt = MediaType.valueOf(mediaType);
return Entity.entity(map, mt);
}
private void givenHeaders(String... headers) {
MultivaluedHashMap<String, String> mockHeaders = new MultivaluedHashMap<>();
for (int i = 0; i < headers.length / 2; i++) {
String hName = headers[2 * i];
String hValue = headers[(2 * i) + 1];
mockHeaders.put(hName, Collections.singletonList(hValue));
}
when(containerRequestContext.getHeaders()).thenReturn(mockHeaders);
}
@Test
public void mergesPathAndQueryParams() {
MultivaluedHashMap<String, String> pathParamMap = new MultivaluedHashMap<>();
pathParamMap.put("pathParam", newArrayList("paramValue"));
MultivaluedHashMap<String, String> queryParamMap = new MultivaluedHashMap<>();
pathParamMap.put("rel_id", newArrayList("fizz"));
when(uriInfo.getPathParameters()).thenReturn(pathParamMap);
when(uriInfo.getQueryParameters()).thenReturn(queryParamMap);
Multimap<String, Object> actual = MultivaluedMapUtils.merge(uriInfo);
assertThat(actual.get("pathParam"), contains((Object)"paramValue"));
assertThat(actual.get("rel_id"), contains((Object)"fizz"));
}
@Test
public void splitsMultivaluedPathParams() {
MultivaluedHashMap<String, String> paramMap = new MultivaluedHashMap<>();
paramMap.put("key", newArrayList("value1+value2"));
Multimap<String, Object> actual = MultivaluedMapUtils.multivaluedMapToMultimap(paramMap, Optional.of('+'));
assertThat(actual.get("key"), contains((Object)"value1", (Object)"value2"));
}
@BeforeClass
public static void setup() {
try (Transaction tx = graphDb.beginTx()) {
Node node = graphDb.createNode();
Node node2 = graphDb.createNode();
node.createRelationshipTo(node2, RelationshipType.withName("foo"));
node.setProperty("foo", "bar");
tx.success();
}
when(context.getUriInfo()).thenReturn(uriInfo);
MultivaluedHashMap<String, String> map = new MultivaluedHashMap<>();
map.put("foo", newArrayList("bar"));
when(uriInfo.getQueryParameters()).thenReturn(map);
when(uriInfo.getPathParameters()).thenReturn(new MultivaluedHashMap<String, String>());
}
protected Invocation prepareResource(final String key, final List<String> text, final String sourceLanguage, final String destLanguage) {
Invocation.Builder builder = client.target(URL).request(MediaType.APPLICATION_JSON);
final MultivaluedHashMap entity = new MultivaluedHashMap();
entity.put("text", text);
entity.add("key", key);
if ((StringUtils.isBlank(sourceLanguage))) {
entity.add("lang", destLanguage);
} else {
entity.add("lang", sourceLanguage + "-" + destLanguage);
}
return builder.buildPost(Entity.form(entity));
}
/**
* Test writeTo method.
*
* @throws Exception when a serious error occurs.
*/
@Test
public void testWriteTo() throws Exception {
ViewableWriter writer = new ViewableWriter();
Field mvcField = writer.getClass().getDeclaredField("mvc");
mvcField.setAccessible(true);
mvcField.set(writer, new MvcContextImpl());
ViewEngineFinder finder = EasyMock.createStrictMock(ViewEngineFinder.class);
Field finderField = writer.getClass().getDeclaredField("engineFinder");
finderField.setAccessible(true);
finderField.set(writer, finder);
HttpServletRequest request = EasyMock.createStrictMock(HttpServletRequest.class);
Field requestField = writer.getClass().getDeclaredField("injectedRequest");
requestField.setAccessible(true);
requestField.set(writer, request);
Event<MvcEvent> dispatcher = EasyMock.createStrictMock(Event.class);
Field dispatcherField = writer.getClass().getDeclaredField("dispatcher");
dispatcherField.setAccessible(true);
dispatcherField.set(writer, dispatcher);
EventDispatcher eventDispatcher = EasyMock.createMock(EventDispatcher.class);
Field eventDispatcherField = writer.getClass().getDeclaredField("eventDispatcher");
eventDispatcherField.setAccessible(true);
eventDispatcherField.set(writer, eventDispatcher);
ViewEngine viewEngine = EasyMock.createStrictMock(ViewEngine.class);
HttpServletResponse response = EasyMock.createStrictMock(HttpServletResponse.class);
response.setContentType(eq("text/html;charset=UTF-8"));
expect(response.getCharacterEncoding()).andReturn("UTF-8");
Field responseField = writer.getClass().getDeclaredField("injectedResponse");
responseField.setAccessible(true);
responseField.set(writer, response);
Configuration config = EasyMock.createStrictMock(Configuration.class);
Field configField = writer.getClass().getDeclaredField("config");
configField.setAccessible(true);
configField.set(writer, config);
MultivaluedHashMap map = new MultivaluedHashMap();
ArrayList<MediaType> contentTypes = new ArrayList<>();
contentTypes.add(MediaType.TEXT_HTML_TYPE);
map.put("Content-Type", contentTypes);
Viewable viewable = new Viewable("myview");
viewable.setModels(new ModelsImpl());
expect(finder.find(anyObject())).andReturn(viewEngine);
viewEngine.processView((ViewEngineContext) anyObject());
replay(finder, request, viewEngine, response);
writer.writeTo(viewable, null, null, new Annotation[] {}, MediaType.TEXT_HTML_TYPE, map, null);
verify(finder, request, viewEngine, response);
}
/**
* Test writeTo method.
*
* @throws Exception when a serious error occurs.
*/
@Test
public void testWriteTo() throws Exception {
ViewableWriter writer = new ViewableWriter();
Field mvcField = writer.getClass().getDeclaredField("mvc");
mvcField.setAccessible(true);
mvcField.set(writer, new MvcContextImpl());
ViewEngineFinder finder = EasyMock.createStrictMock(ViewEngineFinder.class);
Field finderField = writer.getClass().getDeclaredField("engineFinder");
finderField.setAccessible(true);
finderField.set(writer, finder);
HttpServletRequest request = EasyMock.createStrictMock(HttpServletRequest.class);
Field requestField = writer.getClass().getDeclaredField("injectedRequest");
requestField.setAccessible(true);
requestField.set(writer, request);
Event<MvcEvent> dispatcher = EasyMock.createStrictMock(Event.class);
Field dispatcherField = writer.getClass().getDeclaredField("dispatcher");
dispatcherField.setAccessible(true);
dispatcherField.set(writer, dispatcher);
ViewEngine viewEngine = EasyMock.createStrictMock(ViewEngine.class);
HttpServletResponse response = EasyMock.createStrictMock(HttpServletResponse.class);
Field responseField = writer.getClass().getDeclaredField("injectedResponse");
responseField.setAccessible(true);
responseField.set(writer, response);
Configuration config = EasyMock.createStrictMock(Configuration.class);
Field configField = writer.getClass().getDeclaredField("config");
configField.setAccessible(true);
configField.set(writer, config);
MultivaluedHashMap map = new MultivaluedHashMap();
ArrayList<MediaType> contentTypes = new ArrayList<>();
contentTypes.add(MediaType.TEXT_HTML_TYPE);
map.put("Content-Type", contentTypes);
Viewable viewable = new Viewable("myview");
viewable.setModels(new ModelsImpl());
expect(finder.find(anyObject())).andReturn(viewEngine);
viewEngine.processView((ViewEngineContext) anyObject());
replay(finder, request, viewEngine, response);
writer.writeTo(viewable, null, null, new Annotation[] {}, MediaType.WILDCARD_TYPE, map, null);
verify(finder, request, viewEngine, response);
}