Exemplo n.º 1
0
        public IProtocolObject CreateObject(Protocol.Types objectType, string jsonString = null)
        {
            IProtocolObject newObject = null;

            switch (objectType)
            {
            case Protocol.Types.NewDriver:
                newObject = string.IsNullOrEmpty(jsonString) ? new NewDriver() : JsonSerializer.Deserialize <NewDriver>(jsonString);
                break;

            case Protocol.Types.NewSession:
                newObject = string.IsNullOrEmpty(jsonString) ? new NewSession() : JsonSerializer.Deserialize <NewSession>(jsonString);
                break;

            case Protocol.Types.AuthorizationToken:
                newObject = string.IsNullOrEmpty(jsonString) ? new AuthorizationToken() : JsonSerializer.Deserialize <AuthorizationToken>(jsonString);
                break;

            case Protocol.Types.SessionRun:
                newObject = string.IsNullOrEmpty(jsonString) ? new SessionRun() : JsonSerializer.Deserialize <SessionRun>(jsonString);
                break;

            case Protocol.Types.TransactionRun:
                newObject = string.IsNullOrEmpty(jsonString) ? new TransactionRun() : JsonSerializer.Deserialize <TransactionRun>(jsonString);
                break;

            case Protocol.Types.Result:
                //not used in requests so should not be produced by factory.
                break;

            case Protocol.Types.SessionReadTransaction:
                newObject = string.IsNullOrEmpty(jsonString) ? new SessionReadTransaction() : JsonSerializer.Deserialize <SessionReadTransaction>(jsonString);
                break;

            case Protocol.Types.DriverClose:
                newObject = string.IsNullOrEmpty(jsonString) ? new DriverClose() : JsonSerializer.Deserialize <DriverClose>(jsonString);
                break;

            case Protocol.Types.SessionClose:
                newObject = string.IsNullOrEmpty(jsonString) ? new SessionClose() : JsonSerializer.Deserialize <SessionClose>(jsonString);
                break;

            case Protocol.Types.ResultNext:
                newObject = string.IsNullOrEmpty(jsonString) ? new ResultNext() : JsonSerializer.Deserialize <ResultNext>(jsonString);
                break;
            }

            if (newObject is null)
            {
                throw new System.Exception("Trying to create a none supported object in the ProtocolObjectFactory");
            }

            newObject.SetObjectManager(ObjManager);
            ObjManager.AddProtocolObject(newObject);
            return(newObject);
        }
        public static IProtocolObject CreateObject(Protocol.Types type, string jsonString = null)
        {
            var newObject = (IProtocolObject)CreateNewObjectOfType(Protocol.GetObjectType(type), jsonString, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });

            if (newObject is null)
            {
                throw new Exception($"Trying to create a none supported object in the ProtocolObjectFactory of type {type}");
            }

            newObject.SetObjectManager(ObjManager);
            ObjManager.AddProtocolObject(newObject);
            return(newObject);
        }
Exemplo n.º 3
0
        //[InlineData(Protocol.Types.ResultNext)]
        //[InlineData(Protocol.Types.SessionRun)]
        //[InlineData(Protocol.Types.TransactionRun)]   //TODO... need to reimplement these.
        public void ShouldWriteValidResponse(Protocol.Types objectType)
        {
            var moqStream = new Mock <Stream>();

            moqStream.Setup(x => x.CanWrite).Returns(true);
            var moqWriter = new Mock <Writer>(moqStream.Object);

            var             responseWriter = new ResponseWriter(moqWriter.Object);
            var             objFactory     = new ProtocolObjectFactory(new ProtocolObjectManager());
            IProtocolObject protocolObject = objFactory.CreateObject(objectType);

            var resultString = responseWriter.WriteResponse(protocolObject);

            resultString.Should().Be("#response begin\n" + protocolObject.Response() + "\n#response end");
        }