From 16b7b71e36e3287a72ffa56dbdbcb1ca16c1e9df Mon Sep 17 00:00:00 2001 From: Fabricio Duarte Date: Mon, 24 Mar 2025 08:07:06 -0300 Subject: [PATCH] Fix secondary storage selectors feature (#10546) --- .../GenericPresetVariable.java | 6 ++- .../presetvariables/Resource.java | 6 ++- .../heuristics/presetvariables/Domain.java | 2 +- .../GenericHeuristicPresetVariable.java | 10 ++-- .../presetvariables/AccountTest.java | 46 +++++++++++++++++++ .../presetvariables/DomainTest.java | 41 +++++++++++++++++ .../GenericHeuristicPresetVariableTest.java | 40 ++++++++++++++++ .../presetvariables/SecondaryStorageTest.java | 45 ++++++++++++++++++ .../presetvariables/SnapshotTest.java | 44 ++++++++++++++++++ .../presetvariables/TemplateTest.java | 46 +++++++++++++++++++ .../presetvariables/VolumeTest.java | 44 ++++++++++++++++++ 11 files changed, 323 insertions(+), 7 deletions(-) create mode 100644 server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/AccountTest.java create mode 100644 server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/DomainTest.java create mode 100644 server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/GenericHeuristicPresetVariableTest.java create mode 100644 server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/SecondaryStorageTest.java create mode 100644 server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/SnapshotTest.java create mode 100644 server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/TemplateTest.java create mode 100644 server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/VolumeTest.java diff --git a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/GenericPresetVariable.java b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/GenericPresetVariable.java index f59f23abdc1..7073d2760d7 100644 --- a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/GenericPresetVariable.java +++ b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/GenericPresetVariable.java @@ -49,8 +49,12 @@ public class GenericPresetVariable { fieldNamesToIncludeInToString.add("name"); } + /*** + * Converts the preset variable into a valid JSON object that will be injected into the JS interpreter. + * This method should not be overridden or changed. + */ @Override - public String toString() { + public final String toString() { return ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, fieldNamesToIncludeInToString.toArray(new String[0])); } } diff --git a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Resource.java b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Resource.java index b1dfbacbfcd..4f8a971b869 100644 --- a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Resource.java +++ b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Resource.java @@ -40,8 +40,12 @@ public class Resource { this.domainId = domainId; } + /*** + * Converts the preset variable into a valid JSON object that will be injected into the JS interpreter. + * This method should not be overridden or changed. + */ @Override - public String toString() { + public final String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); } diff --git a/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Domain.java b/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Domain.java index 6565c06bfbb..704cbf4373e 100644 --- a/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Domain.java +++ b/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Domain.java @@ -16,7 +16,7 @@ // under the License. package org.apache.cloudstack.storage.heuristics.presetvariables; -public class Domain extends GenericHeuristicPresetVariable{ +public class Domain extends GenericHeuristicPresetVariable { private String id; public String getId() { diff --git a/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/GenericHeuristicPresetVariable.java b/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/GenericHeuristicPresetVariable.java index 28d4327954e..b85b7763eee 100644 --- a/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/GenericHeuristicPresetVariable.java +++ b/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/GenericHeuristicPresetVariable.java @@ -36,10 +36,12 @@ public class GenericHeuristicPresetVariable { fieldNamesToIncludeInToString.add("name"); } + /*** + * Converts the preset variable into a valid JSON object that will be injected into the JS interpreter. + * This method should not be overridden or changed. + */ @Override - public String toString() { - return String.format("GenericHeuristicPresetVariable %s", - ReflectionToStringBuilderUtils.reflectOnlySelectedFields( - this, fieldNamesToIncludeInToString.toArray(new String[0]))); + public final String toString() { + return ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, fieldNamesToIncludeInToString.toArray(new String[0])); } } diff --git a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/AccountTest.java b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/AccountTest.java new file mode 100644 index 00000000000..f7610438b78 --- /dev/null +++ b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/AccountTest.java @@ -0,0 +1,46 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.cloudstack.storage.heuristics.presetvariables; + +import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class AccountTest { + + @Test + public void toStringTestReturnsValidJson() { + Account variable = new Account(); + variable.setName("test name"); + variable.setId("test id"); + + Domain domainVariable = new Domain(); + domainVariable.setId("domain id"); + domainVariable.setName("domain name"); + variable.setDomain(domainVariable); + + String expected = ReflectionToStringBuilderUtils.reflectOnlySelectedFields(variable, "name", "id", "domain"); + String result = variable.toString(); + + Assert.assertEquals(expected, result); + } + +} diff --git a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/DomainTest.java b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/DomainTest.java new file mode 100644 index 00000000000..a1ec6854ecc --- /dev/null +++ b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/DomainTest.java @@ -0,0 +1,41 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.cloudstack.storage.heuristics.presetvariables; + +import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class DomainTest { + + @Test + public void toStringTestReturnsValidJson() { + Domain variable = new Domain(); + variable.setName("test name"); + variable.setId("test id"); + + String expected = ReflectionToStringBuilderUtils.reflectOnlySelectedFields(variable, "name", "id"); + String result = variable.toString(); + + Assert.assertEquals(expected, result); + } + +} diff --git a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/GenericHeuristicPresetVariableTest.java b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/GenericHeuristicPresetVariableTest.java new file mode 100644 index 00000000000..cd295e92caf --- /dev/null +++ b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/GenericHeuristicPresetVariableTest.java @@ -0,0 +1,40 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.cloudstack.storage.heuristics.presetvariables; + +import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class GenericHeuristicPresetVariableTest { + + @Test + public void toStringTestReturnsValidJson() { + GenericHeuristicPresetVariable variable = new GenericHeuristicPresetVariable(); + variable.setName("test name"); + + String expected = ReflectionToStringBuilderUtils.reflectOnlySelectedFields(variable, "name"); + String result = variable.toString(); + + Assert.assertEquals(expected, result); + } + +} diff --git a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/SecondaryStorageTest.java b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/SecondaryStorageTest.java new file mode 100644 index 00000000000..a09386789cf --- /dev/null +++ b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/SecondaryStorageTest.java @@ -0,0 +1,45 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.cloudstack.storage.heuristics.presetvariables; + +import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class SecondaryStorageTest { + + @Test + public void toStringTestReturnsValidJson() { + SecondaryStorage variable = new SecondaryStorage(); + variable.setName("test name"); + variable.setId("test id"); + variable.setProtocol("test protocol"); + variable.setUsedDiskSize(1L); + variable.setTotalDiskSize(2L); + + String expected = ReflectionToStringBuilderUtils.reflectOnlySelectedFields(variable, "name", "id", + "protocol", "usedDiskSize", "totalDiskSize"); + String result = variable.toString(); + + Assert.assertEquals(expected, result); + } + +} diff --git a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/SnapshotTest.java b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/SnapshotTest.java new file mode 100644 index 00000000000..b8476cd8e46 --- /dev/null +++ b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/SnapshotTest.java @@ -0,0 +1,44 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.cloudstack.storage.heuristics.presetvariables; + +import com.cloud.hypervisor.Hypervisor; +import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class SnapshotTest { + + @Test + public void toStringTestReturnsValidJson() { + Snapshot variable = new Snapshot(); + variable.setName("test name"); + variable.setSize(1L); + variable.setHypervisorType(Hypervisor.HypervisorType.KVM); + + String expected = ReflectionToStringBuilderUtils.reflectOnlySelectedFields(variable, "name", "size", + "hypervisorType"); + String result = variable.toString(); + + Assert.assertEquals(expected, result); + } + +} diff --git a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/TemplateTest.java b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/TemplateTest.java new file mode 100644 index 00000000000..2c1582befb2 --- /dev/null +++ b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/TemplateTest.java @@ -0,0 +1,46 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.cloudstack.storage.heuristics.presetvariables; + +import com.cloud.hypervisor.Hypervisor; +import com.cloud.storage.Storage; +import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class TemplateTest { + + @Test + public void toStringTestReturnsValidJson() { + Template variable = new Template(); + variable.setName("test name"); + variable.setTemplateType(Storage.TemplateType.USER); + variable.setHypervisorType(Hypervisor.HypervisorType.KVM); + variable.setFormat(Storage.ImageFormat.QCOW2); + + String expected = ReflectionToStringBuilderUtils.reflectOnlySelectedFields(variable, "name", "templateType", + "hypervisorType", "format"); + String result = variable.toString(); + + Assert.assertEquals(expected, result); + } + +} diff --git a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/VolumeTest.java b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/VolumeTest.java new file mode 100644 index 00000000000..e74ddc93ec5 --- /dev/null +++ b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/VolumeTest.java @@ -0,0 +1,44 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.cloudstack.storage.heuristics.presetvariables; + +import com.cloud.storage.Storage; +import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class VolumeTest { + + @Test + public void toStringTestReturnsValidJson() { + Volume variable = new Volume(); + variable.setName("test name"); + variable.setFormat(Storage.ImageFormat.QCOW2); + variable.setSize(1L); + + String expected = ReflectionToStringBuilderUtils.reflectOnlySelectedFields(variable, "name", "format", + "size"); + String result = variable.toString(); + + Assert.assertEquals(expected, result); + } + +}