Example usage for io.netty.buffer Unpooled wrappedBuffer

List of usage examples for io.netty.buffer Unpooled wrappedBuffer

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled wrappedBuffer.

Prototype

public static ByteBuf wrappedBuffer(ByteBuffer... buffers) 

Source Link

Document

Creates a new big-endian composite buffer which wraps the slices of the specified NIO buffers without copying them.

Usage

From source file:com.rs3e.utility.ByteBufUtils.java

License:Open Source License

public static ByteBuf rsa(ByteBuf buf, String modulus, String exponent) {
    byte[] bytes = new byte[buf.readableBytes()];
    buf.readBytes(bytes);/*from ww  w . j  a  v a 2s  .  c  o m*/

    BigInteger cipherText = new BigInteger(bytes);
    BigInteger mod = new BigInteger(modulus);
    BigInteger exp = new BigInteger(exponent);
    BigInteger plainText = cipherText.modPow(exp, mod);

    return Unpooled.wrappedBuffer(plainText.toByteArray());
}

From source file:com.schibsted.triathlon.operators.ClusterOperatorTest.java

License:Apache License

@Test
public void testDeployExistingDataCenter() throws Exception {
    List<String> constraint = new ArrayList<String>() {
        {//  w  ww .  j  a v  a  2 s. c om
            add("datacenter");
            add("CLUSTER");
            add("pluto-dc");
        }
    };
    TriathlonService triathlonService = Mockito.mock(TriathlonService.class);
    Marathon appDefinition = Mockito.mock(Marathon.class);
    ConstraintModel constraintModel = ConstraintModel.createConstraintModel(constraint);

    String appDefinitionJson = "";
    when(triathlonService.serializeMarathon(Mockito.anyObject())).thenReturn(appDefinitionJson);

    Operator cluster = new ClusterOperator(triathlonService, constraintModel);

    Operator spyCluster = Mockito.spy(cluster);
    doReturn(Observable.just(Unpooled.wrappedBuffer("TEST".getBytes()))).when(spyCluster)
            .getMarathonCommand(Mockito.any(InstanceInfo.class), Mockito.anyString());

    assertEquals("TEST",
            spyCluster.apply(appDefinition).toBlocking().toFuture().get().toString(Charset.defaultCharset()));
    verify(spyCluster, times(1)).getMarathonCommand(eq(ii), Mockito.anyString());
}

From source file:com.schibsted.triathlon.operators.ClusterOperatorTest.java

License:Apache License

@Test(expected = NoSuchElementException.class)
public void testDeployNonExistingDataCenter() throws Exception {
    List<String> constraint = new ArrayList<String>() {
        {/* w  w w  .java2  s  .  co m*/
            add("datacenter");
            add("CLUSTER");
            add("mars-dc");
        }
    };
    TriathlonService triathlonService = Mockito.mock(TriathlonService.class);
    Marathon appDefinition = Mockito.mock(Marathon.class);
    ConstraintModel constraintModel = ConstraintModel.createConstraintModel(constraint);

    String appDefinitionJson = "";
    when(triathlonService.serializeMarathon(Mockito.anyObject())).thenReturn(appDefinitionJson);

    Operator cluster = new ClusterOperator(triathlonService, constraintModel);

    Operator spyCluster = Mockito.spy(cluster);
    doReturn(Observable.just(Unpooled.wrappedBuffer("TEST".getBytes()))).when(spyCluster)
            .getMarathonCommand(Mockito.any(InstanceInfo.class), Mockito.anyString());

    spyCluster.apply(appDefinition);
    verify(spyCluster, times(0)).getMarathonCommand(eq(ii), Mockito.anyString());
}

From source file:com.schibsted.triathlon.operators.GroupByOperatorTest.java

License:Apache License

@Test
public void testDeployOneDataCenter() throws Exception {
    initializeInstanceInfos(1);/*from  w  w  w . j a v a  2 s . c o m*/

    List<String> constraint = new ArrayList<String>() {
        {
            add("datacenter");
            add("GROUP_BY");
        }
    };
    TriathlonService triathlonService = Mockito.mock(TriathlonService.class);
    Marathon appDefinition = Mockito.mock(Marathon.class);
    when(appDefinition.getInstances()).thenReturn(2);
    ConstraintModel constraintModel = ConstraintModel.createConstraintModel(constraint);

    String appDefinitionJson = "";
    when(triathlonService.serializeMarathon(Mockito.anyObject())).thenReturn(appDefinitionJson);

    Operator cluster = new GroupByOperator(triathlonService, constraintModel);

    Operator spyCluster = Mockito.spy(cluster);
    doReturn(Observable.just(Unpooled.wrappedBuffer("TEST".getBytes()))).when(spyCluster)
            .getMarathonCommand(Mockito.any(InstanceInfo.class), Mockito.anyString());

    spyCluster.apply(appDefinition).subscribe();
    verify(spyCluster, times(1)).getMarathonCommand(Mockito.any(InstanceInfo.class), Mockito.anyString());
}

