All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bdware.doip.test.DoipClientTest Maven / Gradle / Ivy

The newest version!
package org.bdware.doip.test;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.audit.EndpointConfig;
import org.bdware.doip.encrypt.SM2Signer;
import org.bdware.doip.audit.client.AuditDoipClient;
import org.bdware.doip.audit.client.AuditIrpClient;
import org.bdware.doip.audit.config.FileStorage;
import org.bdware.doip.audit.writer.AuditConfig;
import org.bdware.doip.audit.writer.AuditType;
import org.bdware.doip.auditrepo.AutoAuditDO;
import org.bdware.doip.codec.digitalObject.DigitalObject;
import org.bdware.doip.codec.digitalObject.DoType;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.codec.doipMessage.DoipMessageFactory;
import org.bdware.doip.codec.exception.DoDecodeException;
import org.bdware.doip.codec.operations.BasicOperations;
import org.bdware.doip.endpoint.client.ClientConfig;
import org.bdware.irp.exception.IrpClientException;
import org.bdware.irp.stateinfo.StateInfoBase;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.zz.gmhelper.SM2KeyPair;
import org.zz.gmhelper.SM2Util;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

public class DoipClientTest {
    AuditDoipClient client;
    AuditIrpClient irpClient;
    static Logger LOGGER = LogManager.getLogger(DoipClientTest.class);
    FileStorage fileStorage;

    public static void main(String[] args) throws Exception {
        DoipClientTest test = new DoipClientTest();
        test.init();
        for (int i = 0; i < test.fileStorage.load().get("times").getAsInt(); i++) {
            Thread.sleep((long) (Math.random() * test.fileStorage.load().get("sleepMax").getAsInt()));
            int j = (int) (Math.random() * 100) % 4;
            LOGGER.info("PASS:" + i + "->" + j);
            switch (j) {
                case 0:
                    test.resolve();
                    break;
                case 1:
                    test.retrieve();
                    break;
                case 2:
                    test.update();
                    break;
                case 3:
                    test.reRegister();
            }
        }
        try {
            test.update();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.exit(0);
    }

    public void init() {
        fileStorage = new FileStorage("./input/auditrepo.json");
        EndpointConfig endconfig = fileStorage.loadAsEndpointConfig();
        endconfig.auditType = AuditType.OnlyHash;
        endconfig.privateKey = endconfig.publicKey = null;
        irpClient = new AuditIrpClient(endconfig);
        AuditConfig auditConfig = irpClient.getAuditConfig();
        String keyPair = "{\"publicKey\":\"%s\",\"privateKey\":\"%s\"}";
        SM2KeyPair sm2KeyPair = SM2KeyPair.fromJson(String.format(keyPair, endconfig.publicKey, endconfig.privateKey));
        client = new AuditDoipClient(auditConfig, "test_client", new SM2Signer(sm2KeyPair));
        ClientConfig config = null;
        String repoDoid = fileStorage.load().get("targetRepo").getAsString();
        try {
            StateInfoBase result = irpClient.resolve(repoDoid);
            config = new ClientConfig(result.getValues("address"));
        } catch (IrpClientException e) {
            e.printStackTrace();
        }
        client.connect(repoDoid, config);
    }

    public void resolve() {
        try {
            irpClient.resolve(pickDO());
        } catch (IrpClientException e) {
            e.printStackTrace();
        }
    }

    public void reRegister() {
        try {
            StateInfoBase infoBase = new StateInfoBase();
            infoBase.identifier = pickDO();
            irpClient.reRegister(infoBase);
        } catch (IrpClientException e) {
            e.printStackTrace();
        }
    }

    private String pickDO() {
        return fileStorage.load().get("doSample").getAsString();
    }

    public void update() {
        DigitalObject doDO = new DigitalObject(pickDO(), DoType.DO);
        client.updateSync(doDO);
        LOGGER.info("DONE!!!!");
        return;
    }

    public void retrieve() {
        AtomicInteger i = new AtomicInteger(0);
        client.retrieveSync(pickDO(), null, true);
    }


    public void retrieveWithSign() throws Exception {
        DoipMessage msg =
                new DoipMessageFactory.DoipMessageBuilder()
                        .createRequest("bdwaretest.loccall/repofile/large", BasicOperations.Retrieve.getName())
                        .create();
        msg.header.parameters.addAttribute("includeElementData", "true");
        SM2Signer signer = new SM2Signer(SM2Util.generateSM2KeyPair());
        signer.signMessage(msg);
        DoipMessage ret = client.sendMessageSync(msg);
        LOGGER.info(ret.header.parameters.id);
        LOGGER.info(new String(ret.body.getDataAsDigitalObject().elements.get(0).getData()));

    }

    public void go() {
        LOGGER.info(new String(ByteUtils.fromHexString("7b226964223a226c61726765222c226f7065726174696f6e223a22302e444f49502f4f702e5265747269657665222c2261747472696275746573223a7b22696e636c756465456c656d656e7444617461223a2274727565227d7d")));
    }


    public void retrieveSync() {
        DoipMessage msg = client.retrieveSync("small", null, true);
        LOGGER.info(msg.header.parameters.id);
        try {
            LOGGER.info(new String(msg.body.getDataAsDigitalObject().elements.get(0).getData()));
        } catch (DoDecodeException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void getAuditType() {
        DoipMessage msg = client.sendMessageSync(AutoAuditDO.getRepoAuditType(fileStorage.load().get("targetRepo").getAsString()));
        LOGGER.info(AutoAuditDO.getAuditTypeFromRespMessage(msg));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy