/*
 */

package com.googlecode.objectify.test;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.test.entity.Trivial;
import com.googlecode.objectify.test.util.TestBase;
import org.junit.jupiter.api.Test;

import static com.google.common.truth.Truth.assertThat;

/**
 * Tests of Key behavior
 *
 * @author Jeff Schnitzer <jeff@infohazard.org>
 */
class KeyTests extends TestBase {
	private static class NoEntity {
	}

	/** */
	@Test
	void kindOfClassWithoutEntityAnnotationIsClassSimpleName() throws Exception {
		final Key<NoEntity> key = Key.create(NoEntity.class, 123L);
		assertThat(key.getKind()).isEqualTo(NoEntity.class.getSimpleName());
	}

	/** */
	@Test
	void canRoundtripBothKindsOfUrlSafeStrings() throws Exception {
		final Key<Trivial> key = Key.create(Trivial.class, 123L);

		final String urlSafe = key.toUrlSafe();
		final Key<Trivial> urlSafeKey = Key.create(urlSafe);
		assertThat(urlSafeKey).isEqualTo(key);

		final String legacyUrlSafe = key.toLegacyUrlSafe();
		final Key<Trivial> legacyUrlSafeKey = Key.create(legacyUrlSafe);
		assertThat(legacyUrlSafeKey).isEqualTo(key);

		assertThat(urlSafe).isNotEqualTo(legacyUrlSafe);
	}

	/** */
	@Test
	void canRoundtripBothKindsOfUrlSafeStringsWithNamespace() throws Exception {
		final Key<Trivial> key = Key.create("namespace", Trivial.class, 123L);

		final String urlSafe = key.toUrlSafe();
		final Key<Trivial> urlSafeKey = Key.create(urlSafe);
		assertThat(urlSafeKey).isEqualTo(key);

		final String legacyUrlSafe = key.toLegacyUrlSafe();
		final Key<Trivial> legacyUrlSafeKey = Key.create(legacyUrlSafe);
		assertThat(legacyUrlSafeKey).isEqualTo(key);

		assertThat(urlSafe).isNotEqualTo(legacyUrlSafe);
	}
}

@@ -285,16 +285,144 @@
 rtThat;%0D
+%0Aimport static com.googlecode.objectify.ObjectifyService.factory;%0D%0Aimport static com.googlecode.objectify.ObjectifyService.ofy;%0D
 %0A%0D%0A/**%0D%0A
@@ -1881,12 +1881,478 @@
 e);%0D%0A%09%7D%0D
+%0A%0D%0A%09@Test%0D%0A%09void urlsafeWorksForSavingAndLoading() throws Exception %7B%0D%0A%09%09factory().register(Trivial.class);%0D%0A%0D%0A%09%09final Trivial triv = new Trivial(123L, %22foo%22, 5);%0D%0A%09%09final Key%3CTrivial%3E savedKey = ofy().save().entity(triv).now();%0D%0A%0D%0A%09%09final String urlSafe = savedKey.toUrlSafe();%0D%0A%09%09final Key%3CTrivial%3E reconstituted = Key.create(urlSafe);%0D%0A%0D%0A%09%09ofy().clear();%0D%0A%09%09final Trivial loaded = ofy().load().key(reconstituted).now();%0D%0A%09%09assertThat(loaded).isEqualTo(triv);%0D%0A%09%7D%0D
 %0A%7D%0D%0A

Add a test for issue #477