package be.iminds.ilabt.jfed.lowlevel.api;

import be.iminds.ilabt.jfed.lowlevel.GeniAMResponseCode;
import org.testng.annotations.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class AbstractGeniAggregateManagerTest {
    @Test
    public void testIsNotEnoughFreeResources() throws Exception {
        AbstractGeniAggregateManager.AggregateManagerReply reply = mock(AbstractGeniAggregateManager.AggregateManagerReply.class);
        when(reply.getValue()).thenReturn("Could not map to resources: Precheck failure");
        when(reply.getRawValue()).thenReturn("Could not map to resources: Precheck failure");
        when(reply.getOutput()).thenReturn("  *** No available physical nodes of type SERVER5P found (1 requested)\n");
        when(reply.getGeniResponseCode()).thenReturn(GeniAMResponseCode.GENIRESPONSE_ERROR);
        assertThat(AbstractGeniAggregateManager.isNotEnoughFreeResources(reply), is(true));
     }
    @Test
    public void testIsNotEnoughFreeResources2() throws Exception {
        //same as testIsNotEnoughFreeResources() but without value, should also work!
        AbstractGeniAggregateManager.AggregateManagerReply reply = mock(AbstractGeniAggregateManager.AggregateManagerReply.class);
        when(reply.getValue()).thenReturn(null);
        when(reply.getRawValue()).thenReturn("Could not map to resources: Precheck failure");
        when(reply.getOutput()).thenReturn("  *** No available physical nodes of type SERVER5P found (1 requested)\n");
        when(reply.getGeniResponseCode()).thenReturn(GeniAMResponseCode.GENIRESPONSE_ERROR);
        assertThat(AbstractGeniAggregateManager.isNotEnoughFreeResources(reply), is(true));
     }
    @Test
    public void testIsNotEnoughFreeResourcesPerformLte() throws Exception {
        AbstractGeniAggregateManager.AggregateManagerReply reply = mock(AbstractGeniAggregateManager.AggregateManagerReply.class);
        when(reply.getValue()).thenReturn(null);
        when(reply.getRawValue()).thenReturn("");
        when(reply.getOutput()).thenReturn("Too Big: insufficient resources to fulfill request");
        when(reply.getGeniResponseCode()).thenReturn(GeniAMResponseCode.GENIRESPONSE_TOOBIG);
        assertThat(AbstractGeniAggregateManager.isNotEnoughFreeResources(reply), is(true));
     }
    @Test
    public void testIsNotEnoughFreeResourcesExogeni() throws Exception {
        AbstractGeniAggregateManager.AggregateManagerReply reply = mock(AbstractGeniAggregateManager.AggregateManagerReply.class);
        when(reply.getValue()).thenReturn(null);
        when(reply.getRawValue()).thenReturn(null);
        when(reply.getOutput()).thenReturn("Embedding workflow ERROR: 2:Not enough resources in domain: http://geni-orca.renci.org/owl/uvanlvmsite.rdf#uvanlvmsite/Domain/vm:vm; Requested:1;resource pool=0.\\n Please see https://geni-orca.renci.org/trac/wiki/orca-errors for possible solutions.");
        when(reply.getGeniResponseCode()).thenReturn(GeniAMResponseCode.getByCode(2));
        assertThat(AbstractGeniAggregateManager.isNotEnoughFreeResources(reply), is(true));
     }
    @Test
    public void testIsNotEnoughFreeResourcesLogATec() throws Exception {
        AbstractGeniAggregateManager.AggregateManagerReply reply = mock(AbstractGeniAggregateManager.AggregateManagerReply.class);
        when(reply.getValue()).thenReturn(null);
        when(reply.getRawValue()).thenReturn(null);
        when(reply.getOutput()).thenReturn("insufficient resources available");
        when(reply.getGeniResponseCode()).thenReturn(GeniAMResponseCode.GENIRESPONSE_REFUSED);
        assertThat(AbstractGeniAggregateManager.isNotEnoughFreeResources(reply), is(true));
     }

    //TODO: add some more examples of isNotEnoughFreeResources()
}