forked from opensearch-project/flow-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a Config XContent model for Config index (opensearch-project#679)
* Create a Config XContent model for Config index Signed-off-by: Daniel Widdis <[email protected]> * Move XContent builder into lambda Signed-off-by: Daniel Widdis <[email protected]> * Improve test coverage Signed-off-by: Daniel Widdis <[email protected]> * Even more test coverage Signed-off-by: Daniel Widdis <[email protected]> --------- Signed-off-by: Daniel Widdis <[email protected]>
- Loading branch information
Showing
7 changed files
with
282 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/main/java/org/opensearch/flowframework/model/Config.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
package org.opensearch.flowframework.model; | ||
|
||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.core.xcontent.ToXContentObject; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
import org.opensearch.flowframework.exception.FlowFrameworkException; | ||
import org.opensearch.flowframework.util.ParseUtils; | ||
|
||
import java.io.IOException; | ||
import java.time.Instant; | ||
|
||
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken; | ||
import static org.opensearch.flowframework.common.CommonValue.CREATE_TIME; | ||
import static org.opensearch.flowframework.common.CommonValue.MASTER_KEY; | ||
|
||
/** | ||
* Flow Framework Configuration | ||
*/ | ||
public class Config implements ToXContentObject { | ||
|
||
private final String masterKey; | ||
private final Instant createTime; | ||
|
||
/** | ||
* Instantiate this object | ||
* | ||
* @param masterKey The encryption master key | ||
* @param createTime The config creation time | ||
*/ | ||
public Config(String masterKey, Instant createTime) { | ||
this.masterKey = masterKey; | ||
this.createTime = createTime; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
XContentBuilder xContentBuilder = builder.startObject(); | ||
xContentBuilder.field(MASTER_KEY, this.masterKey); | ||
xContentBuilder.field(CREATE_TIME, this.createTime.toEpochMilli()); | ||
return xContentBuilder.endObject(); | ||
} | ||
|
||
/** | ||
* Parse raw xContent into a Config instance. | ||
* | ||
* @param parser xContent based content parser | ||
* @return an instance of the config | ||
* @throws IOException if content can't be parsed correctly | ||
*/ | ||
public static Config parse(XContentParser parser) throws IOException { | ||
String masterKey = null; | ||
Instant createTime = Instant.now(); | ||
|
||
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser); | ||
while (parser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
String fieldName = parser.currentName(); | ||
parser.nextToken(); | ||
switch (fieldName) { | ||
case MASTER_KEY: | ||
masterKey = parser.text(); | ||
break; | ||
case CREATE_TIME: | ||
createTime = ParseUtils.parseInstant(parser); | ||
break; | ||
default: | ||
throw new FlowFrameworkException( | ||
"Unable to parse field [" + fieldName + "] in a config object.", | ||
RestStatus.BAD_REQUEST | ||
); | ||
} | ||
} | ||
if (masterKey == null) { | ||
throw new FlowFrameworkException("The config object requires a master key.", RestStatus.BAD_REQUEST); | ||
} | ||
return new Config(masterKey, createTime); | ||
} | ||
|
||
/** | ||
* @return the masterKey | ||
*/ | ||
public String masterKey() { | ||
return masterKey; | ||
} | ||
|
||
/** | ||
* @return the createTime | ||
*/ | ||
public Instant createTime() { | ||
return createTime; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/test/java/org/opensearch/flowframework/model/ConfigTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
package org.opensearch.flowframework.model; | ||
|
||
import org.opensearch.common.xcontent.XContentFactory; | ||
import org.opensearch.core.common.bytes.BytesReference; | ||
import org.opensearch.core.xcontent.NamedXContentRegistry; | ||
import org.opensearch.core.xcontent.ToXContent; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
import org.opensearch.flowframework.exception.FlowFrameworkException; | ||
import org.opensearch.flowframework.util.ParseUtils; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
|
||
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class ConfigTests extends OpenSearchTestCase { | ||
private NamedXContentRegistry xContentRegistry; | ||
|
||
@Override | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
this.xContentRegistry = mock(NamedXContentRegistry.class); | ||
} | ||
|
||
public void testConfig() throws IOException { | ||
String masterKey = "foo"; | ||
Instant createTime = Instant.now().truncatedTo(ChronoUnit.MILLIS); | ||
Config config = new Config(masterKey, createTime); | ||
|
||
assertEquals(masterKey, config.masterKey()); | ||
assertEquals(createTime, config.createTime()); | ||
|
||
BytesReference bytesRef; | ||
try (XContentBuilder builder = XContentFactory.jsonBuilder()) { | ||
XContentBuilder source = config.toXContent(builder, ToXContent.EMPTY_PARAMS); | ||
bytesRef = BytesReference.bytes(source); | ||
} | ||
try (XContentParser parser = ParseUtils.createXContentParserFromRegistry(xContentRegistry, bytesRef)) { | ||
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser); | ||
config = Config.parse(parser); | ||
} | ||
assertEquals(masterKey, config.masterKey()); | ||
assertEquals(createTime, config.createTime()); | ||
} | ||
|
||
public void testBadConfig() throws IOException { | ||
BytesReference bytesRef; | ||
try (XContentBuilder builder = XContentFactory.jsonBuilder()) { | ||
builder.startObject().endObject(); | ||
bytesRef = BytesReference.bytes(builder); | ||
} | ||
try (XContentParser parser = ParseUtils.createXContentParserFromRegistry(xContentRegistry, bytesRef)) { | ||
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser); | ||
FlowFrameworkException e = assertThrows(FlowFrameworkException.class, () -> Config.parse(parser)); | ||
assertEquals("The config object requires a master key.", e.getMessage()); | ||
} | ||
|
||
try (XContentBuilder builder = XContentFactory.jsonBuilder()) { | ||
builder.startObject().field("foo", "bar").endObject(); | ||
bytesRef = BytesReference.bytes(builder); | ||
} | ||
try (XContentParser parser = ParseUtils.createXContentParserFromRegistry(xContentRegistry, bytesRef)) { | ||
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser); | ||
FlowFrameworkException e = assertThrows(FlowFrameworkException.class, () -> Config.parse(parser)); | ||
assertEquals("Unable to parse field [foo] in a config object.", e.getMessage()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.