All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.epam.reportportal.aspect.StepNameUtils Maven / Gradle / Ivy

Go to download

A application used as an example on how to set up pushing its components to the Central Repository .

The newest version!
/*
 * Copyright 2019 EPAM Systems
 *
 * 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 com.epam.reportportal.aspect;

import com.epam.reportportal.annotations.Step;
import com.epam.reportportal.utils.templating.TemplateConfiguration;
import com.epam.reportportal.utils.templating.TemplateProcessing;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import javax.annotation.Nonnull;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import static java.util.Optional.ofNullable;

/**
 * Helper methods to generate step names.
 */
public class StepNameUtils {

	private StepNameUtils() {
		throw new IllegalStateException("Static only class");
	}

	/**
	 * Generate step name based on a template bypassed in {@link Step} annotation.
	 *
	 * @param step      annotation
	 * @param signature signature of the method annotated with {@link Step}
	 * @param joinPoint intercepted step join point
	 * @return step name
	 */
	@Nonnull
	public static String getStepName(@Nonnull Step step, @Nonnull MethodSignature signature, @Nonnull JoinPoint joinPoint) {
		String nameTemplate = step.value();
		if (nameTemplate.trim().isEmpty()) {
			return signature.getMethod().getName();
		}

		TemplateConfiguration defaultConfig = new TemplateConfiguration();
		TemplateConfiguration deprecatedConfig = new TemplateConfiguration(step.templateConfig());
		TemplateConfiguration config = new TemplateConfiguration(step.config());
		if (!deprecatedConfig.equals(defaultConfig)) {
			if (config.equals(defaultConfig)) {
				config = deprecatedConfig;
			}
		}

		return getStepName(nameTemplate, config, signature, joinPoint);
	}

	/**
	 * Generate step name based on a template and configuration bypassed.
	 *
	 * @param nameTemplate template string
	 * @param config       template configuration to use
	 * @param signature    signature of the method related to the step
	 * @param joinPoint    intercepted step join point
	 * @return step name
	 */
	@Nonnull
	public static String getStepName(@Nonnull String nameTemplate, @Nonnull TemplateConfiguration config,
			@Nonnull MethodSignature signature, @Nonnull JoinPoint joinPoint) {
		Map parametersMap = createParamsMapping(config, signature, joinPoint);
		return TemplateProcessing.processTemplate(nameTemplate, parametersMap, config);
	}

	@Nonnull
	static Map createParamsMapping(@Nonnull TemplateConfiguration templateConfig, @Nonnull MethodSignature signature,
			@Nonnull JoinPoint joinPoint) {
		Object[] args = joinPoint.getArgs();
		String[] parameterNames = signature.getParameterNames();
		int paramsCount = Math.min(ofNullable(parameterNames).map(p -> p.length).orElse(0), ofNullable(args).map(a -> a.length).orElse(0));
		Map paramsMapping = new HashMap<>();
		ofNullable(signature.getMethod()).map(Method::getName).ifPresent(name -> paramsMapping.put(templateConfig.getMethodName(), name));
		ofNullable(joinPoint.getThis()).ifPresent(current -> paramsMapping.put(templateConfig.getSelfName(), current));
		for (int i = 0; i < paramsCount; i++) {
			paramsMapping.put(parameterNames[i], args[i]);
			paramsMapping.put(Integer.toString(i), args[i]);
		}
		return paramsMapping;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy