Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring Model Profile Test #750

Merged
merged 11 commits into from
Dec 6, 2022
28 changes: 20 additions & 8 deletions src/test/java/org/opensearch/ad/model/ModelProfileTests.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/*
* 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.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.ad.model;

import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder;
Expand All @@ -20,24 +31,25 @@ public void testToXContent() throws IOException {
Entity.createSingleAttributeEntity(randomAlphaOfLength(5), randomAlphaOfLength(5)),
0
);
XContentBuilder builder = jsonBuilder();
builder.startObject();
profile1.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
XContentBuilder builder = getBuilder(profile1);
String json = Strings.toString(builder);
assertTrue(JsonDeserializer.hasChildNode(json, CommonName.ENTITY_KEY));
assertFalse(JsonDeserializer.hasChildNode(json, CommonName.MODEL_SIZE_IN_BYTES));

ModelProfile profile2 = new ModelProfile(randomAlphaOfLength(5), null, 1);

builder = jsonBuilder();
builder.startObject();
profile2.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
builder = getBuilder(profile2);
json = Strings.toString(builder);

assertFalse(JsonDeserializer.hasChildNode(json, CommonName.ENTITY_KEY));
assertTrue(JsonDeserializer.hasChildNode(json, CommonName.MODEL_SIZE_IN_BYTES));
}

private XContentBuilder getBuilder(ModelProfile profile) throws IOException {
XContentBuilder builder = jsonBuilder();
builder.startObject();
profile.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
return builder;
}
}