mirror of https://github.com/apache/cloudstack.git
Refactoring the GetHostStatsCommand
- Added basic tests - Added test for when the key (Command) does not exist in the Map
This commit is contained in:
parent
30e72e4a15
commit
0e51379da7
|
|
@ -2250,7 +2250,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
|
|||
}
|
||||
}
|
||||
|
||||
protected HostStatsEntry getHostStats(final Connection conn, final GetHostStatsCommand cmd, final String hostGuid, final long hostId) {
|
||||
public HostStatsEntry getHostStats(final Connection conn, final GetHostStatsCommand cmd, final String hostGuid, final long hostId) {
|
||||
|
||||
final HostStatsEntry hostStats = new HostStatsEntry(hostId, 0, 0, 0, "host", 0, 0, 0, 0);
|
||||
final Object[] rrdData = getRRDData(conn, 1); // call rrd method with 1 for host
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ import java.util.Hashtable;
|
|||
|
||||
import com.cloud.agent.api.Answer;
|
||||
import com.cloud.agent.api.Command;
|
||||
import com.cloud.agent.api.GetHostStatsCommand;
|
||||
import com.cloud.agent.api.ReadyCommand;
|
||||
import com.cloud.agent.api.RebootRouterCommand;
|
||||
import com.cloud.agent.api.proxy.CheckConsoleProxyLoadCommand;
|
||||
|
|
@ -55,6 +56,7 @@ public class CitrixRequestWrapper extends RequestWrapper {
|
|||
map.put(CheckConsoleProxyLoadCommand.class, new CitrixCheckConsoleProxyLoadCommandWrapper());
|
||||
map.put(WatchConsoleProxyLoadCommand.class, new CitrixWatchConsoleProxyLoadCommandWrapper());
|
||||
map.put(ReadyCommand.class, new CitrixReadyCommandWrapper());
|
||||
map.put(GetHostStatsCommand.class, new GetHostStatsCommandWrapper());
|
||||
}
|
||||
|
||||
public static CitrixRequestWrapper getInstance() {
|
||||
|
|
@ -66,6 +68,10 @@ public class CitrixRequestWrapper extends RequestWrapper {
|
|||
@SuppressWarnings("unchecked")
|
||||
final CommandWrapper<Command, Answer, ServerResource> commandWrapper = map.get(command.getClass());
|
||||
|
||||
if (commandWrapper == null) {
|
||||
throw new NullPointerException("No key found for '" + command.getClass() + "' in the Map!");
|
||||
}
|
||||
|
||||
return commandWrapper.execute(command, serverResource);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
//
|
||||
// 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 com.cloud.hypervisor.xenserver.resource.wrapper;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.agent.api.Answer;
|
||||
import com.cloud.agent.api.GetHostStatsAnswer;
|
||||
import com.cloud.agent.api.GetHostStatsCommand;
|
||||
import com.cloud.agent.api.HostStatsEntry;
|
||||
import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase;
|
||||
import com.cloud.resource.CommandWrapper;
|
||||
import com.xensource.xenapi.Connection;
|
||||
|
||||
public final class GetHostStatsCommandWrapper extends CommandWrapper<GetHostStatsCommand, Answer, CitrixResourceBase> {
|
||||
|
||||
private static final Logger s_logger = Logger.getLogger(GetHostStatsCommandWrapper.class);
|
||||
|
||||
@Override
|
||||
public Answer execute(final GetHostStatsCommand command, final CitrixResourceBase citrixResourceBase) {
|
||||
final Connection conn = citrixResourceBase.getConnection();
|
||||
try {
|
||||
final HostStatsEntry hostStats = citrixResourceBase.getHostStats(conn, command, command.getHostGuid(), command.getHostId());
|
||||
return new GetHostStatsAnswer(command, hostStats);
|
||||
} catch (final Exception e) {
|
||||
final String msg = "Unable to get Host stats" + e.toString();
|
||||
s_logger.warn(msg, e);
|
||||
return new GetHostStatsAnswer(command, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ package com.cloud.hypervisor.xenserver.resource.wrapper;
|
|||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
|
@ -14,6 +15,8 @@ import org.mockito.Mock;
|
|||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import com.cloud.agent.api.Answer;
|
||||
import com.cloud.agent.api.Command;
|
||||
import com.cloud.agent.api.GetHostStatsCommand;
|
||||
import com.cloud.agent.api.ReadyCommand;
|
||||
import com.cloud.agent.api.RebootAnswer;
|
||||
import com.cloud.agent.api.RebootCommand;
|
||||
|
|
@ -41,6 +44,18 @@ public class CitrixRequestWrapperTest {
|
|||
assertNotNull(wrapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnknownCommand() {
|
||||
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
|
||||
assertNotNull(wrapper);
|
||||
|
||||
try {
|
||||
wrapper.execute(new NotAValidCommand(), citrixResourceBase);
|
||||
} catch (final Exception e) {
|
||||
assertTrue(e instanceof NullPointerException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteRebootRouterCommand() {
|
||||
final RebootRouterCommand rebootCommand = new RebootRouterCommand("", "");
|
||||
|
|
@ -109,4 +124,25 @@ public class CitrixRequestWrapperTest {
|
|||
|
||||
assertFalse(answer.getResult());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetHostStatsCommandCommand() {
|
||||
final GetHostStatsCommand statsCommand = new GetHostStatsCommand(null, null, 0);
|
||||
|
||||
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
|
||||
assertNotNull(wrapper);
|
||||
|
||||
final Answer answer = wrapper.execute(statsCommand, citrixResourceBase);
|
||||
|
||||
assertTrue(answer.getResult());
|
||||
}
|
||||
}
|
||||
|
||||
class NotAValidCommand extends Command {
|
||||
|
||||
@Override
|
||||
public boolean executeInSequence() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue