Skip to content

Commit b901912

Browse files
authored
Merge pull request #13 from mcanoy/option-key-value
add apis to get options for engagements, regions and artifacts
2 parents 46999a1 + 44b7466 commit b901912

9 files changed

Lines changed: 172 additions & 30 deletions

File tree

src/main/java/com/redhat/labs/lodestar/resource/RuntimeConfigResource.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.redhat.labs.lodestar.resource;
22

3+
import java.util.Map;
34
import java.util.Optional;
45

56
import javax.inject.Inject;
@@ -25,6 +26,24 @@ public class RuntimeConfigResource {
2526
public String get(@QueryParam("type") Optional<String> type) {
2627
return configService.getRuntimeConfiguration(type);
2728
}
29+
30+
@GET
31+
@Path("artifact/options")
32+
public Map<Object, Object> getArtifactOptions() {
33+
return configService.getArtifactOptions();
34+
}
35+
36+
@GET
37+
@Path("engagement/options")
38+
public Map<String, String> getEngagementOptions() {
39+
return configService.getEngagementOptions();
40+
}
41+
42+
@GET
43+
@Path("region/options")
44+
public Map<String, String> getRegionOptions() {
45+
return configService.getEngagementRegionOptions();
46+
}
2847

2948
@GET
3049
@Path("rbac")

src/main/java/com/redhat/labs/lodestar/service/RuntimeConfigService.java

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,41 +65,57 @@ void createRuntimeConfigurations() {
6565
baseConfiguration.loadFromConfigMapIfChanged();
6666

6767
// Get List of engagement types from base config
68-
List<String> engagementTypes = getEngagementTypes();
68+
Collection<String> engagementTypes = getEngagementOptions().keySet();
6969

70-
engagementTypes.stream().forEach(this::createOverrideConfig);
70+
engagementTypes.forEach(this::createOverrideConfig);
7171
LOGGER.debug("override configurations: {}", overrideConfigurations.keySet());
7272

7373
}
7474

75+
@SuppressWarnings("unchecked")
76+
public Map<Object, Object> getArtifactOptions() {
77+
Map<String, Object> configuration = baseConfiguration.getConfiguration();
78+
List<Map<String, Object>> typesList = Optional.of(configuration)
79+
.map(m -> (Map<String, Object>) m.get("artifact_options"))
80+
.map(m -> (Map<String, Object>) m.get("types"))
81+
.map(m -> (List<Map<String, Object>>) m.get("options")).orElse(new ArrayList<>());
82+
83+
return typesList.stream().collect(Collectors.toMap(s -> s.get("value"), s -> s.get("label")));
84+
}
85+
86+
7587
/**
76-
* Returns a {@link List} of {@link String} engagement type values from the
77-
* configured base {@link RuntimeConfiguration}. Otherwise, an empty
78-
* {@link List} is returned.
79-
*
80-
* @return
88+
* Returns a {@link Map} of {@link String} engagement type key / values from the
89+
* configured base {@link RuntimeConfiguration}. Empty if none found
90+
*
91+
* @return a map of engagements
8192
*/
82-
@SuppressWarnings("unchecked")
83-
List<String> getEngagementTypes() {
93+
public Map<String, String> getEngagementOptions() {
94+
return getEngagementOptions("engagement_types");
95+
}
8496

97+
public Map<String, String> getEngagementRegionOptions() {
98+
return getEngagementOptions("engagement_regions");
99+
}
100+
101+
@SuppressWarnings("unchecked")
102+
private Map<String, String> getEngagementOptions(String type) {
85103
Map<String, Object> configuration = baseConfiguration.getConfiguration();
86104
List<Map<String, Object>> typesList = Optional.of(configuration)
87105
.map(m -> (Map<String, Object>) m.get("basic_information"))
88-
.map(m -> (Map<String, Object>) m.get("engagement_types"))
106+
.map(m -> (Map<String, Object>) m.get(type))
89107
.map(m -> (List<Map<String, Object>>) m.get("options")).orElse(new ArrayList<>());
90108

91-
return typesList.stream().flatMap(m -> m.entrySet().stream()).filter(e -> e.getKey().equals("value"))
92-
.map(e -> e.getValue().toString()).collect(Collectors.toList());
93-
109+
return typesList.stream().collect(Collectors.toMap(s -> (String) s.get("value"), s -> (String) s.get("label")));
94110
}
95111

96112
/**
97113
* Creates and adds a {@link RuntimeConfiguration} to the override
98114
* configurations {@link Map} for the given engagement type.
99-
*
100-
* @param engagementType
115+
*
116+
* @param engagementType - the type of engagement
101117
*/
102-
void createOverrideConfig(String engagementType) {
118+
void createOverrideConfig(String engagementType) {
103119
String filePath = this.runtimeBaseConfig.replaceAll("base", engagementType);
104120
RuntimeConfiguration rc = RuntimeConfiguration.builder().filePath(filePath).build();
105121
if(rc.checkPath()) {
@@ -116,29 +132,33 @@ void createOverrideConfig(String engagementType) {
116132
* configuration will be returned.
117133
*
118134
* @param engagementType
119-
* @return
135+
* @return a map of runtime configurations
120136
*/
121-
public String getRuntimeConfiguration(Optional<String> engagementType) {
137+
public Map<String, Object> mapRuntimeConfiguration(Optional<String> engagementType) {
122138

123139
// get map for base configuration
124140
Map<String, Object> base = baseConfiguration.getConfiguration();
125141

126142
if (engagementType.isPresent() && overrideConfigurations.containsKey(engagementType.get())) {
127143

128144
Map<String, Object> override = overrideConfigurations.get(engagementType.get()).getConfiguration();
129-
return jsonb.toJson(MarshalUtils.merge(base, override));
145+
base = MarshalUtils.merge(base, override);
130146

131147
}
132148

133-
return jsonb.toJson(base);
149+
return base;
134150

135151
}
152+
153+
public String getRuntimeConfiguration(Optional<String> engagementType) {
154+
return jsonb.toJson(mapRuntimeConfiguration(engagementType));
155+
}
136156

137157
/**
138158
* Returns a mapping of engagement type to rbac access indicating the groups
139159
* that can write engagements. The purpose is to inform the front end on a user's
140160
* ability to write data for an engagement
141-
* @return
161+
* @return a map of permissions per group
142162
*/
143163
@SuppressWarnings("unchecked")
144164
public Map<String, Set<String>> getPermission() {

src/main/java/com/redhat/labs/lodestar/utils/MarshalUtils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import com.fasterxml.jackson.databind.JavaType;
88
import com.redhat.labs.lodestar.model.GitlabHook;
9-
import com.redhat.labs.lodestar.model.GitlabHookConfiguration;
109
import org.slf4j.Logger;
1110
import org.slf4j.LoggerFactory;
1211

src/test/java/com/redhat/labs/lodestar/resource/RuntimeConfigResourceTest.java

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
import static io.restassured.RestAssured.given;
44
import static io.restassured.path.json.JsonPath.from;
5-
import static org.hamcrest.CoreMatchers.is;
5+
import static org.hamcrest.CoreMatchers.*;
66
import static org.junit.jupiter.api.Assertions.assertEquals;
77
import static org.junit.jupiter.api.Assertions.assertTrue;
88

99
import java.util.List;
1010
import java.util.Map;
1111

12+
import com.fasterxml.jackson.databind.JsonNode;
13+
import com.fasterxml.jackson.databind.ObjectMapper;
14+
import io.restassured.path.json.JsonPath;
15+
import org.junit.jupiter.api.Assertions;
1216
import org.junit.jupiter.api.Test;
1317

1418
import com.redhat.labs.lodestar.utils.ResourceLoader;
@@ -43,22 +47,31 @@ void testGetPermission() {
4347

4448
@Test
4549
void testGetRuntimeConfigurationNoType() {
46-
String response = ResourceLoader.load("expected/service-base-config.json");
47-
given().when().contentType(ContentType.JSON).get().then().statusCode(200).body(is(response));
50+
String expectedResponse = ResourceLoader.load("expected/service-base-config.json");
51+
String response = given().when().contentType(ContentType.JSON).get()
52+
.then().statusCode(200).extract().asString();
53+
54+
Assertions.assertEquals(expectedResponse, response);
4855

4956
}
5057

5158
@Test
52-
void testGetRuntimeConfigurationTypeOne() {
53-
String response = ResourceLoader.load("expected/service-get-type-one-config.json");
54-
given().when().contentType(ContentType.JSON).queryParam("type", "TypeOne").get().then().statusCode(200).body(is(response));
59+
void testGetRuntimeConfigurationTypeOne() throws Exception {
60+
String expectedResponse = ResourceLoader.load("expected/service-get-type-one-config.json");
61+
String response = given().when().contentType(ContentType.JSON).queryParam("type", "TypeOne").get()
62+
.then().statusCode(200).extract().asString();
63+
64+
Assertions.assertEquals(expectedResponse, response);
5565

5666
}
5767

5868
@Test
5969
void testGetRuntimeConfigurationTypeTwo() {
60-
String response = ResourceLoader.load("expected/service-get-type-two-config.json");
61-
given().when().contentType(ContentType.JSON).queryParam("type", "TypeTwo").get().then().statusCode(200).body(is(response));
70+
String expectedResponse = ResourceLoader.load("expected/service-get-type-two-config.json");
71+
String response = given().when().contentType(ContentType.JSON).queryParam("type", "TypeTwo").get()
72+
.then().statusCode(200).extract().asString();
73+
74+
Assertions.assertEquals(expectedResponse, response);
6275

6376
}
6477

@@ -69,4 +82,28 @@ void testGetRuntimeConfigurationTypeUnknown() {
6982

7083
}
7184

85+
@Test
86+
void testGetArtifactOptions() {
87+
given().when().contentType(ContentType.JSON).get("artifact/options")
88+
.then().statusCode(200).body("size()", equalTo(1))
89+
.body("flyer", equalTo("Flyer"));
90+
91+
}
92+
93+
@Test
94+
void testGetEngagementRegionOptions() {
95+
given().when().contentType(ContentType.JSON).get("region/options")
96+
.then().statusCode(200).body("size()", equalTo(1))
97+
.body("tuscana", equalTo("Tuscony"));
98+
99+
}
100+
101+
@Test
102+
void testGetEngagementOptions() {
103+
given().when().contentType(ContentType.JSON).get("engagement/options")
104+
.then().statusCode(200).body("size()", equalTo(3))
105+
.body("TypeOne", equalTo("Type One"));
106+
107+
}
108+
72109
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
{
3+
"flyer": "Flyer"
4+
}

src/test/resources/expected/service-base-config.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
"default": true
1818
}
1919
]
20+
},
21+
"engagement_regions": {
22+
"options": [
23+
{
24+
"label": "Tuscony",
25+
"value": "tuscana"
26+
}
27+
]
2028
}
2129
},
2230
"more_information": {
@@ -49,5 +57,15 @@
4957
"velour-writer",
5058
"writer"
5159
]
60+
},
61+
"artifact_options": {
62+
"types": {
63+
"options": [
64+
{
65+
"value": "flyer",
66+
"label": "Flyer"
67+
}
68+
]
69+
}
5270
}
5371
}

src/test/resources/expected/service-get-type-one-config.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717
"writer"
1818
]
1919
},
20+
"artifact_options": {
21+
"types": {
22+
"options": [
23+
{
24+
"value": "flyer",
25+
"label": "Flyer"
26+
}
27+
]
28+
}
29+
},
2030
"basic_information": {
2131
"engagement_types": {
2232
"options": [
@@ -34,6 +44,14 @@
3444
"default": true
3545
}
3646
]
47+
},
48+
"engagement_regions": {
49+
"options": [
50+
{
51+
"label": "Tuscony",
52+
"value": "tuscana"
53+
}
54+
]
3755
}
3856
},
3957
"other_options": {

src/test/resources/expected/service-get-type-two-config.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717
"writer"
1818
]
1919
},
20+
"artifact_options": {
21+
"types": {
22+
"options": [
23+
{
24+
"value": "flyer",
25+
"label": "Flyer"
26+
}
27+
]
28+
}
29+
},
2030
"basic_information": {
2131
"engagement_types": {
2232
"options": [
@@ -34,6 +44,14 @@
3444
"default": true
3545
}
3646
]
47+
},
48+
"engagement_regions": {
49+
"options": [
50+
{
51+
"label": "Tuscony",
52+
"value": "tuscana"
53+
}
54+
]
3755
}
3856
},
3957
"other_options": {

src/test/resources/runtime-config-base.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ basic_information:
99
- label: TypeThree
1010
value: TypeThree
1111
default: true
12+
engagement_regions:
13+
options:
14+
- label: Tuscony
15+
value: tuscana
1216
more_information:
1317
some_value: hello
1418
another_value: base
@@ -28,3 +32,8 @@ rbac:
2832
velour:
2933
- velour-writer
3034
- writer
35+
artifact_options:
36+
types:
37+
options:
38+
- value: flyer
39+
label: Flyer

0 commit comments

Comments
 (0)