|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package org.apache.cloudstack.api.command.offering; |
| 18 | + |
| 19 | +import static org.mockito.Mockito.mock; |
| 20 | +import static org.mockito.Mockito.when; |
| 21 | + |
| 22 | +import java.lang.reflect.Field; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.List; |
| 26 | +import java.util.function.LongFunction; |
| 27 | + |
| 28 | +import com.cloud.dc.DataCenter; |
| 29 | +import com.cloud.domain.Domain; |
| 30 | +import com.cloud.exception.ConcurrentOperationException; |
| 31 | +import com.cloud.exception.InsufficientCapacityException; |
| 32 | +import com.cloud.exception.InvalidParameterValueException; |
| 33 | +import com.cloud.exception.NetworkRuleConflictException; |
| 34 | +import com.cloud.exception.ResourceAllocationException; |
| 35 | +import com.cloud.exception.ResourceUnavailableException; |
| 36 | +import com.cloud.utils.db.EntityManager; |
| 37 | +import org.apache.cloudstack.api.BaseCmd; |
| 38 | +import org.apache.cloudstack.api.ServerApiException; |
| 39 | +import org.junit.Assert; |
| 40 | +import org.junit.Test; |
| 41 | + |
| 42 | +public class DomainAndZoneIdResolverTest { |
| 43 | + static class TestCmd extends BaseCmd implements DomainAndZoneIdResolver { |
| 44 | + @Override |
| 45 | + public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException { |
| 46 | + // No implementation needed for tests |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public String getCommandName() { |
| 51 | + return "test"; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public long getEntityOwnerId() { |
| 56 | + return 1L; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private void setEntityMgr(final BaseCmd cmd, final EntityManager entityMgr) throws Exception { |
| 61 | + Field f = BaseCmd.class.getDeclaredField("_entityMgr"); |
| 62 | + f.setAccessible(true); |
| 63 | + f.set(cmd, entityMgr); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void resolveDomainIds_usesDefaultProviderWhenEmpty() { |
| 68 | + TestCmd cmd = new TestCmd(); |
| 69 | + |
| 70 | + final LongFunction<List<Long>> defaultsProvider = id -> Arrays.asList(100L, 200L); |
| 71 | + |
| 72 | + List<Long> result = cmd.resolveDomainIds("", 42L, defaultsProvider, "offering"); |
| 73 | + Assert.assertEquals(Arrays.asList(100L, 200L), result); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void resolveDomainIds_resolvesValidUuids() throws Exception { |
| 78 | + TestCmd cmd = new TestCmd(); |
| 79 | + |
| 80 | + EntityManager em = mock(EntityManager.class); |
| 81 | + setEntityMgr(cmd, em); |
| 82 | + |
| 83 | + Domain d1 = mock(Domain.class); |
| 84 | + when(d1.getId()).thenReturn(10L); |
| 85 | + Domain d2 = mock(Domain.class); |
| 86 | + when(d2.getId()).thenReturn(20L); |
| 87 | + |
| 88 | + when(em.findByUuid(Domain.class, "uuid1")).thenReturn(d1); |
| 89 | + when(em.findByUuid(Domain.class, "uuid2")).thenReturn(d2); |
| 90 | + |
| 91 | + List<Long> ids = cmd.resolveDomainIds("uuid1, public, uuid2", null, null, "template"); |
| 92 | + Assert.assertEquals(Arrays.asList(10L, 20L), ids); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void resolveDomainIds_invalidUuid_throws() throws Exception { |
| 97 | + TestCmd cmd = new TestCmd(); |
| 98 | + |
| 99 | + EntityManager em = mock(EntityManager.class); |
| 100 | + setEntityMgr(cmd, em); |
| 101 | + |
| 102 | + when(em.findByUuid(Domain.class, "bad-uuid")).thenReturn(null); |
| 103 | + |
| 104 | + Assert.assertThrows(InvalidParameterValueException.class, |
| 105 | + () -> cmd.resolveDomainIds("bad-uuid", null, null, "offering")); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void resolveZoneIds_usesDefaultProviderWhenEmpty() { |
| 110 | + TestCmd cmd = new TestCmd(); |
| 111 | + |
| 112 | + final LongFunction<List<Long>> defaultsProvider = id -> Collections.singletonList(300L); |
| 113 | + |
| 114 | + List<Long> result = cmd.resolveZoneIds("", 99L, defaultsProvider, "offering"); |
| 115 | + Assert.assertEquals(Collections.singletonList(300L), result); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void resolveZoneIds_resolvesValidUuids() throws Exception { |
| 120 | + TestCmd cmd = new TestCmd(); |
| 121 | + |
| 122 | + EntityManager em = mock(EntityManager.class); |
| 123 | + setEntityMgr(cmd, em); |
| 124 | + |
| 125 | + DataCenter z1 = mock(DataCenter.class); |
| 126 | + when(z1.getId()).thenReturn(30L); |
| 127 | + DataCenter z2 = mock(DataCenter.class); |
| 128 | + when(z2.getId()).thenReturn(40L); |
| 129 | + |
| 130 | + when(em.findByUuid(DataCenter.class, "zone-1")).thenReturn(z1); |
| 131 | + when(em.findByUuid(DataCenter.class, "zone-2")).thenReturn(z2); |
| 132 | + |
| 133 | + List<Long> ids = cmd.resolveZoneIds("zone-1, all, zone-2", null, null, "service"); |
| 134 | + Assert.assertEquals(Arrays.asList(30L, 40L), ids); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void resolveZoneIds_invalidUuid_throws() throws Exception { |
| 139 | + TestCmd cmd = new TestCmd(); |
| 140 | + |
| 141 | + EntityManager em = mock(EntityManager.class); |
| 142 | + setEntityMgr(cmd, em); |
| 143 | + |
| 144 | + when(em.findByUuid(DataCenter.class, "bad-zone")).thenReturn(null); |
| 145 | + |
| 146 | + Assert.assertThrows(InvalidParameterValueException.class, |
| 147 | + () -> cmd.resolveZoneIds("bad-zone", null, null, "offering")); |
| 148 | + } |
| 149 | +} |
| 150 | + |
0 commit comments