|
| 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.resource; |
| 18 | + |
| 19 | +import com.cloud.agent.api.Answer; |
| 20 | +import com.cloud.agent.api.Command; |
| 21 | +import org.apache.cloudstack.agent.api.AddOrUpdateNetrisStaticRouteCommand; |
| 22 | +import org.apache.cloudstack.agent.api.CreateNetrisACLCommand; |
| 23 | +import org.apache.cloudstack.agent.api.CreateNetrisVnetCommand; |
| 24 | +import org.apache.cloudstack.agent.api.CreateNetrisVpcCommand; |
| 25 | +import org.apache.cloudstack.agent.api.CreateOrUpdateNetrisLoadBalancerRuleCommand; |
| 26 | +import org.apache.cloudstack.agent.api.DeleteNetrisACLCommand; |
| 27 | +import org.apache.cloudstack.agent.api.DeleteNetrisLoadBalancerRuleCommand; |
| 28 | +import org.apache.cloudstack.agent.api.DeleteNetrisNatRuleCommand; |
| 29 | +import org.apache.cloudstack.agent.api.DeleteNetrisStaticRouteCommand; |
| 30 | +import org.apache.cloudstack.agent.api.DeleteNetrisVnetCommand; |
| 31 | +import org.apache.cloudstack.agent.api.DeleteNetrisVpcCommand; |
| 32 | +import org.apache.cloudstack.agent.api.ReleaseNatIpCommand; |
| 33 | +import org.apache.cloudstack.agent.api.SetupNetrisPublicRangeCommand; |
| 34 | +import org.apache.cloudstack.service.NetrisApiClient; |
| 35 | +import org.junit.After; |
| 36 | +import org.junit.Assert; |
| 37 | +import org.junit.Before; |
| 38 | +import org.junit.Test; |
| 39 | +import org.mockito.InjectMocks; |
| 40 | +import org.mockito.Mock; |
| 41 | +import org.mockito.Mockito; |
| 42 | +import org.mockito.MockitoAnnotations; |
| 43 | +import org.mockito.Spy; |
| 44 | + |
| 45 | +import java.util.Arrays; |
| 46 | +import java.util.List; |
| 47 | + |
| 48 | +public class NetrisResourceTest { |
| 49 | + |
| 50 | + @Mock |
| 51 | + private NetrisApiClient netrisApiClient; |
| 52 | + |
| 53 | + @Spy |
| 54 | + @InjectMocks |
| 55 | + private NetrisResource netrisResource = new NetrisResource(); |
| 56 | + |
| 57 | + private AutoCloseable closeable; |
| 58 | + |
| 59 | + @Mock |
| 60 | + private CreateNetrisVpcCommand createNetrisVpcCommand; |
| 61 | + @Mock |
| 62 | + private CreateNetrisVnetCommand createNetrisVnetCommand; |
| 63 | + @Mock |
| 64 | + private DeleteNetrisVnetCommand deleteNetrisVnetCommand; |
| 65 | + @Mock |
| 66 | + private DeleteNetrisVpcCommand deleteNetrisVpcCommand; |
| 67 | + @Mock |
| 68 | + private SetupNetrisPublicRangeCommand setupNetrisPublicRangeCommand; |
| 69 | + @Mock |
| 70 | + private DeleteNetrisNatRuleCommand deleteNetrisNatRuleCommand; |
| 71 | + @Mock |
| 72 | + private CreateNetrisACLCommand createNetrisACLCommand; |
| 73 | + @Mock |
| 74 | + private DeleteNetrisACLCommand deleteNetrisACLCommand; |
| 75 | + @Mock |
| 76 | + private AddOrUpdateNetrisStaticRouteCommand addOrUpdateNetrisStaticRouteCommand; |
| 77 | + @Mock |
| 78 | + private DeleteNetrisStaticRouteCommand deleteNetrisStaticRouteCommand; |
| 79 | + @Mock |
| 80 | + private ReleaseNatIpCommand releaseNatIpCommand; |
| 81 | + @Mock |
| 82 | + private CreateOrUpdateNetrisLoadBalancerRuleCommand createOrUpdateNetrisLoadBalancerRuleCommand; |
| 83 | + @Mock |
| 84 | + private DeleteNetrisLoadBalancerRuleCommand deleteNetrisLoadBalancerRuleCommand; |
| 85 | + |
| 86 | + @Before |
| 87 | + public void setup() { |
| 88 | + closeable = MockitoAnnotations.openMocks(this); |
| 89 | + } |
| 90 | + |
| 91 | + @After |
| 92 | + public void tearDown() throws Exception { |
| 93 | + closeable.close(); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testExecuteRequest() { |
| 98 | + List<Command> commands = Arrays.asList(createNetrisVpcCommand, createNetrisVnetCommand, deleteNetrisVnetCommand, |
| 99 | + deleteNetrisVpcCommand, setupNetrisPublicRangeCommand, deleteNetrisNatRuleCommand, createNetrisACLCommand, |
| 100 | + deleteNetrisACLCommand, addOrUpdateNetrisStaticRouteCommand, deleteNetrisStaticRouteCommand, |
| 101 | + releaseNatIpCommand, createOrUpdateNetrisLoadBalancerRuleCommand, deleteNetrisLoadBalancerRuleCommand); |
| 102 | + |
| 103 | + for (boolean res : new boolean[]{true, false}) { |
| 104 | + setMocksToValue(res); |
| 105 | + for (Command command : commands) { |
| 106 | + Answer answer = netrisResource.executeRequest(command); |
| 107 | + Assert.assertEquals(res, answer.getResult()); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + Mockito.verify(netrisApiClient, Mockito.times(2)).createVpc(createNetrisVpcCommand); |
| 112 | + Mockito.verify(netrisApiClient, Mockito.times(2)).createVnet(createNetrisVnetCommand); |
| 113 | + Mockito.verify(netrisApiClient, Mockito.times(2)).deleteVnet(deleteNetrisVnetCommand); |
| 114 | + Mockito.verify(netrisApiClient, Mockito.times(2)).deleteVpc(deleteNetrisVpcCommand); |
| 115 | + Mockito.verify(netrisApiClient, Mockito.times(2)).setupZoneLevelPublicRange(setupNetrisPublicRangeCommand); |
| 116 | + Mockito.verify(netrisApiClient, Mockito.times(2)).deleteNatRule(deleteNetrisNatRuleCommand); |
| 117 | + Mockito.verify(netrisApiClient, Mockito.times(2)).addAclRule(createNetrisACLCommand); |
| 118 | + Mockito.verify(netrisApiClient, Mockito.times(2)).deleteAclRule(deleteNetrisACLCommand); |
| 119 | + Mockito.verify(netrisApiClient, Mockito.times(2)).addOrUpdateStaticRoute(addOrUpdateNetrisStaticRouteCommand); |
| 120 | + Mockito.verify(netrisApiClient, Mockito.times(2)).deleteStaticRoute(deleteNetrisStaticRouteCommand); |
| 121 | + Mockito.verify(netrisApiClient, Mockito.times(2)).releaseNatIp(releaseNatIpCommand); |
| 122 | + Mockito.verify(netrisApiClient, Mockito.times(2)).createOrUpdateLbRule(createOrUpdateNetrisLoadBalancerRuleCommand); |
| 123 | + Mockito.verify(netrisApiClient, Mockito.times(2)).deleteLbRule(deleteNetrisLoadBalancerRuleCommand); |
| 124 | + } |
| 125 | + |
| 126 | + private void setMocksToValue(boolean value) { |
| 127 | + Mockito.when(netrisApiClient.createVpc(createNetrisVpcCommand)).thenReturn(value); |
| 128 | + Mockito.when(netrisApiClient.createVnet(createNetrisVnetCommand)).thenReturn(value); |
| 129 | + Mockito.when(netrisApiClient.deleteVnet(deleteNetrisVnetCommand)).thenReturn(value); |
| 130 | + Mockito.when(netrisApiClient.deleteVpc(deleteNetrisVpcCommand)).thenReturn(value); |
| 131 | + Mockito.when(netrisApiClient.setupZoneLevelPublicRange(setupNetrisPublicRangeCommand)).thenReturn(value); |
| 132 | + Mockito.when(netrisApiClient.deleteNatRule(deleteNetrisNatRuleCommand)).thenReturn(value); |
| 133 | + Mockito.when(netrisApiClient.addAclRule(createNetrisACLCommand)).thenReturn(value); |
| 134 | + Mockito.when(netrisApiClient.deleteAclRule(deleteNetrisACLCommand)).thenReturn(value); |
| 135 | + Mockito.when(netrisApiClient.addOrUpdateStaticRoute(addOrUpdateNetrisStaticRouteCommand)).thenReturn(value); |
| 136 | + Mockito.when(netrisApiClient.deleteStaticRoute(deleteNetrisStaticRouteCommand)).thenReturn(value); |
| 137 | + Mockito.when(netrisApiClient.releaseNatIp(releaseNatIpCommand)).thenReturn(value); |
| 138 | + Mockito.when(netrisApiClient.createOrUpdateLbRule(createOrUpdateNetrisLoadBalancerRuleCommand)).thenReturn(value); |
| 139 | + Mockito.when(netrisApiClient.deleteLbRule(deleteNetrisLoadBalancerRuleCommand)).thenReturn(value); |
| 140 | + } |
| 141 | +} |
0 commit comments