[FIXED JENKINS-16846] fix logic and add test

I got Exception on situation added test.

And made patch to fix it.