Skip to content

Commit fd95faa

Browse files
authored
Merge pull request #39 from sbesson/microservicerequestctx_api
OmeroRequestCtx: add utility methods for parameters parsing
2 parents 739219a + bdea495 commit fd95faa

File tree

2 files changed

+222
-0
lines changed

2 files changed

+222
-0
lines changed

src/main/java/com/glencoesoftware/omero/ms/core/OmeroRequestCtx.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.HashMap;
2222
import java.util.Map;
2323

24+
import io.vertx.core.MultiMap;
25+
2426
import brave.Tracing;
2527
import brave.propagation.Propagation.Setter;
2628
import brave.propagation.TraceContext.Injector;
@@ -60,4 +62,77 @@ public void injectCurrentTraceContext() {
6062
injector.inject(
6163
Tracing.currentTracer().currentSpan().context(), traceContext);
6264
}
65+
66+
/**
67+
* Return the key of a multi-map
68+
* @param params a multi-map object
69+
* @param key the key to return
70+
* @return true if the key exists and has a value equal to true, false otherwise
71+
*/
72+
public static String getCheckedParam(MultiMap params, String key)
73+
throws IllegalArgumentException {
74+
String value = params.get(key);
75+
if (null == value) {
76+
throw new IllegalArgumentException("Missing parameter '"
77+
+ key + "'");
78+
}
79+
return value;
80+
}
81+
82+
/**
83+
* Return a key as a boolean from a multimap
84+
* @param params a MultiMap object
85+
* @param key a String specifying the key
86+
* @return true if the key exists and has a value equal to true, false otherwise
87+
*/
88+
public static Boolean getBooleanParameter(MultiMap params, String key) {
89+
if (params.get(key) != null) {
90+
String booleanString = params.get(key).toLowerCase();
91+
if (booleanString.equals("true")) {
92+
return true;
93+
}
94+
}
95+
return false;
96+
}
97+
98+
/**
99+
* Parse a string value as a valid image ID
100+
* @param imageIdString a value representing an image ID
101+
* @return the image ID as a Long
102+
* @throw an IllegalArgumentException if the string cannot be parsed as a Long
103+
* or if the parsed Long is not positive
104+
*/
105+
public static Long getImageIdFromString(String imageIdString)
106+
throws IllegalArgumentException{
107+
Long imageId;
108+
try {
109+
imageId = Long.parseLong(imageIdString);
110+
} catch (NumberFormatException e) {
111+
throw new IllegalArgumentException("Incorrect format for "
112+
+ "imageId parameter '" + imageIdString + "'");
113+
}
114+
if (imageId <= 0) {
115+
throw new IllegalArgumentException("Incorrect format for "
116+
+ "imageId parameter '" + imageIdString + "'");
117+
}
118+
return imageId;
119+
}
120+
121+
/**
122+
* Parse a string value as an Integer and return it
123+
* @param intString a value representing an integer
124+
* @return the value parsed as an Integer object
125+
* @throw an IllegalArgumentException if the string cannot be parsed as an integer
126+
*/
127+
public static Integer getIntegerFromString(String intString)
128+
throws IllegalArgumentException{
129+
Integer i = null;
130+
try {
131+
i = Integer.parseInt(intString);
132+
} catch (NumberFormatException e) {
133+
throw new IllegalArgumentException("Incorrect format for "
134+
+ "parameter value '" + intString + "'");
135+
}
136+
return i;
137+
}
63138
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Copyright (C) 2025 Glencoe Software, Inc. All rights reserved.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License along
15+
* with this program; if not, write to the Free Software Foundation, Inc.,
16+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17+
*/
18+
19+
package com.glencoesoftware.omero.ms.core;
20+
21+
import io.vertx.core.MultiMap;
22+
23+
import org.testng.Assert;
24+
import org.testng.annotations.DataProvider;
25+
import org.testng.annotations.Test;
26+
27+
28+
public class OmeroRequestCtxTest {
29+
30+
private MultiMap params = MultiMap.caseInsensitiveMultiMap();
31+
32+
@Test
33+
public void testGetCheckedParams() {
34+
params.add("imageId", "1");
35+
params.add("key1", "value1");
36+
params.add("key2", "value2");
37+
Assert.assertEquals(OmeroRequestCtx.getCheckedParam(params, "imageId"), "1");
38+
Assert.assertEquals(OmeroRequestCtx.getCheckedParam(params, "ImageID"), "1");
39+
Assert.assertEquals(OmeroRequestCtx.getCheckedParam(params, "key1"), "value1");
40+
Assert.assertEquals(OmeroRequestCtx.getCheckedParam(params, "key2"), "value2");
41+
}
42+
43+
@Test(
44+
expectedExceptions = {IllegalArgumentException.class},
45+
expectedExceptionsMessageRegExp="Missing parameter '.*'"
46+
)
47+
public void testGetCheckedParamsMissingKey() {
48+
OmeroRequestCtx.getCheckedParam(params, "missingkey");
49+
}
50+
51+
@Test
52+
public void testGetCheckedParamsMultipleValues() {
53+
params.add("imageId", "1");
54+
params.add("imageId", "2");
55+
params.add("imageId", "3");
56+
Assert.assertEquals(OmeroRequestCtx.getCheckedParam(params, "imageId"), "1");
57+
}
58+
59+
@Test
60+
public void testGetBooleanParameter() {
61+
params.add("imageId", "1");
62+
params.add("boolean1", "true");
63+
params.add("boolean2", "false");
64+
params.add("boolean3", "TRUE");
65+
params.add("boolean4", "FALSE");
66+
Assert.assertFalse(OmeroRequestCtx.getBooleanParameter(params, "imageId"));
67+
Assert.assertTrue(OmeroRequestCtx.getBooleanParameter(params, "boolean1"));
68+
Assert.assertFalse(OmeroRequestCtx.getBooleanParameter(params, "boolean2"));
69+
Assert.assertTrue(OmeroRequestCtx.getBooleanParameter(params, "boolean3"));
70+
Assert.assertFalse(OmeroRequestCtx.getBooleanParameter(params, "boolean4"));
71+
Assert.assertFalse(OmeroRequestCtx.getBooleanParameter(params, "missingkey"));
72+
}
73+
74+
@Test
75+
public void testGetBooleanParameterMultipleValue() {
76+
params.add("boolean1", "true");
77+
params.add("boolean1", "false");
78+
Assert.assertTrue(OmeroRequestCtx.getBooleanParameter(params, "boolean1"));
79+
}
80+
81+
@DataProvider(name = "valid image IDs")
82+
public Object[][] validLongs() {
83+
return new Object[][] {
84+
{"1", Long.valueOf(1)},
85+
{String.valueOf(Integer.MAX_VALUE), Long.valueOf(Integer.MAX_VALUE)},
86+
{String.valueOf(Long.MAX_VALUE), Long.valueOf(Long.MAX_VALUE)}
87+
};
88+
}
89+
90+
@Test(dataProvider = "valid image IDs")
91+
public void testGetImageIdFromString(String value, Long id) {
92+
Assert.assertEquals(OmeroRequestCtx.getImageIdFromString(value), id);
93+
}
94+
95+
@DataProvider(name = "invalid image IDs")
96+
public Object[][] invalidLongs() {
97+
return new Object[][] {
98+
{"0"},
99+
{"not a number"},
100+
{"0.1"},
101+
{String.valueOf(-Integer.MAX_VALUE)}
102+
};
103+
}
104+
105+
@Test(
106+
dataProvider = "invalid image IDs",
107+
expectedExceptions = {IllegalArgumentException.class},
108+
expectedExceptionsMessageRegExp="Incorrect format for imageId parameter '.*'"
109+
)
110+
public void testGetImageIdFromStringInvalid(String imageId) {
111+
OmeroRequestCtx.getImageIdFromString(imageId);
112+
}
113+
114+
@DataProvider(name = "valid integers")
115+
public Object[][] validInts() {
116+
return new Object[][] {
117+
{"0", Integer.valueOf(0)},
118+
{"1", Integer.valueOf(1)},
119+
{String.valueOf(Integer.MAX_VALUE), Integer.valueOf(Integer.MAX_VALUE)},
120+
{String.valueOf(-Integer.MAX_VALUE), Integer.valueOf(-Integer.MAX_VALUE)}
121+
};
122+
}
123+
124+
@Test(dataProvider = "valid integers")
125+
public void testGetIntegerFromString(String value, Integer integer) {
126+
Assert.assertEquals(OmeroRequestCtx.getIntegerFromString(value), integer);
127+
}
128+
129+
@DataProvider(name = "invalid integers")
130+
public Object[][] invalidInts() {
131+
return new Object[][] {
132+
{"not a number"},
133+
{"0.1" },
134+
{"0L"},
135+
{String.valueOf(Long.valueOf(Integer.MAX_VALUE) + 1)}
136+
};
137+
}
138+
139+
@Test(
140+
dataProvider = "invalid integers",
141+
expectedExceptions = {IllegalArgumentException.class},
142+
expectedExceptionsMessageRegExp="Incorrect format for parameter value '.*'"
143+
)
144+
public void testGetIntegerFromStringInvalid(String value) {
145+
OmeroRequestCtx.getIntegerFromString(value);
146+
}
147+
}

0 commit comments

Comments
 (0)