mirror of
https://github.com/google/nomulus.git
synced 2025-05-18 02:09:35 +02:00
This is currently erroring out on entities that fail to load properly through Objectify (e.g. because their entity type is no longer registered). The proper thing to do is to catch the error, log it, and fall back to the raw Datastore operation, which will succeed. The exact Exception this is designed to catch is: com.google.apphosting.runtime.jetty9.JettyLogger warn: /_dr/admin/deleteEntity (JettyLogger.java:29) java.lang.IllegalStateException: No registered subclass for discriminator 'DomainApplication' at com.googlecode.objectify.v4.impl.PolymorphicEntityMetadata.getConcrete(PolymorphicEntityMetadata.java:133) at com.googlecode.objectify.v4.impl.PolymorphicEntityMetadata.load(PolymorphicEntityMetadata.java:164) at com.googlecode.objectify.v4.impl.LoadEngine.load(LoadEngine.java:220) at com.googlecode.objectify.v4.impl.LoadEngine$1.nowUncached(LoadEngine.java:178) at com.googlecode.objectify.v4.impl.LoadEngine$1.nowUncached(LoadEngine.java:164) at com.googlecode.objectify.v4.util.ResultCache.now(ResultCache.java:30) at com.googlecode.objectify.v4.impl.Round$1.nowUncached(Round.java:73) at com.googlecode.objectify.v4.util.ResultCache.now(ResultCache.java:30) at com.googlecode.objectify.v4.LoadResult.now(LoadResult.java:25) at google.registry.tools.server.DeleteEntityAction.loadOfyEntity(DeleteEntityAction.java:103) at google.registry.tools.server.DeleteEntityAction.run(DeleteEntityAction.java:74) ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=230737553
114 lines
4.6 KiB
Java
114 lines
4.6 KiB
Java
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package google.registry.tools.server;
|
|
|
|
import static com.google.appengine.api.datastore.DatastoreServiceFactory.getDatastoreService;
|
|
import static com.google.common.truth.Truth.assertThat;
|
|
import static com.googlecode.objectify.Key.create;
|
|
import static google.registry.model.ofy.ObjectifyService.ofy;
|
|
import static google.registry.testing.JUnitBackports.assertThrows;
|
|
|
|
import com.google.appengine.api.datastore.Entity;
|
|
import com.google.appengine.api.datastore.KeyFactory;
|
|
import google.registry.model.registry.label.ReservedList;
|
|
import google.registry.request.HttpException.BadRequestException;
|
|
import google.registry.testing.AppEngineRule;
|
|
import google.registry.testing.FakeResponse;
|
|
import org.junit.Before;
|
|
import org.junit.Rule;
|
|
import org.junit.Test;
|
|
import org.junit.runner.RunWith;
|
|
import org.junit.runners.JUnit4;
|
|
|
|
/** Unit tests for {@link DeleteEntityAction}. */
|
|
@RunWith(JUnit4.class)
|
|
public class DeleteEntityActionTest {
|
|
|
|
@Rule
|
|
public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build();
|
|
FakeResponse response = new FakeResponse();
|
|
DeleteEntityAction action = new DeleteEntityAction();
|
|
|
|
@Before
|
|
public void init() {
|
|
action.response = response;
|
|
}
|
|
|
|
@Test
|
|
public void test_deleteSingleRawEntitySuccessfully() {
|
|
Entity entity = new Entity("single", "raw");
|
|
getDatastoreService().put(entity);
|
|
action.rawKeys = KeyFactory.keyToString(entity.getKey());
|
|
action.run();
|
|
assertThat(response.getPayload())
|
|
.isEqualTo("Deleted 1 raw entities and 0 registered entities");
|
|
}
|
|
|
|
@Test
|
|
public void test_deleteSingleRegisteredEntitySuccessfully() {
|
|
ReservedList ofyEntity = new ReservedList.Builder().setName("foo").build();
|
|
ofy().saveWithoutBackup().entity(ofyEntity).now();
|
|
action.rawKeys = KeyFactory.keyToString(create(ofyEntity).getRaw());
|
|
action.run();
|
|
assertThat(response.getPayload())
|
|
.isEqualTo("Deleted 0 raw entities and 1 registered entities");
|
|
}
|
|
|
|
@Test
|
|
public void test_deletePolymorphicEntity_fallbackSucceedsForUnregisteredType() {
|
|
Entity entity = new Entity("single", "raw");
|
|
entity.setIndexedProperty("^d", "UnregType");
|
|
getDatastoreService().put(entity);
|
|
action.rawKeys = KeyFactory.keyToString(entity.getKey());
|
|
action.run();
|
|
assertThat(response.getPayload())
|
|
.isEqualTo("Deleted 1 raw entities and 0 registered entities");
|
|
}
|
|
|
|
@Test
|
|
public void test_deleteOneRawEntityAndOneRegisteredEntitySuccessfully() {
|
|
Entity entity = new Entity("first", "raw");
|
|
getDatastoreService().put(entity);
|
|
String rawKey = KeyFactory.keyToString(entity.getKey());
|
|
ReservedList ofyEntity = new ReservedList.Builder().setName("registered").build();
|
|
ofy().saveWithoutBackup().entity(ofyEntity).now();
|
|
String ofyKey = KeyFactory.keyToString(create(ofyEntity).getRaw());
|
|
action.rawKeys = String.format("%s,%s", rawKey, ofyKey);
|
|
action.run();
|
|
assertThat(response.getPayload())
|
|
.isEqualTo("Deleted 1 raw entities and 1 registered entities");
|
|
}
|
|
|
|
@Test
|
|
public void test_deleteNonExistentEntityRepliesWithError() {
|
|
Entity entity = new Entity("not", "here");
|
|
String rawKey = KeyFactory.keyToString(entity.getKey());
|
|
action.rawKeys = rawKey;
|
|
BadRequestException thrown = assertThrows(BadRequestException.class, action::run);
|
|
assertThat(thrown).hasMessageThat().contains("Could not find entity with key " + rawKey);
|
|
}
|
|
|
|
@Test
|
|
public void test_deleteOneEntityAndNonExistentEntityRepliesWithError() {
|
|
ReservedList ofyEntity = new ReservedList.Builder().setName("first_registered").build();
|
|
ofy().saveWithoutBackup().entity(ofyEntity).now();
|
|
String ofyKey = KeyFactory.keyToString(create(ofyEntity).getRaw());
|
|
Entity entity = new Entity("non", "existent");
|
|
String rawKey = KeyFactory.keyToString(entity.getKey());
|
|
action.rawKeys = String.format("%s,%s", ofyKey, rawKey);
|
|
BadRequestException thrown = assertThrows(BadRequestException.class, action::run);
|
|
assertThat(thrown).hasMessageThat().contains("Could not find entity with key " + rawKey);
|
|
}
|
|
}
|