From source file:com.schibsted.triathlon.operators.GroupByOperatorTest.java

License:Apache License

@Test
public void testDeployTwoDataCenters() throws Exception {
    initializeInstanceInfos(2);//from   www  . jav  a2  s  . c  om

    List<String> constraint = new ArrayList<String>() {
        {
            add("datacenter");
            add("GROUP_BY");
        }
    };
    TriathlonService triathlonService = Mockito.mock(TriathlonService.class);
    Marathon appDefinition = Mockito.mock(Marathon.class);
    when(appDefinition.getInstances()).thenReturn(2);
    ConstraintModel constraintModel = ConstraintModel.createConstraintModel(constraint);

    String appDefinitionJson = "";
    when(triathlonService.serializeMarathon(Mockito.anyObject())).thenReturn(appDefinitionJson);

    Operator cluster = new GroupByOperator(triathlonService, constraintModel);

    Operator spyCluster = Mockito.spy(cluster);
    doReturn(Observable.just(Unpooled.wrappedBuffer("TEST".getBytes()))).when(spyCluster)
            .getMarathonCommand(Mockito.any(InstanceInfo.class), Mockito.anyString());

    spyCluster.apply(appDefinition).subscribe();
    verify(spyCluster, times(2)).getMarathonCommand(Mockito.any(InstanceInfo.class), Mockito.anyString());
}

From source file:com.schibsted.triathlon.operators.GroupByOperatorTest.java

License:Apache License

@Test
public void testDeployTwoDataCentersOddNumberOfInstances() throws Exception {
    initializeInstanceInfos(2);/*  w w w.j ava2s.  c o  m*/

    List<String> constraint = new ArrayList<String>() {
        {
            add("datacenter");
            add("GROUP_BY");
        }
    };
    TriathlonService triathlonService = new TriathlonServiceImpl();

    String content = IOUtils.toString(this.getClass().getResourceAsStream("group_by_operator_1.json"), "UTF-8");

    ByteBuf buffer = Unpooled.copiedBuffer(content.getBytes());

    Marathon appDefinition = triathlonService.parseJson(Observable.just(buffer)).toBlocking().toFuture().get();
    appDefinition.setInstances(5);

    ConstraintModel constraintModel = ConstraintModel.createConstraintModel(constraint);

    Operator cluster = new GroupByOperator(triathlonService, constraintModel);

    Operator spyCluster = Mockito.spy(cluster);
    doReturn(Observable.just(Unpooled.wrappedBuffer("TEST".getBytes()))).when(spyCluster)
            .getMarathonCommand(Mockito.any(InstanceInfo.class), Mockito.anyString());

    Stack<Integer> values = new Stack<>();
    values.push(2);
    values.push(3);

    when(spyCluster.getMarathonCommand(Mockito.any(InstanceInfo.class), Mockito.anyString()))
            .thenAnswer(invocation -> {
                Object[] args = invocation.getArguments();
                Observable<ByteBuf> val = Observable
                        .just(Unpooled.wrappedBuffer(((String) args[1]).getBytes()));
                Marathon appDef = triathlonService.parseJson(val).toBlocking().toFuture().get();
                Integer v = values.pop();
                assertEquals(v, appDef.getInstances());
                return val;
            });

    spyCluster.apply(appDefinition).subscribe();
    verify(spyCluster, times(2)).getMarathonCommand(Mockito.any(InstanceInfo.class), Mockito.anyString());
}

From source file:com.schibsted.triathlon.operators.GroupByOperatorTest.java

License:Apache License

@Test
public void testDeployLessInstancesThanDataCenters() throws Exception {
    initializeInstanceInfos(4);//from   w  w  w.  ja v a  2  s  . co  m

    List<String> constraint = new ArrayList<String>() {
        {
            add("datacenter");
            add("GROUP_BY");
        }
    };
    TriathlonService triathlonService = Mockito.mock(TriathlonService.class);
    Marathon appDefinition = Mockito.mock(Marathon.class);
    when(appDefinition.getInstances()).thenReturn(3);
    ConstraintModel constraintModel = ConstraintModel.createConstraintModel(constraint);

    String appDefinitionJson = "";
    when(triathlonService.serializeMarathon(Mockito.anyObject())).thenReturn(appDefinitionJson);

    Operator cluster = new GroupByOperator(triathlonService, constraintModel);

    Operator spyCluster = Mockito.spy(cluster);
    doReturn(Observable.just(Unpooled.wrappedBuffer("TEST".getBytes()))).when(spyCluster)
            .getMarathonCommand(Mockito.any(InstanceInfo.class), Mockito.anyString());

    spyCluster.apply(appDefinition).subscribe();
    verify(spyCluster, times(3)).getMarathonCommand(Mockito.any(InstanceInfo.class), Mockito.anyString());
}

From source file:com.schibsted.triathlon.operators.UniqueOperatorTest.java

License:Apache License

@Test
public void testDeployFourInstancestoFourDataCenters() throws Exception {
    initializeInstanceInfos(4);/*from w  w w .  j  a v a2  s  .  c  o m*/
    List<String> constraint = new ArrayList<String>() {
        {
            add("datacenter");
            add("UNIQUE");
        }
    };
    TriathlonService triathlonService = new TriathlonServiceImpl();

    String content = IOUtils.toString(this.getClass().getResourceAsStream("unique_operator_1.json"), "UTF-8");

    ByteBuf buffer = Unpooled.copiedBuffer(content.getBytes());

    Marathon appDefinition = triathlonService.parseJson(Observable.just(buffer)).toBlocking().toFuture().get();
    appDefinition.setInstances(5);

    ConstraintModel constraintModel = ConstraintModel.createConstraintModel(constraint);

    Operator cluster = new UniqueOperator(triathlonService, constraintModel);

    Operator spyCluster = Mockito.spy(cluster);
    doReturn(Observable.just(Unpooled.wrappedBuffer("TEST".getBytes()))).when(spyCluster)
            .getMarathonCommand(Mockito.any(InstanceInfo.class), Mockito.anyString());

    when(spyCluster.getMarathonCommand(Mockito.any(InstanceInfo.class), Mockito.anyString()))
            .thenAnswer(invocation -> {
                Object[] args = invocation.getArguments();
                Observable<ByteBuf> val = Observable
                        .just(Unpooled.wrappedBuffer(((String) args[1]).getBytes()));
                Marathon appDef = triathlonService.parseJson(val).toBlocking().toFuture().get();
                assertEquals(Integer.valueOf(1), appDef.getInstances());
                return val;
            });

    spyCluster.apply(appDefinition).subscribe();
    verify(spyCluster, times(4)).getMarathonCommand(Mockito.any(InstanceInfo.class), Mockito.anyString());
}

From source file:com.schibsted.triathlon.operators.UniqueOperatorTest.java

License:Apache License

@Test
public void testDeployFourInstancestoTwoDataCenters() throws Exception {
    initializeInstanceInfos(2);//from  w ww.j av  a2 s  .c om

    List<String> constraint = new ArrayList<String>() {
        {
            add("datacenter");
            add("UNIQUE");
        }
    };
    TriathlonService triathlonService = new TriathlonServiceImpl();

    String content = IOUtils.toString(this.getClass().getResourceAsStream("unique_operator_1.json"), "UTF-8");

    ByteBuf buffer = Unpooled.copiedBuffer(content.getBytes());

    Marathon appDefinition = triathlonService.parseJson(Observable.just(buffer)).toBlocking().toFuture().get();
    appDefinition.setInstances(5);

    ConstraintModel constraintModel = ConstraintModel.createConstraintModel(constraint);

    Operator cluster = new UniqueOperator(triathlonService, constraintModel);

    Operator spyCluster = Mockito.spy(cluster);
    doReturn(Observable.just(Unpooled.wrappedBuffer("TEST".getBytes()))).when(spyCluster)
            .getMarathonCommand(Mockito.any(InstanceInfo.class), Mockito.anyString());

    when(spyCluster.getMarathonCommand(Mockito.any(InstanceInfo.class), Mockito.anyString()))
            .thenAnswer(invocation -> {
                Object[] args = invocation.getArguments();
                Observable<ByteBuf> val = Observable
                        .just(Unpooled.wrappedBuffer(((String) args[1]).getBytes()));
                Marathon appDef = triathlonService.parseJson(val).toBlocking().toFuture().get();
                assertEquals(Integer.valueOf(1), appDef.getInstances());
                return val;
            });

    spyCluster.apply(appDefinition).subscribe();
    verify(spyCluster, times(2)).getMarathonCommand(Mockito.any(InstanceInfo.class), Mockito.anyString());

}

From source file:com.schibsted.triathlon.service.TriathlonServiceImplTest.java

License:Apache License

@Test
public void testParseJson() throws Exception {
    TriathlonService service = new TriathlonServiceImpl();
    Observable<ByteBuf> json = Observable.just(Unpooled.wrappedBuffer(
            "{\"constraints\": [[\"hostname\", \"UNIQUE\"], [\"datacenter\", \"aws\"]]}".getBytes()));

    Observable<Marathon> marathonObject = service.parseJson(json);
    marathonObject.subscribe(val -> {
        assertEquals(2, val.getConstraints().size());
    });//from  w w w .java  2s.  c o  m
}