google-nomulus/javatests/google/registry/rdap/RdapSearchActionTestCase.java
guyben 9f017edc2e Simplify some of the RDAP Action classes
Overriding getter methods to change values is a bit overkill when these values
are static (don't change based on internal state).

Just setting them in the base class' constructor is simpler.

Also, we can read the PATH of an Action based on the Annotation instead
returning it manually for each Action.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=246135754
2019-05-06 16:23:05 -04:00

96 lines
3.8 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.rdap;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import static org.mockito.Mockito.verify;
import com.google.common.collect.ImmutableListMultimap;
import google.registry.rdap.RdapMetrics.EndpointType;
import google.registry.rdap.RdapMetrics.SearchType;
import google.registry.rdap.RdapMetrics.WildcardType;
import google.registry.rdap.RdapSearchResults.IncompletenessWarningType;
import google.registry.request.Action;
import java.util.Optional;
import org.junit.Before;
/** Common unit test code for actions inheriting {@link RdapSearchActionBase}. */
public class RdapSearchActionTestCase<A extends RdapSearchActionBase>
extends RdapActionBaseTestCase<A> {
protected RdapSearchActionTestCase(Class<A> rdapActionClass) {
super(rdapActionClass);
}
SearchType metricSearchType = SearchType.NONE;
WildcardType metricWildcardType = WildcardType.INVALID;
int metricPrefixLength = 0;
int metricStatusCode = SC_OK;
@Before
public void initRdapSearchActionTestCase() {
action.parameterMap = ImmutableListMultimap.of();
action.cursorTokenParam = Optional.empty();
action.rdapResultSetMaxSize = 4;
action.requestUrl = "https://example.tld" + actionPath;
action.requestPath = actionPath;
}
void rememberWildcardType(String queryString) {
try {
RdapSearchPattern partialStringQuery = RdapSearchPattern.create(queryString, true);
if (!partialStringQuery.getHasWildcard()) {
metricWildcardType = WildcardType.NO_WILDCARD;
} else if (partialStringQuery.getSuffix() == null) {
metricWildcardType = WildcardType.PREFIX;
} else if (partialStringQuery.getInitialString().isEmpty()) {
metricWildcardType = WildcardType.SUFFIX;
} else {
metricWildcardType = WildcardType.PREFIX_AND_SUFFIX;
}
metricPrefixLength = partialStringQuery.getInitialString().length();
} catch (Exception e) {
metricWildcardType = WildcardType.INVALID;
metricPrefixLength = 0;
}
}
void verifyMetrics(
EndpointType endpointType,
Action.Method requestMethod,
boolean includeDeleted,
boolean registrarSpecified,
Optional<Long> numDomainsRetrieved,
Optional<Long> numHostsRetrieved,
Optional<Long> numContactsRetrieved,
IncompletenessWarningType incompletenessWarningType) {
RdapMetrics.RdapMetricInformation.Builder builder =
RdapMetrics.RdapMetricInformation.builder()
.setEndpointType(endpointType)
.setSearchType(metricSearchType)
.setWildcardType(metricWildcardType)
.setPrefixLength(metricPrefixLength)
.setIncludeDeleted(includeDeleted)
.setRegistrarSpecified(registrarSpecified)
.setRole(metricRole)
.setRequestMethod(requestMethod)
.setStatusCode(metricStatusCode)
.setIncompletenessWarningType(incompletenessWarningType);
numDomainsRetrieved.ifPresent(builder::setNumDomainsRetrieved);
numHostsRetrieved.ifPresent(builder::setNumHostsRetrieved);
numContactsRetrieved.ifPresent(builder::setNumContactsRetrieved);
verify(rdapMetrics).updateMetrics(builder.build());
}
}