From 158cc7a3b3a5c71335f5efb0df06b5018e8c6cac Mon Sep 17 00:00:00 2001 From: Varun Jain Date: Mon, 5 Dec 2022 12:44:09 -0800 Subject: [PATCH 1/3] Model Profile Test Signed-off-by: Varun Jain --- .../ad/model/ModelProfileTests.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/test/java/org/opensearch/ad/model/ModelProfileTests.java diff --git a/src/test/java/org/opensearch/ad/model/ModelProfileTests.java b/src/test/java/org/opensearch/ad/model/ModelProfileTests.java new file mode 100644 index 000000000..012b5f99e --- /dev/null +++ b/src/test/java/org/opensearch/ad/model/ModelProfileTests.java @@ -0,0 +1,43 @@ +package org.opensearch.ad.model; + +import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder; + +import java.io.IOException; + +import org.opensearch.ad.AbstractADTest; +import org.opensearch.ad.constant.CommonName; +import org.opensearch.common.Strings; +import org.opensearch.common.xcontent.ToXContent; +import org.opensearch.common.xcontent.XContentBuilder; + +import test.org.opensearch.ad.util.JsonDeserializer; + +public class ModelProfileTests extends AbstractADTest { + + public void testToXContent() throws IOException { + ModelProfile profile1 = new ModelProfile( + randomAlphaOfLength(5), + Entity.createSingleAttributeEntity(randomAlphaOfLength(5), randomAlphaOfLength(5)), + 0 + ); + XContentBuilder builder = jsonBuilder(); + builder.startObject(); + profile1.toXContent(builder, ToXContent.EMPTY_PARAMS); + builder.endObject(); + 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(); + json = Strings.toString(builder); + + assertFalse(JsonDeserializer.hasChildNode(json, CommonName.ENTITY_KEY)); + assertTrue(JsonDeserializer.hasChildNode(json, CommonName.MODEL_SIZE_IN_BYTES)); + + } +} From 086eed963d1659ed266c167879465d31a01acd79 Mon Sep 17 00:00:00 2001 From: Varun Jain Date: Mon, 5 Dec 2022 21:11:07 -0800 Subject: [PATCH 2/3] Model Profile Test Signed-off-by: Varun Jain --- .../ad/model/ModelProfileTests.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/opensearch/ad/model/ModelProfileTests.java b/src/test/java/org/opensearch/ad/model/ModelProfileTests.java index 012b5f99e..996e81c17 100644 --- a/src/test/java/org/opensearch/ad/model/ModelProfileTests.java +++ b/src/test/java/org/opensearch/ad/model/ModelProfileTests.java @@ -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; @@ -20,24 +31,26 @@ 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; + } } From ba6ed4179c3d551f2306becb5c825a81bc1d9322 Mon Sep 17 00:00:00 2001 From: Varun Jain Date: Mon, 5 Dec 2022 21:22:58 -0800 Subject: [PATCH 3/3] Model Profile Test Signed-off-by: Varun Jain --- .../java/org/opensearch/ad/model/ModelProfileTests.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/opensearch/ad/model/ModelProfileTests.java b/src/test/java/org/opensearch/ad/model/ModelProfileTests.java index 996e81c17..d4d0fbf49 100644 --- a/src/test/java/org/opensearch/ad/model/ModelProfileTests.java +++ b/src/test/java/org/opensearch/ad/model/ModelProfileTests.java @@ -27,9 +27,9 @@ public class ModelProfileTests extends AbstractADTest { public void testToXContent() throws IOException { ModelProfile profile1 = new ModelProfile( - randomAlphaOfLength(5), - Entity.createSingleAttributeEntity(randomAlphaOfLength(5), randomAlphaOfLength(5)), - 0 + randomAlphaOfLength(5), + Entity.createSingleAttributeEntity(randomAlphaOfLength(5), randomAlphaOfLength(5)), + 0 ); XContentBuilder builder = getBuilder(profile1); String json = Strings.toString(builder); @@ -49,7 +49,7 @@ public void testToXContent() throws IOException { private XContentBuilder getBuilder(ModelProfile profile) throws IOException { XContentBuilder builder = jsonBuilder(); builder.startObject(); - profile.toXContent(builder,ToXContent.EMPTY_PARAMS); + profile.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); return builder; }