Skip to content

Commit

Permalink
Merge pull request #13 from mcanoy/option-key-value
Browse files Browse the repository at this point in the history
add apis to get options for engagements, regions and artifacts
  • Loading branch information
mcanoy authored Feb 11, 2022
2 parents 46999a1 + 44b7466 commit b901912
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.redhat.labs.lodestar.resource;

import java.util.Map;
import java.util.Optional;

import javax.inject.Inject;
Expand All @@ -25,6 +26,24 @@ public class RuntimeConfigResource {
public String get(@QueryParam("type") Optional<String> type) {
return configService.getRuntimeConfiguration(type);
}

@GET
@Path("artifact/options")
public Map<Object, Object> getArtifactOptions() {
return configService.getArtifactOptions();
}

@GET
@Path("engagement/options")
public Map<String, String> getEngagementOptions() {
return configService.getEngagementOptions();
}

@GET
@Path("region/options")
public Map<String, String> getRegionOptions() {
return configService.getEngagementRegionOptions();
}

@GET
@Path("rbac")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,41 +65,57 @@ void createRuntimeConfigurations() {
baseConfiguration.loadFromConfigMapIfChanged();

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

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

}

@SuppressWarnings("unchecked")
public Map<Object, Object> getArtifactOptions() {
Map<String, Object> configuration = baseConfiguration.getConfiguration();
List<Map<String, Object>> typesList = Optional.of(configuration)
.map(m -> (Map<String, Object>) m.get("artifact_options"))
.map(m -> (Map<String, Object>) m.get("types"))
.map(m -> (List<Map<String, Object>>) m.get("options")).orElse(new ArrayList<>());

return typesList.stream().collect(Collectors.toMap(s -> s.get("value"), s -> s.get("label")));
}


/**
* Returns a {@link List} of {@link String} engagement type values from the
* configured base {@link RuntimeConfiguration}. Otherwise, an empty
* {@link List} is returned.
*
* @return
* Returns a {@link Map} of {@link String} engagement type key / values from the
* configured base {@link RuntimeConfiguration}. Empty if none found
*
* @return a map of engagements
*/
@SuppressWarnings("unchecked")
List<String> getEngagementTypes() {
public Map<String, String> getEngagementOptions() {
return getEngagementOptions("engagement_types");
}

public Map<String, String> getEngagementRegionOptions() {
return getEngagementOptions("engagement_regions");
}

@SuppressWarnings("unchecked")
private Map<String, String> getEngagementOptions(String type) {
Map<String, Object> configuration = baseConfiguration.getConfiguration();
List<Map<String, Object>> typesList = Optional.of(configuration)
.map(m -> (Map<String, Object>) m.get("basic_information"))
.map(m -> (Map<String, Object>) m.get("engagement_types"))
.map(m -> (Map<String, Object>) m.get(type))
.map(m -> (List<Map<String, Object>>) m.get("options")).orElse(new ArrayList<>());

return typesList.stream().flatMap(m -> m.entrySet().stream()).filter(e -> e.getKey().equals("value"))
.map(e -> e.getValue().toString()).collect(Collectors.toList());

return typesList.stream().collect(Collectors.toMap(s -> (String) s.get("value"), s -> (String) s.get("label")));
}

/**
* Creates and adds a {@link RuntimeConfiguration} to the override
* configurations {@link Map} for the given engagement type.
*
* @param engagementType
*
* @param engagementType - the type of engagement
*/
void createOverrideConfig(String engagementType) {
void createOverrideConfig(String engagementType) {
String filePath = this.runtimeBaseConfig.replaceAll("base", engagementType);
RuntimeConfiguration rc = RuntimeConfiguration.builder().filePath(filePath).build();
if(rc.checkPath()) {
Expand All @@ -116,29 +132,33 @@ void createOverrideConfig(String engagementType) {
* configuration will be returned.
*
* @param engagementType
* @return
* @return a map of runtime configurations
*/
public String getRuntimeConfiguration(Optional<String> engagementType) {
public Map<String, Object> mapRuntimeConfiguration(Optional<String> engagementType) {

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

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

Map<String, Object> override = overrideConfigurations.get(engagementType.get()).getConfiguration();
return jsonb.toJson(MarshalUtils.merge(base, override));
base = MarshalUtils.merge(base, override);

}

return jsonb.toJson(base);
return base;

}

public String getRuntimeConfiguration(Optional<String> engagementType) {
return jsonb.toJson(mapRuntimeConfiguration(engagementType));
}

/**
* Returns a mapping of engagement type to rbac access indicating the groups
* that can write engagements. The purpose is to inform the front end on a user's
* ability to write data for an engagement
* @return
* @return a map of permissions per group
*/
@SuppressWarnings("unchecked")
public Map<String, Set<String>> getPermission() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import com.fasterxml.jackson.databind.JavaType;
import com.redhat.labs.lodestar.model.GitlabHook;
import com.redhat.labs.lodestar.model.GitlabHookConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

import static io.restassured.RestAssured.given;
import static io.restassured.path.json.JsonPath.from;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.restassured.path.json.JsonPath;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

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

@Test
void testGetRuntimeConfigurationNoType() {
String response = ResourceLoader.load("expected/service-base-config.json");
given().when().contentType(ContentType.JSON).get().then().statusCode(200).body(is(response));
String expectedResponse = ResourceLoader.load("expected/service-base-config.json");
String response = given().when().contentType(ContentType.JSON).get()
.then().statusCode(200).extract().asString();

Assertions.assertEquals(expectedResponse, response);

}

@Test
void testGetRuntimeConfigurationTypeOne() {
String response = ResourceLoader.load("expected/service-get-type-one-config.json");
given().when().contentType(ContentType.JSON).queryParam("type", "TypeOne").get().then().statusCode(200).body(is(response));
void testGetRuntimeConfigurationTypeOne() throws Exception {
String expectedResponse = ResourceLoader.load("expected/service-get-type-one-config.json");
String response = given().when().contentType(ContentType.JSON).queryParam("type", "TypeOne").get()
.then().statusCode(200).extract().asString();

Assertions.assertEquals(expectedResponse, response);

}

@Test
void testGetRuntimeConfigurationTypeTwo() {
String response = ResourceLoader.load("expected/service-get-type-two-config.json");
given().when().contentType(ContentType.JSON).queryParam("type", "TypeTwo").get().then().statusCode(200).body(is(response));
String expectedResponse = ResourceLoader.load("expected/service-get-type-two-config.json");
String response = given().when().contentType(ContentType.JSON).queryParam("type", "TypeTwo").get()
.then().statusCode(200).extract().asString();

Assertions.assertEquals(expectedResponse, response);

}

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

}

@Test
void testGetArtifactOptions() {
given().when().contentType(ContentType.JSON).get("artifact/options")
.then().statusCode(200).body("size()", equalTo(1))
.body("flyer", equalTo("Flyer"));

}

@Test
void testGetEngagementRegionOptions() {
given().when().contentType(ContentType.JSON).get("region/options")
.then().statusCode(200).body("size()", equalTo(1))
.body("tuscana", equalTo("Tuscony"));

}

@Test
void testGetEngagementOptions() {
given().when().contentType(ContentType.JSON).get("engagement/options")
.then().statusCode(200).body("size()", equalTo(3))
.body("TypeOne", equalTo("Type One"));

}

}
4 changes: 4 additions & 0 deletions src/test/resources/expected/artifact-options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

{
"flyer": "Flyer"
}
18 changes: 18 additions & 0 deletions src/test/resources/expected/service-base-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
"default": true
}
]
},
"engagement_regions": {
"options": [
{
"label": "Tuscony",
"value": "tuscana"
}
]
}
},
"more_information": {
Expand Down Expand Up @@ -49,5 +57,15 @@
"velour-writer",
"writer"
]
},
"artifact_options": {
"types": {
"options": [
{
"value": "flyer",
"label": "Flyer"
}
]
}
}
}
18 changes: 18 additions & 0 deletions src/test/resources/expected/service-get-type-one-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
"writer"
]
},
"artifact_options": {
"types": {
"options": [
{
"value": "flyer",
"label": "Flyer"
}
]
}
},
"basic_information": {
"engagement_types": {
"options": [
Expand All @@ -34,6 +44,14 @@
"default": true
}
]
},
"engagement_regions": {
"options": [
{
"label": "Tuscony",
"value": "tuscana"
}
]
}
},
"other_options": {
Expand Down
18 changes: 18 additions & 0 deletions src/test/resources/expected/service-get-type-two-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
"writer"
]
},
"artifact_options": {
"types": {
"options": [
{
"value": "flyer",
"label": "Flyer"
}
]
}
},
"basic_information": {
"engagement_types": {
"options": [
Expand All @@ -34,6 +44,14 @@
"default": true
}
]
},
"engagement_regions": {
"options": [
{
"label": "Tuscony",
"value": "tuscana"
}
]
}
},
"other_options": {
Expand Down
9 changes: 9 additions & 0 deletions src/test/resources/runtime-config-base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ basic_information:
- label: TypeThree
value: TypeThree
default: true
engagement_regions:
options:
- label: Tuscony
value: tuscana
more_information:
some_value: hello
another_value: base
Expand All @@ -28,3 +32,8 @@ rbac:
velour:
- velour-writer
- writer
artifact_options:
types:
options:
- value: flyer
label: Flyer

0 comments on commit b901912

Please sign in to comment.