google-nomulus/javatests/google/registry/rdap/RdapSearchActionTestCase.java
guyben 3a3b0b738a Centralize RDAP test initialization and login
All RDAP actions inherit from a common RdapActionBase class.

All RDAP search actions inherit from a comman RdapSearchActionBase class, which inherits from RdapActionBase

Each of the base classes has @Before initialization needed for the tests, as well as utility functions (such as login and logout).

Currently, these were copied in all test classes. Instead, we created a similar test inheritance tree to centralize the initialization and place common utility functions.

This way, the @Before of every test only needs to initialize the variables new to the specific action, making the code somewhat clearer.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=217856844
2018-10-22 19:01:37 -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, String path) {
super(rdapActionClass, path);
}
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());
}
}