Undercover police officer stabbed attempting stop and search, court hears