public void TestEanCodes()
        {
            using (StreamReader sr = new StreamReader(SimpleTestPath)) {
                string currentLine;
                // currentLine will be null when the StreamReader reaches the end of file
                while ((currentLine = sr.ReadLine()) != null) {
                    EanEicCode code = new EanEicCode(currentLine.Split(";".ToCharArray())[0]);
                    CheckResultCode expectedResult = (CheckResultCode)Enum.Parse(typeof(CheckResultCode), currentLine.Split(";".ToCharArray())[1]);

                    CheckResult res = _service.CheckCode(code);
                    Assert.AreEqual(res.ResultCode, expectedResult);
                }
            }
        }
        public CheckResult Post(EanEicCode code)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "POST";
            request.ContentType = "application/json;charset=utf-8";
            var encoding = new UTF8Encoding();

            byte[] bytes = encoding.GetBytes(javaScriptSerializer.Serialize(code));

            request.ContentLength = bytes.Length;

            using (Stream requestStream = request.GetRequestStream()) {
                requestStream.Write(bytes, 0, bytes.Length);
            }
            Stream responseStream = request.GetResponse().GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            return javaScriptSerializer.Deserialize<CheckResult>(reader.ReadToEnd());
        }
Exemplo n.º 3
0
        public CheckResult CheckCode(EanEicCode code)
        {
            if (code == null || string.IsNullOrEmpty(code.Code))
            {
                return new CheckResult(CheckResultCode.NoCodeSupplied);
            }

            //EAN prefix
            if (code.Code.Length >= 2 && code.Code.Substring(0, 2) == "85")
            {
                return ValidateEan(code);
            }

            //EIC prefix
            if (code.Code.Length >= 2 && code.Code.Substring(0, 2) == "27")
            {
                return ValidateEic(code);
            }

            //Prefix did not match
            return new CheckResult(CheckResultCode.CodePrefixInvalid);
        }
Exemplo n.º 4
0
        private CheckResult ValidateEic(EanEicCode code)
        {
            if (code.Code.Length != 16)
            {
                return new CheckResult(CheckResultCode.EicInvalidLength);
            }

            // EIC check character computation algorithm from page 32-33 from the https://www.entsoe.eu/Documents/EDI/Library/2015-0612_451-n%20EICCode_Data_exchange_implementation_guide_final.pdf
            IEnumerable<int> numericEncodingOfEic;
            try
            {
                //Step 1&2
                numericEncodingOfEic = GetNumericEicEncoding(code.Code);
            }
            catch (InvalidCharacterInEic)
            {
                return new CheckResult(CheckResultCode.EicInvalidCharacter);
            }

            //Step 3&4
            IEnumerable<int> weightedList = getWeightedListEic(numericEncodingOfEic.Take(15));
            //Step 5
            int sumOfWeights = weightedList.Sum();
            //Step 6
            int modulo37 = 36 - ((sumOfWeights - 1)%37);
            char checkChar = EncodeIntToChar(modulo37);

            if (checkChar == code.Code.Last())
            {
                return new CheckResult(CheckResultCode.EicOk);
            }
            return new CheckResult(CheckResultCode.EicInvalidCheckCharacter);
        }
Exemplo n.º 5
0
        private static CheckResult ValidateEan(EanEicCode code)
        {
            if (code.Code.Length != 18)
            {
                return new CheckResult(CheckResultCode.EanInvalidLength);
            }

            // sum = EAN[0] * 3 + EAN[1] + EAN[2] * 3 + EAN[3] + ... + EAN[16] * 3
            int sum = 0;
            for (int i = 0; i < 17; i++)
            {
                int digit = code.Code[i] - '0';
                if (digit < 0 || digit > 9)
                {
                    return new CheckResult(CheckResultCode.EanInvalidCharacter);
                }
                if (i%2 == 0)
                {
                    sum += digit*3;
                }
                else
                {
                    sum += digit;
                }
            }

            int lastDigit = code.Code[17] - '0';

            int checkSum = Ceiling(sum, 10) - sum;
            if (lastDigit == checkSum)
            {
                return new CheckResult(CheckResultCode.EanOk);
            }
            return new CheckResult(CheckResultCode.EanInvalidCheckCharacter);
        }