// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.model.content.cluster; import com.yahoo.vespa.model.builder.xml.dom.ModelElement; import com.yahoo.vespa.model.content.TuningDispatch; import org.apache.commons.io.input.CharSequenceInputStream; import org.junit.Test; import javax.xml.parsers.DocumentBuilderFactory; import java.nio.charset.StandardCharsets; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; /** * @author Simon Thoresen Hult */ public class DomTuningDispatchBuilderTest { @Test public void requireThatDefaultsAreNull() throws Exception { TuningDispatch dispatch = newTuningDispatch( ""); assertNull(dispatch.getMaxHitsPerPartition()); } @Test public void requireThatEmptyTuningIsSafe() throws Exception { TuningDispatch dispatch = newTuningDispatch( "" + " " + ""); assertNull(dispatch.getMaxHitsPerPartition()); } @Test public void requireThatEmptydispatchIsSafe() throws Exception { TuningDispatch dispatch = newTuningDispatch( "" + " " + " " + " " + ""); assertNull(dispatch.getMaxHitsPerPartition()); assertNull(dispatch.getMinGroupCoverage()); assertNull(dispatch.getMinActiveDocsCoverage()); assertTrue(TuningDispatch.DispatchPolicy.ROUNDROBIN == dispatch.getDispatchPolicy()); } @Test public void requireThatTuningDispatchCanBeBuilt() throws Exception { TuningDispatch dispatch = newTuningDispatch( "" + " " + " " + " 69" + " 7.5" + " 12.5" + " " + " " + ""); assertEquals(69, dispatch.getMaxHitsPerPartition().intValue()); assertEquals(7.5, dispatch.getMinGroupCoverage().doubleValue(), 0.0); assertEquals(12.5, dispatch.getMinActiveDocsCoverage().doubleValue(), 0.0); } @Test public void requireThatTuningDispatchPolicyRoundRobin() throws Exception { TuningDispatch dispatch = newTuningDispatch( "" + " " + " " + " round-robin" + " " + " " + ""); assertTrue(TuningDispatch.DispatchPolicy.ROUNDROBIN == dispatch.getDispatchPolicy()); } @Test public void requireThatTuningDispatchPolicyRandom() throws Exception { TuningDispatch dispatch = newTuningDispatch( "" + " " + " " + " random" + " " + " " + ""); assertTrue(TuningDispatch.DispatchPolicy.RANDOM == dispatch.getDispatchPolicy()); } private static TuningDispatch newTuningDispatch(String xml) throws Exception { return DomTuningDispatchBuilder.build( new ModelElement(DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(new CharSequenceInputStream(xml, StandardCharsets.UTF_8)) .getDocumentElement())); } }