The Japanese upheld the Samurai Bushido code, but they had to change their ways of fighting after encountering the Mongols. Samurai Swords were a strong symbol of honor, fairness (even in battle), courage, and tradition. All these virtues came under fire when the Mongols first invaded Japan. Contrary to the samurais, the Mongols were fierce, brutal, and most importantly, ruthless combatants. They did not follow the bushido code, and would regularly slaughter women and children, rape and pillage entire communities, and generally carry themselves in a manner quite different from Japanese norms.
The first Mongol invasion in 1274 was a test for the japanese samurai, who had to completely change their style of fighting to match their enemies' dishonorable practices. However, they still fought in the bushido style, and the Mongols retreated when they found out they were no match for samurai swords. It was equally surprising to them to encounter people who had so much zest for fighting and feared neither pain nor death. Imagine a Mongol cornering a samurai, and just before he could inflict the lethal blow, the samurai would disembowel himself with his samurai sword rather than die by the filthy blade of the foreigner. Hara Kiri was a very intimidating spectacle to behold.
This race of Mongol "warriors" were of Chinese descent, and were all united under the banner of the ruthless warlord Genghis Khan. Khan was one of the most aggressive conquerors the world has ever known, and his empire expanded from most of Asia all the way to Eastern Europe. He nearly succeeded in conquering Japan, but he was unprepared for the Japanese samurai warrior, who found it an honor to fight and die in battle. Not only was their verve for battle impressive, but their skill with japanese swords was second to none.
The Mongol force retreated shamefully back to their homeland, regrouped, and then returned in 1281. The story of the 1281 attack is a very popular legend to this day in Japan. When the Chinese force returned, their fleet was massive. Seeing their numbers, the samurai knew, even though they were more fearless and more skilled with samurai swords, that they were outnumbered and sure to be overtaken. So a regiment was sent to the mountaintop to pray to the sun god for help. Upon their return, the skies opened up, the seas turned furious, and a tornado ripped across the beach. The Mongol ships were tossed about like driftwood in the bay and smashed against the rocks, killing every last invader. The Japanese forces didn't even have to unsheathe their samurai swords.
Be it domestic invaders or foreign forces, samurais, armed with their trusty samurai swords, could defeat most any foe. In fact, after the Mongol invasion, no one dared invade Japan for another six centuries!