diff --git a/.gitignore b/.gitignore index b24d71e..9645f74 100644 --- a/.gitignore +++ b/.gitignore @@ -1,50 +1,18 @@ -# These are some examples of commonly ignored file patterns. -# You should customize this list as applicable to your project. -# Learn more about .gitignore: -# https://www.atlassian.com/git/tutorials/saving-changes/gitignore +#VSCode +.classpath +.project -# Node artifact files -node_modules/ -dist/ +#Ignore the compiled files +target -# Compiled Java class files -*.class - -# Compiled Python bytecode -*.py[cod] - -# Log files +#Ignore log files *.log +*.log.*.gz -# Package files -*.jar - -# Maven -target/ -dist/ - -# JetBrains IDE -.idea/ - -# Unit test reports -TEST*.xml - -# Generated by MacOS -.DS_Store - -# Generated by Windows -Thumbs.db - -# Applications -*.app -*.exe -*.war - -# Large media files -*.mp4 -*.tiff -*.avi -*.flv -*.mov -*.wmv +#Ignore temp files +*.tmp +#Ignore local config files +local/ +sonarBuild.ps1 +sonarBuild.sh diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..29abf99 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,6 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/main/resources=UTF-8 +encoding//src/test/java=UTF-8 +encoding//src/test/resources=UTF-8 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.apt.core.prefs b/.settings/org.eclipse.jdt.apt.core.prefs new file mode 100644 index 0000000..dfa4f3a --- /dev/null +++ b/.settings/org.eclipse.jdt.apt.core.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.apt.aptEnabled=true +org.eclipse.jdt.apt.genSrcDir=target/generated-sources/annotations +org.eclipse.jdt.apt.genTestSrcDir=target/generated-test-sources/test-annotations diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..4768cea --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,105 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.annotation.inheritNullAnnotations=disabled +org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore +org.eclipse.jdt.core.compiler.annotation.nonnull=org.springframework.lang.NonNull +org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.springframework.lang.NonNullApi +org.eclipse.jdt.core.compiler.annotation.nullable=org.springframework.lang.Nullable +org.eclipse.jdt.core.compiler.annotation.nullanalysis=enabled +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=21 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=21 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.autoboxing=ignore +org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning +org.eclipse.jdt.core.compiler.problem.deadCode=warning +org.eclipse.jdt.core.compiler.problem.deprecation=warning +org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled +org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled +org.eclipse.jdt.core.compiler.problem.discouragedReference=warning +org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore +org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore +org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled +org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore +org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning +org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning +org.eclipse.jdt.core.compiler.problem.forbiddenReference=error +org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning +org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=disabled +org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning +org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=warning +org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore +org.eclipse.jdt.core.compiler.problem.localVariableHiding=ignore +org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning +org.eclipse.jdt.core.compiler.problem.missingDefaultCase=ignore +org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore +org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled +org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=ignore +org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=warning +org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled +org.eclipse.jdt.core.compiler.problem.missingSerialVersion=ignore +org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore +org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning +org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning +org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore +org.eclipse.jdt.core.compiler.problem.nonnullParameterAnnotationDropped=warning +org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=warning +org.eclipse.jdt.core.compiler.problem.nullReference=warning +org.eclipse.jdt.core.compiler.problem.nullSpecViolation=warning +org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=ignore +org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning +org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore +org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore +org.eclipse.jdt.core.compiler.problem.potentialNullReference=warning +org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=ignore +org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning +org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning +org.eclipse.jdt.core.compiler.problem.redundantNullCheck=ignore +org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=warning +org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore +org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore +org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled +org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning +org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled +org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled +org.eclipse.jdt.core.compiler.problem.syntacticNullAnalysisForFields=enabled +org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore +org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning +org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=enabled +org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning +org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning +org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore +org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning +org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore +org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=ignore +org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=ignore +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled +org.eclipse.jdt.core.compiler.problem.unusedExceptionParameter=ignore +org.eclipse.jdt.core.compiler.problem.unusedImport=warning +org.eclipse.jdt.core.compiler.problem.unusedLabel=warning +org.eclipse.jdt.core.compiler.problem.unusedLocal=warning +org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=ignore +org.eclipse.jdt.core.compiler.problem.unusedParameter=ignore +org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=enabled +org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled +org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled +org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning +org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=ignore +org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning +org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning +org.eclipse.jdt.core.compiler.processAnnotations=enabled +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=21 diff --git a/.settings/org.eclipse.jdt.ui.prefs b/.settings/org.eclipse.jdt.ui.prefs new file mode 100644 index 0000000..5b25dc1 --- /dev/null +++ b/.settings/org.eclipse.jdt.ui.prefs @@ -0,0 +1,60 @@ +eclipse.preferences.version=1 +editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true +sp_cleanup.add_default_serial_version_id=true +sp_cleanup.add_generated_serial_version_id=false +sp_cleanup.add_missing_annotations=true +sp_cleanup.add_missing_deprecated_annotations=true +sp_cleanup.add_missing_methods=false +sp_cleanup.add_missing_nls_tags=false +sp_cleanup.add_missing_override_annotations=true +sp_cleanup.add_missing_override_annotations_interface_methods=true +sp_cleanup.add_serial_version_id=false +sp_cleanup.always_use_blocks=true +sp_cleanup.always_use_parentheses_in_expressions=false +sp_cleanup.always_use_this_for_non_static_field_access=false +sp_cleanup.always_use_this_for_non_static_method_access=false +sp_cleanup.convert_functional_interfaces=false +sp_cleanup.convert_to_enhanced_for_loop=false +sp_cleanup.correct_indentation=false +sp_cleanup.format_source_code=false +sp_cleanup.format_source_code_changes_only=false +sp_cleanup.insert_inferred_type_arguments=false +sp_cleanup.make_local_variable_final=true +sp_cleanup.make_parameters_final=false +sp_cleanup.make_private_fields_final=true +sp_cleanup.make_type_abstract_if_missing_method=false +sp_cleanup.make_variable_declarations_final=false +sp_cleanup.never_use_blocks=false +sp_cleanup.never_use_parentheses_in_expressions=true +sp_cleanup.on_save_use_additional_actions=true +sp_cleanup.organize_imports=true +sp_cleanup.qualify_static_field_accesses_with_declaring_class=false +sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_with_declaring_class=false +sp_cleanup.qualify_static_method_accesses_with_declaring_class=false +sp_cleanup.remove_private_constructors=true +sp_cleanup.remove_redundant_type_arguments=true +sp_cleanup.remove_trailing_whitespaces=false +sp_cleanup.remove_trailing_whitespaces_all=true +sp_cleanup.remove_trailing_whitespaces_ignore_empty=false +sp_cleanup.remove_unnecessary_casts=true +sp_cleanup.remove_unnecessary_nls_tags=false +sp_cleanup.remove_unused_imports=false +sp_cleanup.remove_unused_local_variables=false +sp_cleanup.remove_unused_private_fields=true +sp_cleanup.remove_unused_private_members=false +sp_cleanup.remove_unused_private_methods=true +sp_cleanup.remove_unused_private_types=true +sp_cleanup.sort_members=false +sp_cleanup.sort_members_all=false +sp_cleanup.use_anonymous_class_creation=false +sp_cleanup.use_blocks=false +sp_cleanup.use_blocks_only_for_return_and_throw=false +sp_cleanup.use_lambda=true +sp_cleanup.use_parentheses_in_expressions=false +sp_cleanup.use_this_for_non_static_field_access=false +sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true +sp_cleanup.use_this_for_non_static_method_access=false +sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true +sp_cleanup.use_type_arguments=false diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/.settings/org.eclipse.pde.prefs b/.settings/org.eclipse.pde.prefs new file mode 100644 index 0000000..2d3d6b7 --- /dev/null +++ b/.settings/org.eclipse.pde.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +compilers.p.unresolved-ex-points=1 \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..b2ca225 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + "configurations": [ + { + "type": "java", + "name": "Spring Boot-CipherStreamWeb", + "request": "launch", + "cwd": "${workspaceFolder}", + "mainClass": "com.mattrixwv.cipherstream.CipherStreamWeb", + "projectName": "cipherstream-website", + "args": "--spring.config.additional-location=local/application.properties --logging.config=local/log4j2-spring.xml", + "envFile": "${workspaceFolder}/.env" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..ed8a344 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,18 @@ +{ + "sonarlint.connectedMode.project": { + "connectionId": "mattrixwvSonarqube", + "projectKey": "CipherStreamWeb" + }, + "java.configuration.updateBuildConfiguration": "automatic", + "java.compile.nullAnalysis.mode": "automatic", + "java.test.config": { + "name": "", + "workingDirectory": "${workspaceFolder}", + "testKind": "junit", + "filters": { + "tags": [ + "unit-test" + ] + } + } +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..9273d41 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,46 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "java (buildArtifact)", + "mainClass": "com.mattrixwv.cipherstream.CipherStreamWebsite.java", + "targetPath": "${workspaceFolder}/${workspaceFolderBasename}.jar", + "elements": [ + "${compileOutput}", + "${dependencies}" + ], + "problemMatcher": [], + "label": "java: exportjar:CipherStreamWeb" + }, + { + "label": "sonarqube build (Windows)", + "group": "build", + "type": "shell", + "command": "./sonarBuild.ps1", + "presentation": { + "echo": true, + "reveal": "always", + "focus": true, + "panel": "shared", + "showReuseMessage": true, + "clear": false + }, + "problemMatcher": [] + }, + { + "label": "sonarqube build (Linux)", + "group": "build", + "type": "shell", + "command": "./sonarBuild.sh", + "presentation": { + "echo": true, + "reveal": "always", + "focus": true, + "panel": "shared", + "showReuseMessage": true, + "clear": false + }, + "problemMatcher": [] + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/lombok.config b/lombok.config new file mode 100644 index 0000000..df71bb6 --- /dev/null +++ b/lombok.config @@ -0,0 +1,2 @@ +config.stopBubbling = true +lombok.addLombokGeneratedAnnotation = true diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..72a704c --- /dev/null +++ b/pom.xml @@ -0,0 +1,316 @@ + + + 4.0.0 + + com.mattrixwv.cipherstream + cipherstream-api + jar + 1.0.0-SNAPSHOT + CipherStream API + https://cipherstream.mattrixwv.com/api + + + + UTF-8 + 21 + 21 + 21 + + + 21 + target/dependency-check-report.json + target/dependency-check-report.html + + + + org.springframework.boot + spring-boot-starter-parent + 3.2.4 + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-logging + + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-devtools + true + runtime + + + + + org.projectlombok + lombok + 1.18.32 + provided + + + + + org.springframework.boot + spring-boot-starter-log4j2 + + + org.apache.logging.log4j + log4j-layout-template-json + 2.23.1 + + + com.lmax + disruptor + 4.0.0 + + + + + org.webjars + jquery + 3.7.1 + + + + + org.aspectj + aspectjweaver + 1.9.22 + + + + + com.mattrixwv + cipher-stream-java + 1.3.3 + + + + + org.springframework.boot + spring-boot-starter-test + test + + + com.vaadin.external.google + android-json + + + + + org.simplify4u + slf4j2-mock + 2.3.0 + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.apache.maven.plugins + maven-enforcer-plugin + 3.4.1 + + + enforce-maven + + enforce + + + + + 3.8.6 + + + + + + + + org.apache.maven.plugins + maven-clean-plugin + 3.3.2 + + + org.apache.maven.plugins + maven-resources-plugin + 3.3.1 + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + + -Xlint:all + + true + true + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.2.5 + + ${skip.unit.tests} + + **/*IntegrationTest.java + + + org.apache.logging.log4j:log4j-slf4j2-impl + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.3.0 + + + org.apache.maven.plugins + maven-install-plugin + 3.1.1 + + + org.apache.maven.plugins + maven-deploy-plugin + 3.1.1 + + + org.apache.maven.plugins + maven-failsafe-plugin + 3.2.5 + + + + ${skip.integration.tests} + + **/*IntegrationTest.java + + + org.apache.logging.log4j:log4j-slf4j2-impl + + + + + + + org.apache.maven.plugins + maven-site-plugin + 3.12.1 + + + org.apache.maven.plugins + maven-project-info-reports-plugin + 3.5.0 + + + org.codehaus.mojo + versions-maven-plugin + 2.16.2 + + file://${session.executionRootDirectory}/version-rules.xml + + + + + org.sonarsource.scanner.maven + sonar-maven-plugin + 3.11.0.3922 + + + org.jacoco + jacoco-maven-plugin + 0.8.12 + + + **/CipherStreamWeb* + + + + + jacoco-initialize + none + + prepare-agent + + + + jacoco-site + none + + report + + + + + + org.owasp + dependency-check-maven + 9.1.0 + + + none + + check + + + + + + json + html + + + + + + + + + unit-test + + true + false + + + + + integration-test + + false + true + + + + + full-test + + true + + + false + false + + + + diff --git a/src/main/java/com/mattrixwv/cipherstream/CipherStreamAPI.java b/src/main/java/com/mattrixwv/cipherstream/CipherStreamAPI.java new file mode 100644 index 0000000..088dec6 --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/CipherStreamAPI.java @@ -0,0 +1,13 @@ +package com.mattrixwv.cipherstream; + + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + + +@SpringBootApplication +public class CipherStreamAPI{ + public static void main(String[] args){ + SpringApplication.run(CipherStreamAPI.class, args); + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/aspect/CipherStreamLoggingAspect.java b/src/main/java/com/mattrixwv/cipherstream/aspect/CipherStreamLoggingAspect.java new file mode 100644 index 0000000..a1958af --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/aspect/CipherStreamLoggingAspect.java @@ -0,0 +1,36 @@ +package com.mattrixwv.cipherstream.aspect; + + +import org.aspectj.lang.annotation.AfterReturning; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.context.annotation.Configuration; + +import com.fasterxml.jackson.databind.node.ObjectNode; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@Aspect +@Configuration +public class CipherStreamLoggingAspect{ + public static final String CIPHER_NAME_LOGGING = "cipher"; + + + @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping) || @annotation(org.springframework.web.bind.annotation.GetMapping) || @annotation(org.springframework.web.bind.annotation.PutMapping) || @annotation(org.springframework.web.bind.annotation.PostMapping) || @annotation(org.springframework.web.bind.annotation.DeleteMapping)") + public void mappedFunction(){ + //Intentionally blank + } + + @Pointcut("execution(* com.mattrixwv.cipherstream.controller..*(..))") + public void cipherMethod(){ + //Intentionally blank + } + + @AfterReturning(pointcut = "cipherMethod() && mappedFunction()", returning = "returnedJson") + public void getCipherInfo(ObjectNode returnedJson){ + //Print a log + log.info("CipherStream log {}", returnedJson); + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/config/FullFilter.java b/src/main/java/com/mattrixwv/cipherstream/config/FullFilter.java new file mode 100644 index 0000000..8742a61 --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/config/FullFilter.java @@ -0,0 +1,66 @@ +package com.mattrixwv.cipherstream.config; + + +import java.io.IOException; +import java.util.StringJoiner; + +import org.slf4j.MDC; +import org.springframework.lang.NonNull; +import org.springframework.stereotype.Component; +import org.springframework.web.filter.OncePerRequestFilter; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@Component +public class FullFilter extends OncePerRequestFilter{ + @Override + protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull FilterChain filterChain) throws ServletException, IOException{ + //Get the requestId + if(request.getHeader("X-Request-Id") != null){ + MDC.put("requestId", request.getHeader("X-Request-Id")); + } + + //Get IP address + if(request.getHeader("X-Forwarded-For") != null){ + MDC.put("ip", request.getHeader("X-Forwarded-For").split(",")[0]); + } + + //Get all of the parameters in the request and print them in the log + StringJoiner parameters = new StringJoiner(", "); + request.getParameterMap().entrySet().forEach(entry -> { + if(!entry.getKey().equals("_")){ + String key = entry.getKey(); + String value = ""; + if(entry.getValue().length > 1){ + StringJoiner joiner = new StringJoiner(", ", "[", "]"); + for(String str : entry.getValue()){ + joiner.add(str); + } + value = joiner.toString(); + } + else{ + value = entry.getValue()[0]; + } + parameters.add(key + "->" + value); + } + }); + if(parameters.length() > 0){ + log.info("Request parameters: {}", parameters); + } + + //Get the path + MDC.put("url", request.getRequestURI()); + + //Continue to the controller + filterChain.doFilter(request, response); + + //Clear the MDC for the next request + MDC.clear(); + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/ExceptionRestController.java b/src/main/java/com/mattrixwv/cipherstream/controller/ExceptionRestController.java new file mode 100644 index 0000000..fa27e75 --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/ExceptionRestController.java @@ -0,0 +1,52 @@ +package com.mattrixwv.cipherstream.controller; + + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestControllerAdvice(annotations = RestController.class) +public class ExceptionRestController extends ResponseEntityExceptionHandler{ + private static final ObjectMapper mapper = new ObjectMapper(); + + + @ExceptionHandler(InvalidCipherParameterException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ResponseBody + public ObjectNode invalidCipherParameterHandler(InvalidCipherParameterException error){ + log.warn("Invalid user input was supplied: {}", error.getMessage()); + + + ObjectNode returnJson = mapper.createObjectNode(); + returnJson.put("message", error.getMessage()); + + + return returnJson; + } + + @ExceptionHandler(Exception.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + @ResponseBody + public ObjectNode genericExceptionHandler(Exception error){ + log.error(error.getMessage(), error); + + + ObjectNode returnJson = mapper.createObjectNode(); + returnJson.put("message", error.getMessage()); + + + return returnJson; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/combination/AdfgvxCipherController.java b/src/main/java/com/mattrixwv/cipherstream/controller/combination/AdfgvxCipherController.java new file mode 100644 index 0000000..2511861 --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/combination/AdfgvxCipherController.java @@ -0,0 +1,69 @@ +package com.mattrixwv.cipherstream.controller.combination; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.combination.ADFGVX; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("adfgvx") +public class AdfgvxCipherController{ + @GetMapping("/encode") + public ObjectNode encodeAdfgvx(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "ADFGVX"); + log.info("Encoding ADFGVX"); + + + CipherParameterUtil.verifySquareKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String squareKeyword = cipherParams.get(CipherParameterUtil.SQUARE_KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + ADFGVX adfgvx = new ADFGVX(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = adfgvx.encode(squareKeyword, keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodeAdfgvx(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "ADFGVX"); + log.info("Decoding ADFGVX"); + + + CipherParameterUtil.verifySquareKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String squareKeyword = cipherParams.get(CipherParameterUtil.SQUARE_KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + ADFGVX adfgvx = new ADFGVX(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = adfgvx.decode(squareKeyword, keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/combination/AdfgxCipherController.java b/src/main/java/com/mattrixwv/cipherstream/controller/combination/AdfgxCipherController.java new file mode 100644 index 0000000..8d9fca7 --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/combination/AdfgxCipherController.java @@ -0,0 +1,69 @@ +package com.mattrixwv.cipherstream.controller.combination; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.combination.ADFGX; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("adfgx") +public class AdfgxCipherController{ + @GetMapping("/encode") + public ObjectNode encodeAdfgx(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "ADFGX"); + log.info("Encoding ADFGX"); + + + CipherParameterUtil.verifySquareKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String squareKeyword = cipherParams.get(CipherParameterUtil.SQUARE_KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + ADFGX adfgx = new ADFGX(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = adfgx.encode(squareKeyword, keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodeAdfgx(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "ADFGX"); + log.info("Decoding ADFGX"); + + + CipherParameterUtil.verifySquareKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String squareKeyword = cipherParams.get(CipherParameterUtil.SQUARE_KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + ADFGX adfgx = new ADFGX(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = adfgx.decode(squareKeyword, keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/AffineCipherController.java b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/AffineCipherController.java new file mode 100644 index 0000000..048119f --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/AffineCipherController.java @@ -0,0 +1,69 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.monosubstitution.Affine; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("/cipherStream/affine") +public class AffineCipherController{ + @GetMapping("/encode") + public ObjectNode encodeAffine(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Affine"); + log.info("Encoding Affine"); + + + CipherParameterUtil.verifyAffineParams(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + int key1 = cipherParams.get(CipherParameterUtil.AFFINE_KEY_1).asInt(); + int key2 = cipherParams.get(CipherParameterUtil.AFFINE_KEY_2).asInt(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Affine affine = new Affine(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = affine.encode(key1, key2, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodeAffine(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Affine"); + log.info("Decoding Affine"); + + + CipherParameterUtil.verifyAffineParams(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + int key1 = cipherParams.get(CipherParameterUtil.AFFINE_KEY_1).asInt(); + int key2 = cipherParams.get(CipherParameterUtil.AFFINE_KEY_2).asInt(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Affine affine = new Affine(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = affine.decode(key1, key2, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/AtbashCipherController.java b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/AtbashCipherController.java new file mode 100644 index 0000000..7b94ff8 --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/AtbashCipherController.java @@ -0,0 +1,65 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.monosubstitution.Atbash; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("/cipherStream/atbash") +public class AtbashCipherController{ + @GetMapping("/encode") + public ObjectNode encodeAtbash(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Atbash"); + log.info("Encoding Atbash"); + + + CipherParameterUtil.verifyAtbashParams(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Atbash atbash = new Atbash(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = atbash.encode(inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodeAtbash(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Atbash"); + log.info("Decoding Atbash"); + + + CipherParameterUtil.verifyAtbashParams(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Atbash atbash = new Atbash(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = atbash.decode(inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/AutokeyCipherController.java b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/AutokeyCipherController.java new file mode 100644 index 0000000..639bcaa --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/AutokeyCipherController.java @@ -0,0 +1,67 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.monosubstitution.Autokey; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("/cipherStream/autokey") +public class AutokeyCipherController{ + @GetMapping("/encode") + public ObjectNode encodeAutokey(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Autokey"); + log.info("Encoding Autokey"); + + + CipherParameterUtil.verifyParamsWithKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Autokey autokey = new Autokey(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = autokey.encode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodeAutokey(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Autokey"); + log.info("Decoding Autokey"); + + + CipherParameterUtil.verifyParamsWithKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Autokey autokey = new Autokey(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = autokey.decode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/BaconianCipherController.java b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/BaconianCipherController.java new file mode 100644 index 0000000..6e2287e --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/BaconianCipherController.java @@ -0,0 +1,61 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.monosubstitution.Baconian; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("/cipherStream/baconian") +public class BaconianCipherController{ + @GetMapping("/encode") + public ObjectNode encodeBaconian(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Baconian"); + log.info("Encoding Baconian"); + + + CipherParameterUtil.verifyBaconianParams(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Baconian baconian = new Baconian(preserveCapitals); + String outputString = baconian.encode(inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodeBaconian(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Baconian"); + log.info("Decoding Baconian"); + + + CipherParameterUtil.verifyBaconianParams(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Baconian baconian = new Baconian(preserveCapitals); + String outputString = baconian.decode(inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/BaseXCipherController.java b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/BaseXCipherController.java new file mode 100644 index 0000000..cea8578 --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/BaseXCipherController.java @@ -0,0 +1,61 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.monosubstitution.BaseX; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("/cipherStream/basex") +public class BaseXCipherController{ + @GetMapping("/encode") + public ObjectNode encodeBaseX(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "BaseX"); + log.info("Encoding BaseX"); + + + CipherParameterUtil.verifyBaseXParams(cipherParams); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + int base = cipherParams.get(CipherParameterUtil.BASE_X_BASE).asInt(); + + + BaseX baseX = new BaseX(base); + String outputString = baseX.encode(inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodeBaseX(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "BaseX"); + log.info("Decoding BaseX"); + + + CipherParameterUtil.verifyBaseXParams(cipherParams); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + int base = cipherParams.get(CipherParameterUtil.BASE_X_BASE).asInt(); + + + BaseX baseX = new BaseX(base); + String outputString = baseX.decode(inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/BeaufortCipherController.java b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/BeaufortCipherController.java new file mode 100644 index 0000000..9fc449c --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/BeaufortCipherController.java @@ -0,0 +1,67 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.monosubstitution.Beaufort; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("/cipherStream/beaufort") +public class BeaufortCipherController{ + @GetMapping("/encode") + public ObjectNode encodeBeaufort(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Beaufort"); + log.info("Encoding Beaufort"); + + + CipherParameterUtil.verifyParamsWithKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Beaufort beaufort = new Beaufort(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = beaufort.encode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodeBeaufort(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Beaufort"); + log.info("Decoding Beaufort"); + + + CipherParameterUtil.verifyParamsWithKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Beaufort beaufort = new Beaufort(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = beaufort.decode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/CaesarCipherController.java b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/CaesarCipherController.java new file mode 100644 index 0000000..3eb6412 --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/CaesarCipherController.java @@ -0,0 +1,66 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.monosubstitution.Caesar; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("/cipherStream/caesar") +public class CaesarCipherController{ + @GetMapping("/encode") + public ObjectNode encodeCaesar(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Caesar"); + log.info("Encoding Caesar"); + + + CipherParameterUtil.verifyCaesarParams(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + int shiftAmount = cipherParams.get(CipherParameterUtil.CAESAR_SHIFT_AMOUNT).asInt(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Caesar caesar = new Caesar(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = caesar.encode(shiftAmount, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodeCaesar(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Caesar"); + log.info("Decoding Caesar"); + + + CipherParameterUtil.verifyCaesarParams(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + int shiftAmount = cipherParams.get(CipherParameterUtil.CAESAR_SHIFT_AMOUNT).asInt(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + Caesar caesar = new Caesar(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = caesar.decode(shiftAmount, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/OneTimePadCipherController.java b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/OneTimePadCipherController.java new file mode 100644 index 0000000..67e47f8 --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/OneTimePadCipherController.java @@ -0,0 +1,67 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.monosubstitution.OneTimePad; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("/cipherStream/oneTimePad") +public class OneTimePadCipherController{ + @GetMapping("/encode") + public ObjectNode encodeOneTimePad(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "One-Time Pad"); + log.info("Encoding One Time Pad"); + + + CipherParameterUtil.verifyParamsWithKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + OneTimePad oneTimePad = new OneTimePad(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = oneTimePad.encode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodeOneTimePad(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "One-Time Pad"); + log.info("Decoding One Time Pad"); + + + CipherParameterUtil.verifyParamsWithKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + OneTimePad oneTimePad = new OneTimePad(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = oneTimePad.decode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/PortaCipherController.java b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/PortaCipherController.java new file mode 100644 index 0000000..4b87e20 --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/PortaCipherController.java @@ -0,0 +1,67 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.monosubstitution.Porta; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("/cipherStream/porta") +public class PortaCipherController{ + @GetMapping("/encode") + public ObjectNode encodePorta(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Porta"); + log.info("Encoding Porta"); + + + CipherParameterUtil.verifyParamsWithKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Porta porta = new Porta(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = porta.encode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodePorta(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Porta"); + log.info("Decoding Porta"); + + + CipherParameterUtil.verifyParamsWithKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Porta porta = new Porta(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = porta.decode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/SubstitutionCipherController.java b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/SubstitutionCipherController.java new file mode 100644 index 0000000..15a5bd9 --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/SubstitutionCipherController.java @@ -0,0 +1,67 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.monosubstitution.Substitution; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("/cipherStream/substitution") +public class SubstitutionCipherController{ + @GetMapping("/encode") + public ObjectNode encodeSubstitution(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Substitution"); + log.info("Encoding Substitution"); + + + CipherParameterUtil.verifyParamsWithKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Substitution substitution = new Substitution(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = substitution.encode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodeSubstitution(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Substitution"); + log.info("Decoding Substitution"); + + + CipherParameterUtil.verifyParamsWithKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Substitution substitution = new Substitution(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = substitution.decode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/VigenereCipherController.java b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/VigenereCipherController.java new file mode 100644 index 0000000..a99c21e --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/VigenereCipherController.java @@ -0,0 +1,67 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.monosubstitution.Vigenere; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("/cipherStream/vigenere") +public class VigenereCipherController{ + @GetMapping("/encode") + public ObjectNode encodeVigenere(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Vigenere"); + log.info("Encoding Vigenere"); + + + CipherParameterUtil.verifyParamsWithKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Vigenere vigenere = new Vigenere(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = vigenere.encode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodeVigenere(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Vigenere"); + log.info("Decoding Vigenere"); + + + CipherParameterUtil.verifyParamsWithKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Vigenere vigenere = new Vigenere(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = vigenere.decode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/BifidCipherController.java b/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/BifidCipherController.java new file mode 100644 index 0000000..d464df2 --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/BifidCipherController.java @@ -0,0 +1,67 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.polysubstitution.Bifid; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("/cipherStream/bifid") +public class BifidCipherController{ + @GetMapping("/encode") + public ObjectNode encodeBifid(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Bifid"); + log.info("Encoding Bifid"); + + + CipherParameterUtil.verifyParamsWithKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Bifid bifid = new Bifid(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = bifid.encode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodeBifid(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Bifid"); + log.info("Decoding Bifid"); + + + CipherParameterUtil.verifyParamsWithKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Bifid bifid = new Bifid(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = bifid.decode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/ColumnarCipherController.java b/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/ColumnarCipherController.java new file mode 100644 index 0000000..a33c04e --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/ColumnarCipherController.java @@ -0,0 +1,67 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.polysubstitution.Columnar; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("/cipherStream/columnar") +public class ColumnarCipherController{ + @GetMapping("/encode") + public ObjectNode encodeColumnar(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Columnar"); + log.info("Encoding Columnar"); + + + CipherParameterUtil.verifyParamsWithKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Columnar columnar = new Columnar(preserveCapitals, preserveWhitespace, preserveSymbols, true); + String outputString = columnar.encode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodeColumnar(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Columnar"); + log.info("Decoding Columnar"); + + + CipherParameterUtil.verifyParamsWithKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Columnar columnar = new Columnar(preserveCapitals, preserveWhitespace, preserveSymbols, true); + String outputString = columnar.decode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/HillCipherController.java b/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/HillCipherController.java new file mode 100644 index 0000000..60831ce --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/HillCipherController.java @@ -0,0 +1,69 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.polysubstitution.Hill; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("/cipherStream/hill") +public class HillCipherController{ + @GetMapping("/encode") + public ObjectNode encodeHill(@RequestBody ObjectNode cipherParams) throws JsonProcessingException{ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Hill"); + log.info("Encoding Hill"); + + + CipherParameterUtil.verifyHillParams(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + int[][] key = new ObjectMapper().treeToValue(cipherParams.get(CipherParameterUtil.HILL_KEY), int[][].class); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Hill hill = new Hill(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = hill.encode(key, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodeHill(@RequestBody ObjectNode cipherParams) throws JsonProcessingException{ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Hill"); + log.info("Decoding Hill"); + + + CipherParameterUtil.verifyHillParams(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + int[][] key = new ObjectMapper().treeToValue(cipherParams.get(CipherParameterUtil.HILL_KEY), int[][].class); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Hill hill = new Hill(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = hill.decode(key, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/MorseCodeController.java b/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/MorseCodeController.java new file mode 100644 index 0000000..d13086a --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/MorseCodeController.java @@ -0,0 +1,59 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.polysubstitution.Morse; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("/cipherStream/morse") +public class MorseCodeController{ + @GetMapping("/encode") + public ObjectNode encodeMorse(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Morse"); + log.info("Encoding Morse"); + + + CipherParameterUtil.verifyMorseParams(cipherParams); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Morse morse = new Morse(); + String outputString = morse.encode(inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodeMorse(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Morse"); + log.info("Decoding Morse"); + + + CipherParameterUtil.verifyMorseParams(cipherParams); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Morse morse = new Morse(); + String outputString = morse.decode(inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/PlayfairCipherController.java b/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/PlayfairCipherController.java new file mode 100644 index 0000000..f3d3a40 --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/PlayfairCipherController.java @@ -0,0 +1,67 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.polysubstitution.Playfair; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("/cipherStream/playfair") +public class PlayfairCipherController{ + @GetMapping("/encode") + public ObjectNode encodePlayfair(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Playfair"); + log.info("Encoding Playfair"); + + + CipherParameterUtil.verifyParamsWithKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Playfair playfair = new Playfair(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = playfair.encode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodePlayfair(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Playfair"); + log.info("Decoding Playfair"); + + + CipherParameterUtil.verifyParamsWithKeyword(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Playfair playfair = new Playfair(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = playfair.decode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/PolybiusSquareController.java b/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/PolybiusSquareController.java new file mode 100644 index 0000000..35a4994 --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/PolybiusSquareController.java @@ -0,0 +1,65 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("/cipherStream/polybius") +public class PolybiusSquareController{ + @GetMapping("/encode") + public ObjectNode encodePolybius(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Polybius Square"); + log.info("Encoding Polybius"); + + + CipherParameterUtil.verifyPolybiusParams(cipherParams); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + PolybiusSquare polybiusSquare = new PolybiusSquare(preserveWhitespace, preserveSymbols); + String outputString = polybiusSquare.encode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodePolybius(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Polybius Square"); + log.info("Decoding Polybius"); + + + CipherParameterUtil.verifyPolybiusParams(cipherParams); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + PolybiusSquare polybiusSquare = new PolybiusSquare(preserveWhitespace, preserveSymbols); + String outputString = polybiusSquare.decode(keyword, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/RailFenceController.java b/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/RailFenceController.java new file mode 100644 index 0000000..480519d --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/RailFenceController.java @@ -0,0 +1,67 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.polysubstitution.RailFence; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("/cipherStream/railFence") +public class RailFenceController{ + @GetMapping("/encode") + public ObjectNode encodeRailFence(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Rail Fence"); + log.info("Encoding Rail Fence"); + + + CipherParameterUtil.verifyRailFenceParams(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + int rails = cipherParams.get(CipherParameterUtil.RAIL_FENCE_RAILS).asInt(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + RailFence railFence = new RailFence(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = railFence.encode(rails, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodeRailFence(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Rail Fence"); + log.info("Decoding Rail Fence"); + + + CipherParameterUtil.verifyRailFenceParams(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + int rails = cipherParams.get(CipherParameterUtil.RAIL_FENCE_RAILS).asInt(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + RailFence railFence = new RailFence(preserveCapitals, preserveWhitespace, preserveSymbols); + String outputString = railFence.decode(rails, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/TrifidCipherController.java b/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/TrifidCipherController.java new file mode 100644 index 0000000..68804ea --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/controller/polysubstitution/TrifidCipherController.java @@ -0,0 +1,71 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import org.slf4j.MDC; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.polysubstitution.Trifid; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +@RestController +@RequestMapping("/cipherStream/trifid") +public class TrifidCipherController{ + @GetMapping("/encode") + public ObjectNode encodeTrifid(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Trifid"); + log.info("Encoding Trifid"); + + + CipherParameterUtil.verifyTrifidParams(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + char fill = cipherParams.get(CipherParameterUtil.TRIFID_FILL).asText().charAt(0); + int groupLength = cipherParams.get(CipherParameterUtil.TRIFID_GROUP_LENGTH).asInt(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Trifid trifid = new Trifid(preserveCapitals, preserveWhitespace, preserveSymbols, fill); + String outputString = trifid.encode(keyword, groupLength, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } + + @GetMapping("/decode") + public ObjectNode decodeTrifid(@RequestBody ObjectNode cipherParams){ + MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Trifid"); + log.info("Decoding Trifid"); + + + CipherParameterUtil.verifyTrifidParams(cipherParams); + boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); + boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean(); + boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean(); + String keyword = cipherParams.get(CipherParameterUtil.KEYWORD).asText(); + char fill = cipherParams.get(CipherParameterUtil.TRIFID_FILL).asText().charAt(0); + int groupLength = cipherParams.get(CipherParameterUtil.TRIFID_GROUP_LENGTH).asInt(); + String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText(); + + + Trifid trifid = new Trifid(preserveCapitals, preserveWhitespace, preserveSymbols, fill); + String outputString = trifid.decode(keyword, groupLength, inputString); + + cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); + + + return cipherParams; + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/exception/InvalidCipherParameterException.java b/src/main/java/com/mattrixwv/cipherstream/exception/InvalidCipherParameterException.java new file mode 100644 index 0000000..f23d05b --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/exception/InvalidCipherParameterException.java @@ -0,0 +1,17 @@ +package com.mattrixwv.cipherstream.exception; + + +public class InvalidCipherParameterException extends RuntimeException{ + public InvalidCipherParameterException(){ + super(); + } + public InvalidCipherParameterException(String message){ + super(message); + } + public InvalidCipherParameterException(Throwable throwable){ + super(throwable); + } + public InvalidCipherParameterException(String message, Throwable throwable){ + super(message, throwable); + } +} diff --git a/src/main/java/com/mattrixwv/cipherstream/utils/CipherParameterUtil.java b/src/main/java/com/mattrixwv/cipherstream/utils/CipherParameterUtil.java new file mode 100644 index 0000000..900272e --- /dev/null +++ b/src/main/java/com/mattrixwv/cipherstream/utils/CipherParameterUtil.java @@ -0,0 +1,256 @@ +package com.mattrixwv.cipherstream.utils; + + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; + +import lombok.experimental.UtilityClass; + + +@UtilityClass +public class CipherParameterUtil{ + //Error messages + public static final String PRESENT_MESSAGE = " must be present"; + public static final String INTEGER_MESSAGE = " must be an integer"; + public static final String TEXT_MESSAGE = " must be text"; + public static final String BOOLEAN_MESSAGE = " must be a boolean"; + public static final String INTEGER_ARRAY_MESSAGE = " must be an integer array"; + public static final String CHARACTER_MESSAGE = " must be a single character"; + //General + public static final String PRESERVE_CAPITALS = "preserveCapitals"; + public static final String PRESERVE_WHITESPACE = "preserveWhitespace"; + public static final String PRESERVE_SYMBOLS = "preserveSymbols"; + public static final String INPUT_STRING = "inputString"; + public static final String OUTPUT_STRING = "outputString"; + public static final String KEYWORD = "keyword"; + + + //Caesar + public static final String CAESAR_SHIFT_AMOUNT = "shiftAmount"; + + //Affine + public static final String AFFINE_KEY_1 = "key1"; + public static final String AFFINE_KEY_2 = "key2"; + + //BaseX + public static final String BASE_X_BASE = "base"; + + //ADFGX + public static final String SQUARE_KEYWORD = "squareKeyword"; + + //Hill + public static final String HILL_KEY = "key"; + + //Rail Fence + public static final String RAIL_FENCE_RAILS = "rails"; + + //Trifid + public static final String TRIFID_FILL = "fill"; + public static final String TRIFID_GROUP_LENGTH = "groupLength"; + + + public static void verifyUniversalParams(ObjectNode params){ + if(!params.has(PRESERVE_CAPITALS)){ + throw new InvalidCipherParameterException(PRESERVE_CAPITALS + PRESENT_MESSAGE); + } + if(!params.get(PRESERVE_CAPITALS).isBoolean()){ + throw new InvalidCipherParameterException(PRESERVE_CAPITALS + BOOLEAN_MESSAGE); + } + + if(!params.has(PRESERVE_WHITESPACE)){ + throw new InvalidCipherParameterException(PRESERVE_WHITESPACE + PRESENT_MESSAGE); + } + if(!params.get(PRESERVE_WHITESPACE).isBoolean()){ + throw new InvalidCipherParameterException(PRESERVE_WHITESPACE + BOOLEAN_MESSAGE); + } + + if(!params.has(PRESERVE_SYMBOLS)){ + throw new InvalidCipherParameterException(PRESERVE_SYMBOLS + PRESENT_MESSAGE); + } + if(!params.get(PRESERVE_SYMBOLS).isBoolean()){ + throw new InvalidCipherParameterException(PRESERVE_SYMBOLS + BOOLEAN_MESSAGE); + } + + if(!params.has(INPUT_STRING)){ + throw new InvalidCipherParameterException(INPUT_STRING + PRESENT_MESSAGE); + } + if(!params.get(INPUT_STRING).isTextual()){ + throw new InvalidCipherParameterException(INPUT_STRING + TEXT_MESSAGE); + } + } + + public static void verifyParamsWithKeyword(ObjectNode params){ + verifyUniversalParams(params); + + if(!params.has(KEYWORD)){ + throw new InvalidCipherParameterException(KEYWORD + PRESENT_MESSAGE); + } + if(!params.get(KEYWORD).isTextual()){ + throw new InvalidCipherParameterException(KEYWORD + TEXT_MESSAGE); + } + } + + public static void verifyCaesarParams(ObjectNode params){ + verifyUniversalParams(params); + + if(!params.has(CAESAR_SHIFT_AMOUNT)){ + throw new InvalidCipherParameterException(CAESAR_SHIFT_AMOUNT + PRESENT_MESSAGE); + } + if(!params.get(CAESAR_SHIFT_AMOUNT).isInt()){ + throw new InvalidCipherParameterException(CAESAR_SHIFT_AMOUNT + INTEGER_MESSAGE); + } + } + + public static void verifyAffineParams(ObjectNode params){ + verifyUniversalParams(params); + + if(!params.has(AFFINE_KEY_1)){ + throw new InvalidCipherParameterException(AFFINE_KEY_1 + PRESENT_MESSAGE); + } + if(!params.get(AFFINE_KEY_1).isInt()){ + throw new InvalidCipherParameterException(AFFINE_KEY_1 + INTEGER_MESSAGE); + } + + if(!params.has(AFFINE_KEY_2)){ + throw new InvalidCipherParameterException(AFFINE_KEY_2 + PRESENT_MESSAGE); + } + if(!params.get(AFFINE_KEY_2).isInt()){ + throw new InvalidCipherParameterException(AFFINE_KEY_2 + INTEGER_MESSAGE); + } + } + + public static void verifyAtbashParams(ObjectNode params){ + verifyUniversalParams(params); + } + + public static void verifyBaconianParams(ObjectNode params){ + if(!params.has(PRESERVE_CAPITALS)){ + throw new InvalidCipherParameterException(PRESERVE_CAPITALS + PRESENT_MESSAGE); + } + if(!params.get(PRESERVE_CAPITALS).isBoolean()){ + throw new InvalidCipherParameterException(PRESERVE_CAPITALS + BOOLEAN_MESSAGE); + } + + if(!params.has(INPUT_STRING)){ + throw new InvalidCipherParameterException(INPUT_STRING + PRESENT_MESSAGE); + } + if(!params.get(INPUT_STRING).isTextual()){ + throw new InvalidCipherParameterException(INPUT_STRING + TEXT_MESSAGE); + } + } + + public static void verifyBaseXParams(ObjectNode params){ + if(!params.has(INPUT_STRING)){ + throw new InvalidCipherParameterException(INPUT_STRING + PRESENT_MESSAGE); + } + if(!params.get(INPUT_STRING).isTextual()){ + throw new InvalidCipherParameterException(INPUT_STRING + TEXT_MESSAGE); + } + + if(!params.has(BASE_X_BASE)){ + throw new InvalidCipherParameterException(BASE_X_BASE + PRESENT_MESSAGE); + } + if(!params.get(BASE_X_BASE).isInt()){ + throw new InvalidCipherParameterException(BASE_X_BASE + INTEGER_MESSAGE); + } + } + + public static void verifySquareKeyword(ObjectNode params){ + verifyUniversalParams(params); + + if(!params.has(SQUARE_KEYWORD)){ + throw new InvalidCipherParameterException(SQUARE_KEYWORD + PRESENT_MESSAGE); + } + if(!params.get(SQUARE_KEYWORD).isTextual()){ + throw new InvalidCipherParameterException(SQUARE_KEYWORD + TEXT_MESSAGE); + } + } + + public static void verifyHillParams(ObjectNode params){ + verifyUniversalParams(params); + + if(!params.has(HILL_KEY)){ + throw new InvalidCipherParameterException(HILL_KEY + PRESENT_MESSAGE); + } + if(!params.get(HILL_KEY).isArray()){ + throw new InvalidCipherParameterException(HILL_KEY + INTEGER_ARRAY_MESSAGE); + } + if(!params.get(HILL_KEY).get(0).isArray()){ + throw new InvalidCipherParameterException(HILL_KEY + INTEGER_ARRAY_MESSAGE); + } + if(!params.get(HILL_KEY).get(0).get(0).isInt()){ + throw new InvalidCipherParameterException(HILL_KEY + INTEGER_ARRAY_MESSAGE); + } + } + + public static void verifyMorseParams(ObjectNode params){ + if(!params.has(INPUT_STRING)){ + throw new InvalidCipherParameterException(INPUT_STRING + PRESENT_MESSAGE); + } + if(!params.get(INPUT_STRING).isTextual()){ + throw new InvalidCipherParameterException(INPUT_STRING + TEXT_MESSAGE); + } + } + + public static void verifyPolybiusParams(ObjectNode params){ + if(!params.has(PRESERVE_WHITESPACE)){ + throw new InvalidCipherParameterException(PRESERVE_WHITESPACE + PRESENT_MESSAGE); + } + if(!params.get(PRESERVE_WHITESPACE).isBoolean()){ + throw new InvalidCipherParameterException(PRESERVE_WHITESPACE + BOOLEAN_MESSAGE); + } + + if(!params.has(PRESERVE_SYMBOLS)){ + throw new InvalidCipherParameterException(PRESERVE_SYMBOLS + PRESENT_MESSAGE); + } + if(!params.get(PRESERVE_SYMBOLS).isBoolean()){ + throw new InvalidCipherParameterException(PRESERVE_SYMBOLS + BOOLEAN_MESSAGE); + } + + if(!params.has(INPUT_STRING)){ + throw new InvalidCipherParameterException(INPUT_STRING + PRESENT_MESSAGE); + } + if(!params.get(INPUT_STRING).isTextual()){ + throw new InvalidCipherParameterException(INPUT_STRING + TEXT_MESSAGE); + } + + if(!params.has(KEYWORD)){ + throw new InvalidCipherParameterException(KEYWORD + PRESENT_MESSAGE); + } + if(!params.get(KEYWORD).isTextual()){ + throw new InvalidCipherParameterException(KEYWORD + TEXT_MESSAGE); + } + } + + public static void verifyRailFenceParams(ObjectNode params){ + verifyUniversalParams(params); + + if(!params.has(RAIL_FENCE_RAILS)){ + throw new InvalidCipherParameterException(RAIL_FENCE_RAILS + PRESENT_MESSAGE); + } + if(!params.get(RAIL_FENCE_RAILS).isInt()){ + throw new InvalidCipherParameterException(RAIL_FENCE_RAILS + INTEGER_MESSAGE); + } + } + + public static void verifyTrifidParams(ObjectNode params){ + verifyParamsWithKeyword(params); + + if(!params.has(TRIFID_FILL)){ + throw new InvalidCipherParameterException(TRIFID_FILL + PRESENT_MESSAGE); + } + if(!params.get(TRIFID_FILL).isTextual()){ + throw new InvalidCipherParameterException(TRIFID_FILL + TEXT_MESSAGE); + } + if(params.get(TRIFID_FILL).asText().length() > 1){ + throw new InvalidCipherParameterException(TRIFID_FILL + CHARACTER_MESSAGE); + } + + if(!params.has(TRIFID_GROUP_LENGTH)){ + throw new InvalidCipherParameterException(TRIFID_GROUP_LENGTH); + } + if(!params.get(TRIFID_GROUP_LENGTH).isInt()){ + throw new InvalidCipherParameterException(TRIFID_GROUP_LENGTH + INTEGER_MESSAGE); + } + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/log4j2-spring.xml b/src/main/resources/log4j2-spring.xml new file mode 100644 index 0000000..3496396 --- /dev/null +++ b/src/main/resources/log4j2-spring.xml @@ -0,0 +1,27 @@ + + + + + + + + + %style{%d{MM-dd-yyyy HH:mm:ss.SSSZ}}{bright, black} %highlight{%5p} %style{function=}{bright, black}%style{%50.50replace{%c{0}.%M}{}{}}{blue, bright} %style{requestId=%36.36X{requestId}}{bright, black} %style{username=%X{username}}{bright, black} %highlight{---} %message%n + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/template.json b/src/main/resources/template.json new file mode 100644 index 0000000..74aea5c --- /dev/null +++ b/src/main/resources/template.json @@ -0,0 +1,37 @@ +{ + "timestamp": { + "$resolver": "timestamp", + "pattern": { + "format": "yyy-MM-dd HH:mm:ss.SSSZ" + } + }, + "level": { + "$resolver": "level", + "field": "name" + }, + "requestId": { + "$resolver": "mdc", + "key": "requestId" + }, + "message": { + "$resolver": "message", + "stringified": true + }, + "exception": { + "exception_class": { + "$resolver": "exception", + "field": "className" + }, + "exception_message": { + "$resolver": "exception", + "field": "message" + }, + "stacktrace": { + "$resolver": "exception", + "field": "stackTrace", + "stackTrace": { + "stringified": true + } + } + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/aspect/CipherStreamLoggingAspectTest.java b/src/test/java/com/mattrixwv/cipherstream/aspect/CipherStreamLoggingAspectTest.java new file mode 100644 index 0000000..aaede93 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/aspect/CipherStreamLoggingAspectTest.java @@ -0,0 +1,50 @@ +package com.mattrixwv.cipherstream.aspect; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class CipherStreamLoggingAspectTest{ + @InjectMocks + private CipherStreamLoggingAspect aspect; + + + @Test + public void testMappedFunction(){ + aspect.mappedFunction(); + + + assertNotNull(aspect); + } + + @Test + public void testCipherMethod(){ + aspect.cipherMethod(); + + + assertNotNull(aspect); + } + + @Test + public void testGetCipherInfo(){ + ObjectNode jsonNode = new ObjectMapper().createObjectNode(); + jsonNode.put("something", "Something Important"); + + + aspect.getCipherInfo(null); + + + assertNotNull(aspect); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/config/FullFilterIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/config/FullFilterIntegrationTest.java new file mode 100644 index 0000000..d4f1a65 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/config/FullFilterIntegrationTest.java @@ -0,0 +1,76 @@ +package com.mattrixwv.cipherstream.config; + + +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import java.util.StringJoiner; +import java.util.UUID; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.slf4j.Logger; +import org.slf4j.spi.MDCAdapter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.monosubstitution.CaesarCipherController; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + + +@Tag("integration-test") +@WebMvcTest(controllers = CaesarCipherController.class) +public class FullFilterIntegrationTest{ + //HTTP + @Autowired + private MockMvc mockMvc; + @Mock + private HttpServletRequest request; + @Mock + private HttpServletResponse response; + //Logging + @Mock(name = "com.mattrixwv.cipherstream.config.FullFilter") + private Logger logger; + @Mock + private MDCAdapter mdc; + //Fields + private UUID requestId = UUID.randomUUID(); + private String ipAddresses = "192.168.1.1,192.168.1.2,192.168.1.3"; + + + @Test + public void testDoFilterInternal() throws Exception{ + mockMvc.perform(get("/cipherStream") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", ipAddresses) + .param("param1", "value1") + .param("param2", "value2.1") + .param("param2", "value2.2") + .param("_", "value3")) + .andExpect(status().isOk()); + + verify(logger, times(1)).info(eq("Request parameters: {}"), any(StringJoiner.class)); + verify(mdc, times(1)).put("requestId", requestId.toString()); + verify(mdc, times(1)).put("ip", "192.168.1.1"); + verify(mdc, times(1)).clear(); + } + + @Test + public void testDoFilterInternal_NoParameters() throws Exception{ + mockMvc.perform(get("/cipherStream") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", ipAddresses)) + .andExpect(status().isOk()); + + verify(logger, never()).info(anyString(), any(Object.class)); + verify(mdc, times(1)).put("requestId", requestId.toString()); + verify(mdc, times(1)).put("ip", "192.168.1.1"); + verify(mdc, times(1)).clear(); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/config/FullFilterTest.java b/src/test/java/com/mattrixwv/cipherstream/config/FullFilterTest.java new file mode 100644 index 0000000..237c789 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/config/FullFilterTest.java @@ -0,0 +1,70 @@ +package com.mattrixwv.cipherstream.config; + + +import static org.mockito.Mockito.*; + +import java.util.HashMap; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class FullFilterTest{ + @InjectMocks + private FullFilter fullFilter; + @Mock + private HttpServletRequest request; + @Mock + private HttpServletResponse response; + @Mock + private FilterChain filterChain; + + + @Test + public void testDoFilterInternal() throws Exception{ + HashMap parameterMap = new HashMap<>(); + parameterMap.put("key1", new String[]{"value1"}); + parameterMap.put("key2", new String[]{"value2.1", "value2.2"}); + parameterMap.put("_", new String[]{"value3"}); + + + doReturn("X-Request-Id").when(request).getHeader("X-Request-Id"); + doReturn("192.168.1.1,192.168.1.2,192.168.1.3").when(request).getHeader("X-Forwarded-For"); + doReturn(parameterMap).when(request).getParameterMap(); + doReturn("/testURL").when(request).getRequestURI(); + + + fullFilter.doFilterInternal(request, response, filterChain); + + + verify(request, times(2)).getHeader("X-Request-Id"); + verify(request, times(2)).getHeader("X-Forwarded-For"); + verify(filterChain, times(1)).doFilter(request, response); + } + + @Test + public void testDoFilterInternal_NoParameters() throws Exception{ + doReturn("X-Request-Id").when(request).getHeader("X-Request-Id"); + doReturn("192.168.1.1,192.168.1.2,192.168.1.3").when(request).getHeader("X-Forwarded-For"); + doReturn(new HashMap<>()).when(request).getParameterMap(); + doReturn("/testURL").when(request).getRequestURI(); + + + fullFilter.doFilterInternal(request, response, filterChain); + + + verify(request, times(2)).getHeader("X-Request-Id"); + verify(request, times(2)).getHeader("X-Forwarded-For"); + verify(filterChain, times(1)).doFilter(request, response); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/CipherStreamControllerIntegrationTestBase.java b/src/test/java/com/mattrixwv/cipherstream/controller/CipherStreamControllerIntegrationTestBase.java new file mode 100644 index 0000000..d8dab01 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/CipherStreamControllerIntegrationTestBase.java @@ -0,0 +1,103 @@ +package com.mattrixwv.cipherstream.controller; + + +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +import java.util.UUID; + +import org.junit.jupiter.api.Tag; +import org.mockito.Mock; +import org.slf4j.Logger; +import org.slf4j.spi.MDCAdapter; +import org.springframework.boot.autoconfigure.aop.AopAutoConfiguration; +import org.springframework.context.annotation.Import; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; +import com.mattrixwv.cipherstream.config.FullFilter; + + +@Tag("integration-test") +@Import({AopAutoConfiguration.class, FullFilter.class, CipherStreamLoggingAspect.class}) +public class CipherStreamControllerIntegrationTestBase{ + protected static final ObjectMapper mapper = new ObjectMapper(); + //Objects + protected ObjectNode decodedNode; + protected ObjectNode encodedNode; + //Fields + protected static final String requestId = UUID.randomUUID().toString(); + protected static final String ipAddress = "192.168.1.1"; + + //MDC + @Mock + protected MDCAdapter mdc; + + //Base + @Mock(name = "com.mattrixwv.cipherstream.controller.CipherStreamController") + protected Logger baseLogger; + + //Combination + @Mock(name = "com.mattrixwv.cipherstream.controller.combination.AdfgvxCipherController") + protected Logger adfgvxLogger; + @Mock(name = "com.mattrixwv.cipherstream.controller.combination.AdfgxCipherController") + protected Logger adfgxLogger; + + //Monosubstitution + @Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.AffineCipherController") + protected Logger affineLogger; + @Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.AtbashCipherController") + protected Logger atbashLogger; + @Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.AutokeyCipherController") + protected Logger autokeyLogger; + @Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.BaconianCipherController") + protected Logger baconianLogger; + @Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.BaseXCipherController") + protected Logger baseXLogger; + @Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.BeaufortCipherController") + protected Logger beaufortLogger; + @Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.CaesarCipherController") + protected Logger caesarLogger; + @Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.OneTimePadCipherController") + protected Logger oneTimePadLogger; + @Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.PortaCipherController") + protected Logger portaLogger; + @Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.SubstitutionCipherController") + protected Logger substitutionLogger; + @Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.VigenereCipherController") + protected Logger vigenereLogger; + + //Polysubstitution + @Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.BifidCipherController") + protected Logger bifidLogger; + @Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.ColumnarCipherController") + protected Logger columnarLogger; + @Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.HillCipherController") + protected Logger hillLogger; + @Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.MorseCodeController") + protected Logger morseLogger; + @Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.PlayfairCipherController") + protected Logger playfiarLogger; + @Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.PolybiusSquareController") + protected Logger polybiusLogger; + @Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.RailFenceController") + protected Logger railFenceLogger; + @Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.TrifidCipherController") + protected Logger trifidLogger; + + //Misc + @Mock(name = "com.mattrixwv.cipherstream.config.FullFilter") + protected Logger filterLogger; + @Mock(name = "com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect") + protected Logger aspectLogger; + + + protected void verifyFilter(String url){ + verify(filterLogger, never()).info(eq("Request parameters: {}"), any(StringBuilder.class)); + verify(mdc, times(1)).put(eq("requestId"), any()); + verify(mdc, times(1)).put("ip", ipAddress); + verify(mdc, times(1)).put("url", url); + verify(mdc, times(1)).clear(); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/ExceptionRestControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/ExceptionRestControllerTest.java new file mode 100644 index 0000000..e95562e --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/ExceptionRestControllerTest.java @@ -0,0 +1,60 @@ +package com.mattrixwv.cipherstream.controller; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class ExceptionRestControllerTest{ + private static final ObjectMapper mapper = new ObjectMapper(); + @InjectMocks + private ExceptionRestController controller; + //Fields + private static final String errorMessage = "Error message"; + + + @Test + public void testInvalidCipherParameterHandler(){ + //Setup variables + InvalidCipherParameterException error = new InvalidCipherParameterException(errorMessage); + ObjectNode expectedJson = mapper.createObjectNode(); + expectedJson.put("message", errorMessage); + + + //Run the function + ObjectNode returnJson = controller.invalidCipherParameterHandler(error); + + + //Verify the data + assertEquals(expectedJson, returnJson); + } + + + @Test + public void testGenericExceptionHandler(){ + //Setup variables + Exception error = new Exception(errorMessage); + ObjectNode expectedJson = mapper.createObjectNode(); + expectedJson.put("message", errorMessage); + + + //Run the function + JsonNode returnJson = controller.genericExceptionHandler(error); + + + //Verify the data + assertEquals(expectedJson, returnJson); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/combination/AdfgvxCipherControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/combination/AdfgvxCipherControllerIntegrationTest.java new file mode 100644 index 0000000..b8da1f1 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/combination/AdfgvxCipherControllerIntegrationTest.java @@ -0,0 +1,91 @@ +package com.mattrixwv.cipherstream.controller.combination; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = AdfgvxCipherController.class) +public class AdfgvxCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/adfgvx"; + private String decodedString = "Message to^encode"; + private String encodedString = "AXgvdavfxgagfa afag^aaxdxfgdagda"; + private String keyword = "keyword"; + private String squareKeyword = "SquareKeyword"; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.put(CipherParameterUtil.KEYWORD, keyword); + decodedNode.put(CipherParameterUtil.SQUARE_KEYWORD, squareKeyword); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.put(CipherParameterUtil.KEYWORD, keyword); + encodedNode.put(CipherParameterUtil.SQUARE_KEYWORD, squareKeyword); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString); + } + + @Test + public void testEncodeAdfgvx() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(adfgvxLogger, times(1)).info("Encoding ADFGVX"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodeAdfgvx() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString)); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(adfgvxLogger, times(1)).info("Decoding ADFGVX"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/combination/AdfgvxCipherControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/combination/AdfgvxCipherControllerTest.java new file mode 100644 index 0000000..0390ee8 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/combination/AdfgvxCipherControllerTest.java @@ -0,0 +1,76 @@ +package com.mattrixwv.cipherstream.controller.combination; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class AdfgvxCipherControllerTest{ + @InjectMocks + private AdfgvxCipherController adfgvxCipherController; + private static final String KEYWORD = CipherParameterUtil.KEYWORD; + private static final String SQUARE_KEYWORD = CipherParameterUtil.SQUARE_KEYWORD; + private static final String INPUT_STRING = "Message to-encode"; + private static final String OUTPUT_STRING = "AXgvdavfxgagfa afag-aaxdxfgdagda"; + private ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void testEncodeAdfgvx(){ + ObjectNode cipherParams = generateParams(KEYWORD, SQUARE_KEYWORD, INPUT_STRING); + + + ObjectNode returnedJson = adfgvxCipherController.encodeAdfgvx(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + adfgvxCipherController.encodeAdfgvx(blankNode); + }); + } + + @Test + public void testDecodeAdfgvx(){ + ObjectNode cipherParams = generateParams(KEYWORD, SQUARE_KEYWORD, OUTPUT_STRING); + + + ObjectNode returnedJson = adfgvxCipherController.decodeAdfgvx(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + adfgvxCipherController.decodeAdfgvx(blankNode); + }); + } + + private ObjectNode generateParams(String keyword, String squareKeyword, String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + cipherParams.put(CipherParameterUtil.KEYWORD, keyword); + cipherParams.put(CipherParameterUtil.SQUARE_KEYWORD, squareKeyword); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/combination/AdfgxCipherControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/combination/AdfgxCipherControllerIntegrationTest.java new file mode 100644 index 0000000..9756414 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/combination/AdfgxCipherControllerIntegrationTest.java @@ -0,0 +1,92 @@ +package com.mattrixwv.cipherstream.controller.combination; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = AdfgxCipherController.class) +public class AdfgxCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/adfgx"; + private String decodedString = "Message to^encode"; + private String encodedString = "AAgagadfagaxxd axdx^adafafxddgdf"; + private String keyword = "keyword"; + private String squareKeyword = "SquareKeyword"; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.put(CipherParameterUtil.KEYWORD, keyword); + decodedNode.put(CipherParameterUtil.SQUARE_KEYWORD, squareKeyword); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.put(CipherParameterUtil.KEYWORD, keyword); + encodedNode.put(CipherParameterUtil.SQUARE_KEYWORD, squareKeyword); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString); + } + + + @Test + public void testEncodeAdfgx() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(adfgxLogger, times(1)).info("Encoding ADFGX"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodeAdfgx() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString)); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(adfgxLogger, times(1)).info("Decoding ADFGX"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/combination/AdfgxCipherControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/combination/AdfgxCipherControllerTest.java new file mode 100644 index 0000000..2835f53 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/combination/AdfgxCipherControllerTest.java @@ -0,0 +1,75 @@ +package com.mattrixwv.cipherstream.controller.combination; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class AdfgxCipherControllerTest{ + @InjectMocks + private AdfgxCipherController adfgxCipherController; + private static final String ADFGX_KEYWORD = CipherParameterUtil.KEYWORD; + private static final String ADFGX_SQUARE_KEYWORD = CipherParameterUtil.SQUARE_KEYWORD; + private static final String ADFGX_INPUT_STRING = "Message to^encode"; + private static final String ADFGX_OUTPUT_STRING = "AAgagadfagaxxd axdx^adafafxddgdf"; + private ObjectMapper objectMapper = new ObjectMapper(); + + @Test + public void testEncodeAdfgx(){ + ObjectNode cipherParams = generateParams(ADFGX_KEYWORD, ADFGX_SQUARE_KEYWORD, ADFGX_INPUT_STRING); + + + ObjectNode returnedJson = adfgxCipherController.encodeAdfgx(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(ADFGX_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + adfgxCipherController.encodeAdfgx(blankNode); + }); + } + + @Test + public void testDecodeAdfgx(){ + ObjectNode cipherParams = generateParams(ADFGX_KEYWORD, ADFGX_SQUARE_KEYWORD, ADFGX_OUTPUT_STRING); + + + ObjectNode returnedJson = adfgxCipherController.decodeAdfgx(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(ADFGX_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + adfgxCipherController.decodeAdfgx(blankNode); + }); + } + + private ObjectNode generateParams(String keyword, String squareKeyword, String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + cipherParams.put(CipherParameterUtil.KEYWORD, keyword); + cipherParams.put(CipherParameterUtil.SQUARE_KEYWORD, squareKeyword); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AffineCipherControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AffineCipherControllerIntegrationTest.java new file mode 100644 index 0000000..c6969d7 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AffineCipherControllerIntegrationTest.java @@ -0,0 +1,91 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = AffineCipherController.class) +public class AffineCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/cipherStream/affine"; + private String decodedString = "Message to^encode"; + private String encodedString = "Pbtthlb yz^burzwb"; + private int key1 = 5; + private int key2 = 7; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.put(CipherParameterUtil.AFFINE_KEY_1, key1); + decodedNode.put(CipherParameterUtil.AFFINE_KEY_2, key2); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.put(CipherParameterUtil.AFFINE_KEY_1, key1); + encodedNode.put(CipherParameterUtil.AFFINE_KEY_2, key2); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString); + } + + @Test + public void testEncodeAffine() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(affineLogger, times(1)).info("Encoding Affine"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodeAffine() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString)); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(affineLogger, times(1)).info("Decoding Affine"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AffineCipherControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AffineCipherControllerTest.java new file mode 100644 index 0000000..f9592d9 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AffineCipherControllerTest.java @@ -0,0 +1,76 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class AffineCipherControllerTest{ + @InjectMocks + private AffineCipherController affineCipherController; + private static final String AFFINE_INPUT_STRING = "Message to^encode"; + private static final String AFFINE_OUTPUT_STRING = "Pbtthlb yz^burzwb"; + private static final int AFFINE_KEY_1 = 5; + private static final int AFFINE_KEY_2 = 7; + private ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void testEncodeAffine(){ + ObjectNode cipherParams = generateParams(AFFINE_KEY_1, AFFINE_KEY_2, AFFINE_INPUT_STRING); + + + ObjectNode returnedJson = affineCipherController.encodeAffine(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(AFFINE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + affineCipherController.encodeAffine(blankNode); + }); + } + + @Test + public void testDecodeAffine(){ + ObjectNode cipherParams = generateParams(AFFINE_KEY_1, AFFINE_KEY_2, AFFINE_OUTPUT_STRING); + + + ObjectNode returnedJson = affineCipherController.decodeAffine(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(AFFINE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + affineCipherController.decodeAffine(blankNode); + }); + } + + private ObjectNode generateParams(int key1, int key2, String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + cipherParams.put(CipherParameterUtil.AFFINE_KEY_1, key1); + cipherParams.put(CipherParameterUtil.AFFINE_KEY_2, key2); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AtbashCipherControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AtbashCipherControllerIntegrationTest.java new file mode 100644 index 0000000..4c42025 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AtbashCipherControllerIntegrationTest.java @@ -0,0 +1,86 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = AtbashCipherController.class) +public class AtbashCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/cipherStream/atbash"; + private String decodedString = "Message to^encode"; + private String encodedString = "Nvhhztv gl^vmxlwv"; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString); + } + + + @Test + public void testEncodeAtbash() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(atbashLogger, times(1)).info("Encoding Atbash"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodeAtbash() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString)); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(atbashLogger, times(1)).info("Decoding Atbash"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AtbashCipherControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AtbashCipherControllerTest.java new file mode 100644 index 0000000..76a42f5 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AtbashCipherControllerTest.java @@ -0,0 +1,68 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class AtbashCipherControllerTest{ + @InjectMocks + private AtbashCipherController atbashCipherController; + private static final String ATBASH_INPUT_STRING = "Message to^encode"; + private static final String ATBASH_OUTPUT_STRING = "Nvhhztv gl^vmxlwv"; + private ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void testEncodeAtbash(){ + ObjectNode cipherParams = generateParams(ATBASH_INPUT_STRING); + + ObjectNode returnedJson = atbashCipherController.encodeAtbash(cipherParams); + + assertEquals(cipherParams, returnedJson); + assertEquals(ATBASH_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + atbashCipherController.encodeAtbash(blankNode); + }); + } + + @Test + public void testDecodeAtbash(){ + ObjectNode cipherParams = generateParams(ATBASH_OUTPUT_STRING); + + ObjectNode returnedJson = atbashCipherController.decodeAtbash(cipherParams); + + assertEquals(cipherParams, returnedJson); + assertEquals(ATBASH_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + atbashCipherController.decodeAtbash(blankNode); + }); + } + + private ObjectNode generateParams(String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AutokeyCipherControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AutokeyCipherControllerIntegrationTest.java new file mode 100644 index 0000000..97a6a92 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AutokeyCipherControllerIntegrationTest.java @@ -0,0 +1,89 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = AutokeyCipherController.class) +public class AutokeyCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/cipherStream/autokey"; + private String decodedString = "Message to^encode"; + private String encodedString = "Wiqooxh fs^wfcuhx"; + private String keyword = "keyword"; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.put(CipherParameterUtil.KEYWORD, keyword); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.put(CipherParameterUtil.KEYWORD, keyword); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString); + } + + @Test + public void testEncodeAutokey() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(autokeyLogger, times(1)).info("Encoding Autokey"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodeAutokey() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString)); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(autokeyLogger, times(1)).info("Decoding Autokey"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AutokeyCipherControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AutokeyCipherControllerTest.java new file mode 100644 index 0000000..121c94d --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/AutokeyCipherControllerTest.java @@ -0,0 +1,74 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class AutokeyCipherControllerTest{ + @InjectMocks + private AutokeyCipherController autokeyCipherController; + private static final String AUTOKEY_INPUT_STRING = "Message to^encode"; + private static final String AUTOKEY_OUTPUT_STRING = "Wiqooxh fs^wfcuhx"; + private static final String AUTOKEY_KEYWORD = CipherParameterUtil.KEYWORD; + private ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void testEncodeAutokey(){ + ObjectNode cipherParams = generateParams(AUTOKEY_KEYWORD, AUTOKEY_INPUT_STRING); + + + ObjectNode returnedJson = autokeyCipherController.encodeAutokey(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(AUTOKEY_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + autokeyCipherController.encodeAutokey(blankNode); + }); + } + + @Test + public void testDecodeAutokey(){ + ObjectNode cipherParams = generateParams(AUTOKEY_KEYWORD, AUTOKEY_OUTPUT_STRING); + + + ObjectNode returnedJson = autokeyCipherController.decodeAutokey(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(AUTOKEY_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + autokeyCipherController.decodeAutokey(blankNode); + }); + } + + private ObjectNode generateParams(String keyword, String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + cipherParams.put(CipherParameterUtil.KEYWORD, keyword); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/BaconianCipherControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/BaconianCipherControllerIntegrationTest.java new file mode 100644 index 0000000..b1508d1 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/BaconianCipherControllerIntegrationTest.java @@ -0,0 +1,85 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = BaconianCipherController.class) +public class BaconianCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/cipherStream/baconian"; + private String decodedString = "Message to^encode"; + private String encodedString = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa"; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString.replaceAll("[^a-zA-Z]", "")); + } + + @Test + public void testEncodeBaconian() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(baconianLogger, times(1)).info("Encoding Baconian"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodeBaconian() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString.replaceAll("[^a-zA-Z]", ""))); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(baconianLogger, times(1)).info("Decoding Baconian"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/BaconianCipherControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/BaconianCipherControllerTest.java new file mode 100644 index 0000000..296e188 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/BaconianCipherControllerTest.java @@ -0,0 +1,71 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class BaconianCipherControllerTest{ + @InjectMocks + private BaconianCipherController baconianCipherController; + private static final String BACONIAN_INPUT_STRING = "Message to-encode"; + private static final String BACONIAN_OUTPUT_STRING = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa"; + private static final String BACONIAN_DECODED_STRING = "Messagetoencode"; + private ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void testEncodeBaconian(){ + ObjectNode cipherParams = generateParams(BACONIAN_INPUT_STRING); + + + ObjectNode returnedJson = baconianCipherController.encodeBaconian(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(BACONIAN_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + baconianCipherController.encodeBaconian(blankNode); + }); + } + + @Test + public void testDecodeBaconian(){ + ObjectNode cipherParams = generateParams(BACONIAN_OUTPUT_STRING); + + + ObjectNode returnedJson = baconianCipherController.decodeBaconian(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(BACONIAN_DECODED_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + baconianCipherController.decodeBaconian(blankNode); + }); + } + + private ObjectNode generateParams(String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/BaseXCipherControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/BaseXCipherControllerIntegrationTest.java new file mode 100644 index 0000000..399bfe0 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/BaseXCipherControllerIntegrationTest.java @@ -0,0 +1,89 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = BaseXCipherController.class) +public class BaseXCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/cipherStream/basex"; + private String decodedString = "A+B@C d\te\nf"; + private String encodedString = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110"; + private int base = 2; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.put(CipherParameterUtil.BASE_X_BASE, base); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.put(CipherParameterUtil.BASE_X_BASE, base); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString); + } + + + @Test + public void testEncodeBaseXEncode() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(baseXLogger, times(1)).info("Encoding BaseX"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodeBaseXDecode() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString)); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(baseXLogger, times(1)).info("Decoding BaseX"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/BaseXCipherControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/BaseXCipherControllerTest.java new file mode 100644 index 0000000..dd8e906 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/BaseXCipherControllerTest.java @@ -0,0 +1,71 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class BaseXCipherControllerTest{ + @InjectMocks + private BaseXCipherController baseXCipherController; + private static final int BASE_X_BASE = 2; + private static final String BASE_X_INPUT_STRING = "A+B@C d\te\nf"; + private static final String BASE_X_OUTPUT_STRING = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110"; + private ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void testEncodeBaseX(){ + ObjectNode cipherParams = generateParams(BASE_X_BASE, BASE_X_INPUT_STRING); + + + ObjectNode returnedJson = baseXCipherController.encodeBaseX(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(BASE_X_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + baseXCipherController.encodeBaseX(blankNode); + }); + } + + @Test + public void testDecodeBaseX(){ + ObjectNode cipherParams = generateParams(BASE_X_BASE, BASE_X_OUTPUT_STRING); + + + ObjectNode returnedJson = baseXCipherController.decodeBaseX(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(BASE_X_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + baseXCipherController.decodeBaseX(blankNode); + }); + } + + private ObjectNode generateParams(int base, String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + cipherParams.put(CipherParameterUtil.BASE_X_BASE, base); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/BeaufortCipherControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/BeaufortCipherControllerIntegrationTest.java new file mode 100644 index 0000000..e3fc68b --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/BeaufortCipherControllerIntegrationTest.java @@ -0,0 +1,88 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = BeaufortCipherController.class) +public class BeaufortCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/cipherStream/beaufort"; + private String decodedString = "Message to^encode"; + private String encodedString = "Yageolz rq^ujmdag"; + private String keyword = "Ke*y word"; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.put(CipherParameterUtil.KEYWORD, keyword); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.put(CipherParameterUtil.KEYWORD, keyword); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString); + } + + @Test + public void testEncodeBeaufort() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(beaufortLogger, times(1)).info("Encoding Beaufort"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodeBeaufort() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString)); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(beaufortLogger, times(1)).info("Decoding Beaufort"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/BeaufortCipherControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/BeaufortCipherControllerTest.java new file mode 100644 index 0000000..c1454fe --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/BeaufortCipherControllerTest.java @@ -0,0 +1,74 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class BeaufortCipherControllerTest{ + @InjectMocks + private BeaufortCipherController beaufortCipherController; + private static final String BEAUFORT_KEYWORD = CipherParameterUtil.KEYWORD; + private static final String BEAUFORT_INPUT_STRING = "Message to^encode"; + private static final String BEAUFORT_OUTPUT_STRING = "Yageolz rq^ujmdag"; + private ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void encodeBeaufort(){ + ObjectNode cipherParams = generateParams(BEAUFORT_KEYWORD, BEAUFORT_INPUT_STRING); + + + ObjectNode returnedJson = beaufortCipherController.encodeBeaufort(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(BEAUFORT_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + beaufortCipherController.encodeBeaufort(blankNode); + }); + } + + @Test + public void decodeBeaufort(){ + ObjectNode cipherParams = generateParams(BEAUFORT_KEYWORD, BEAUFORT_OUTPUT_STRING); + + + ObjectNode returnedJson = beaufortCipherController.decodeBeaufort(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(BEAUFORT_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + beaufortCipherController.decodeBeaufort(blankNode); + }); + } + + private ObjectNode generateParams(String keyword, String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + cipherParams.put(CipherParameterUtil.KEYWORD, keyword); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/CaesarCipherControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/CaesarCipherControllerIntegrationTest.java new file mode 100644 index 0000000..e4c6fa5 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/CaesarCipherControllerIntegrationTest.java @@ -0,0 +1,89 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = CaesarCipherController.class) +public class CaesarCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/cipherStream/caesar"; + private String decodedString = "The quick brown fox jumps over - the lazy dog"; + private String encodedString = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; + private int shiftAmount = 23; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.put(CipherParameterUtil.CAESAR_SHIFT_AMOUNT, shiftAmount); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.put(CipherParameterUtil.CAESAR_SHIFT_AMOUNT, shiftAmount); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString); + } + + + @Test + public void testEncodeCaesar() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(caesarLogger, times(1)).info("Encoding Caesar"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodeCaesar() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString)); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(caesarLogger, times(1)).info("Decoding Caesar"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/CaesarCipherControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/CaesarCipherControllerTest.java new file mode 100644 index 0000000..cf38faf --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/CaesarCipherControllerTest.java @@ -0,0 +1,74 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class CaesarCipherControllerTest{ + @InjectMocks + private CaesarCipherController caesarCipherController; + private static final String CAESAR_INPUT_STRING = "The quick brown fox jumps over - the lazy dog"; + private static final String CAESAR_OUTPUT_STRING = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; + private static final int CAESAR_SHIFT_AMOUNT = 23; + private ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void testEncodeCaesar(){ + ObjectNode cipherParams = generateParams(CAESAR_SHIFT_AMOUNT, CAESAR_INPUT_STRING); + + + ObjectNode returnedJson = caesarCipherController.encodeCaesar(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(CAESAR_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankParams = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + caesarCipherController.encodeCaesar(blankParams); + }); + } + + @Test + public void testDecodeCaesar(){ + ObjectNode cipherParams = generateParams(CAESAR_SHIFT_AMOUNT, CAESAR_OUTPUT_STRING); + + + ObjectNode returnedJson = caesarCipherController.decodeCaesar(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(CAESAR_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankParams = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + caesarCipherController.decodeCaesar(blankParams); + }); + } + + private ObjectNode generateParams(int shiftAmount, String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + cipherParams.put(CipherParameterUtil.CAESAR_SHIFT_AMOUNT, shiftAmount); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/OneTimePadCipherControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/OneTimePadCipherControllerIntegrationTest.java new file mode 100644 index 0000000..6e9d330 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/OneTimePadCipherControllerIntegrationTest.java @@ -0,0 +1,89 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = OneTimePadCipherController.class) +public class OneTimePadCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/cipherStream/oneTimePad"; + private String decodedString = "Message to^encode"; + private String encodedString = "Wiqooxh mv^egkgws"; + private String keyword = "keywordThatIsTotallyRandom"; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.put(CipherParameterUtil.KEYWORD, keyword); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.put(CipherParameterUtil.KEYWORD, keyword); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString); + } + + + @Test + public void testEncodeOneTimePad() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(oneTimePadLogger, times(1)).info("Encoding One Time Pad"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodeOneTimePad() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString)); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(oneTimePadLogger, times(1)).info("Decoding One Time Pad"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/OneTimePadCipherControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/OneTimePadCipherControllerTest.java new file mode 100644 index 0000000..d8f3cca --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/OneTimePadCipherControllerTest.java @@ -0,0 +1,73 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class OneTimePadCipherControllerTest{ + @InjectMocks + private OneTimePadCipherController oneTimePadCipherController; + private static final String ONE_TIME_PAD_KEYWORD = "keywordThatIsTotallyRandom"; + private static final String ONE_TIME_PAD_INPUT_STRING = "Message to^encode"; + private static final String ONE_TIME_PAD_OUTPUT_STRING = "Wiqooxh mv^egkgws"; + private ObjectMapper objectMapper = new ObjectMapper(); + + @Test + public void testEncodeOneTimePad(){ + ObjectNode cipherParams = generateParams(ONE_TIME_PAD_KEYWORD, ONE_TIME_PAD_INPUT_STRING); + + + ObjectNode returnedJson = oneTimePadCipherController.encodeOneTimePad(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(ONE_TIME_PAD_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + oneTimePadCipherController.encodeOneTimePad(blankNode); + }); + } + + @Test + public void testDecodeOneTimePad(){ + ObjectNode cipherParams = generateParams(ONE_TIME_PAD_KEYWORD, ONE_TIME_PAD_OUTPUT_STRING); + + + ObjectNode returnedJson = oneTimePadCipherController.decodeOneTimePad(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(ONE_TIME_PAD_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + oneTimePadCipherController.decodeOneTimePad(blankNode); + }); + } + + private ObjectNode generateParams(String keyword, String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + cipherParams.put(CipherParameterUtil.KEYWORD, keyword); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/PortaCipherControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/PortaCipherControllerIntegrationTest.java new file mode 100644 index 0000000..aadb5d8 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/PortaCipherControllerIntegrationTest.java @@ -0,0 +1,98 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = PortaCipherController.class) +public class PortaCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/cipherStream/porta"; + private String decodedString = "Message to^encode"; + private String encodedString = "Rtghuos bm^qcwgrw"; + private String keyword = "keyword"; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.put(CipherParameterUtil.KEYWORD, keyword); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.put(CipherParameterUtil.KEYWORD, keyword); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString); + } + + + @Test + public void testEncodePorta() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + verify(filterLogger, never()).info(eq("Request parameters: {}"), anyString()); + verify(mdc, times(1)).put("requestId", requestId); + verify(mdc, times(1)).put("ip", "192.168.1.1"); + verify(mdc, times(1)).put("url", url + "/encode"); + verify(mdc, times(1)).clear(); + //Controller + verify(portaLogger, times(1)).info("Encoding Porta"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodePorta() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString)); + + //Filter + verify(filterLogger, never()).info(eq("Request parameters: {}"), anyString()); + verify(mdc, times(1)).put("requestId", requestId); + verify(mdc, times(1)).put("ip", "192.168.1.1"); + verify(mdc, times(1)).put("url", url + "/decode"); + verify(mdc, times(1)).clear(); + //Controller + verify(portaLogger, times(1)).info("Decoding Porta"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/PortaCipherControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/PortaCipherControllerTest.java new file mode 100644 index 0000000..348b24c --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/PortaCipherControllerTest.java @@ -0,0 +1,74 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class PortaCipherControllerTest{ + @InjectMocks + private PortaCipherController portaCipherController; + private static final String PORTA_KEYWORD = CipherParameterUtil.KEYWORD; + private static final String PORTA_INPUT_STRING = "Message to^encode"; + private static final String PORTA_OUTPUT_STRING = "Rtghuos bm^qcwgrw"; + private ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void testEncodePorta(){ + ObjectNode cipherParams = generateParams(PORTA_KEYWORD, PORTA_INPUT_STRING); + + + ObjectNode returnedJson = portaCipherController.encodePorta(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(PORTA_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankParams = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + portaCipherController.encodePorta(blankParams); + }); + } + + @Test + public void testDecodePorta(){ + ObjectNode cipherParams = generateParams(PORTA_KEYWORD, PORTA_OUTPUT_STRING); + + + ObjectNode returnedJson = portaCipherController.decodePorta(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(PORTA_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankParams = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + portaCipherController.decodePorta(blankParams); + }); + } + + private ObjectNode generateParams(String keyword, String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + cipherParams.put(CipherParameterUtil.KEYWORD, keyword); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/SubstitutionCipherControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/SubstitutionCipherControllerIntegrationTest.java new file mode 100644 index 0000000..76ba260 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/SubstitutionCipherControllerIntegrationTest.java @@ -0,0 +1,89 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = SubstitutionCipherController.class) +public class SubstitutionCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/cipherStream/substitution"; + private String decodedString = "Message to^encode"; + private String encodedString = "Oguucig vq^gpeqfg"; + private String keyword = "cdefghijklmnopqrstuvwxyzab"; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.put(CipherParameterUtil.KEYWORD, keyword); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.put(CipherParameterUtil.KEYWORD, keyword); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString); + } + + + @Test + public void testEncodeSubstitution() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(substitutionLogger, times(1)).info("Encoding Substitution"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodeSubstitution() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString)); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(substitutionLogger, times(1)).info("Decoding Substitution"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/SubstitutionCipherControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/SubstitutionCipherControllerTest.java new file mode 100644 index 0000000..eed2921 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/SubstitutionCipherControllerTest.java @@ -0,0 +1,74 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class SubstitutionCipherControllerTest{ + @InjectMocks + private SubstitutionCipherController substitutionCipherController; + private static final String SUBSTITUTION_KEYWORD = "cdefghijklmnopqrstuvwxyzab9876543210"; + private static final String SUBSTITUTION_INPUT_STRING = "Message to&encode 123"; + private static final String SUBSTITUTION_OUTPUT_STRING = "Oguucig vq&gpeqfg 876"; + private ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void testEncodeSubstitution(){ + ObjectNode cipherParams = generateParams(SUBSTITUTION_KEYWORD, SUBSTITUTION_INPUT_STRING); + + + ObjectNode returnedJson = substitutionCipherController.encodeSubstitution(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(SUBSTITUTION_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankParams = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + substitutionCipherController.encodeSubstitution(blankParams); + }); + } + + @Test + public void testDecodeSubstitution(){ + ObjectNode cipherParams = generateParams(SUBSTITUTION_KEYWORD, SUBSTITUTION_OUTPUT_STRING); + + + ObjectNode returnedJson = substitutionCipherController.decodeSubstitution(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(SUBSTITUTION_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankParams = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + substitutionCipherController.decodeSubstitution(blankParams); + }); + } + + private ObjectNode generateParams(String keyword, String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + cipherParams.put(CipherParameterUtil.KEYWORD, keyword); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/VigenereCipherControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/VigenereCipherControllerIntegrationTest.java new file mode 100644 index 0000000..62ba6b4 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/VigenereCipherControllerIntegrationTest.java @@ -0,0 +1,89 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = VigenereCipherController.class) +public class VigenereCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/cipherStream/vigenere"; + private String decodedString = "Message to^encode"; + private String encodedString = "Wiqooxh ds^cjqfgo"; + private String keyword = "keyword"; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.put(CipherParameterUtil.KEYWORD, keyword); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.put(CipherParameterUtil.KEYWORD, keyword); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString); + } + + + @Test + public void testEncodeVigenere() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(vigenereLogger, times(1)).info("Encoding Vigenere"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodeVigenere() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString)); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(vigenereLogger, times(1)).info("Decoding Vigenere"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/VigenereCipherControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/VigenereCipherControllerTest.java new file mode 100644 index 0000000..1d36f55 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/monosubstitution/VigenereCipherControllerTest.java @@ -0,0 +1,74 @@ +package com.mattrixwv.cipherstream.controller.monosubstitution; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class VigenereCipherControllerTest{ + @InjectMocks + private VigenereCipherController vigenereCipherController; + private static final String VIGENERE_KEYWORD = CipherParameterUtil.KEYWORD; + private static final String VIGENERE_INPUT_STRING = "Message to^encode"; + private static final String VIGENERE_OUTPUT_STRING = "Wiqooxh ds^cjqfgo"; + private ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void testEncodeVigenere(){ + ObjectNode cipherParams = generateParams(VIGENERE_KEYWORD, VIGENERE_INPUT_STRING); + + + ObjectNode returnedJson = vigenereCipherController.encodeVigenere(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(VIGENERE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankParams = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + vigenereCipherController.encodeVigenere(blankParams); + }); + } + + @Test + public void testDecodeVigenere(){ + ObjectNode cipherParams = generateParams(VIGENERE_KEYWORD, VIGENERE_OUTPUT_STRING); + + + ObjectNode returnedJson = vigenereCipherController.decodeVigenere(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(VIGENERE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankParams = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + vigenereCipherController.decodeVigenere(blankParams); + }); + } + + private ObjectNode generateParams(String keyword, String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + cipherParams.put(CipherParameterUtil.KEYWORD, keyword); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/BifidCipherControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/BifidCipherControllerIntegrationTest.java new file mode 100644 index 0000000..becc079 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/BifidCipherControllerIntegrationTest.java @@ -0,0 +1,89 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = BifidCipherController.class) +public class BifidCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/cipherStream/bifid"; + private String decodedString = "Message to^encode"; + private String encodedString = "Mqaokne kc^vdodzd"; + private String keyword = "keyword"; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.put(CipherParameterUtil.KEYWORD, keyword); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.put(CipherParameterUtil.KEYWORD, keyword); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString); + } + + + @Test + public void testEncodeBifid() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(bifidLogger, times(1)).info("Encoding Bifid"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodeBifid() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString)); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(bifidLogger, times(1)).info("Decoding Bifid"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/BifidCipherControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/BifidCipherControllerTest.java new file mode 100644 index 0000000..5adf90e --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/BifidCipherControllerTest.java @@ -0,0 +1,74 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class BifidCipherControllerTest{ + @InjectMocks + private BifidCipherController bifidCipherController; + private static final String BIFID_INPUT_STRING = "Message to^encode"; + private static final String BIFID_OUTPUT_STRING = "Mqaokne kc^vdodzd"; + private static final String BIFID_KEYWORD = CipherParameterUtil.KEYWORD; + private ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void testEncodeBifid(){ + ObjectNode cipherParams = generateParams(BIFID_KEYWORD, BIFID_INPUT_STRING); + + + ObjectNode returnedJson = bifidCipherController.encodeBifid(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(BIFID_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + bifidCipherController.encodeBifid(blankNode); + }); + } + + @Test + public void testDecodeAutokey(){ + ObjectNode cipherParams = generateParams(BIFID_KEYWORD, BIFID_OUTPUT_STRING); + + + ObjectNode returnedJson = bifidCipherController.decodeBifid(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(BIFID_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + bifidCipherController.decodeBifid(blankNode); + }); + } + + private ObjectNode generateParams(String keyword, String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + cipherParams.put(CipherParameterUtil.KEYWORD, keyword); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/ColumnarCipherControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/ColumnarCipherControllerIntegrationTest.java new file mode 100644 index 0000000..7324b88 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/ColumnarCipherControllerIntegrationTest.java @@ -0,0 +1,89 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = ColumnarCipherController.class) +public class ColumnarCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/cipherStream/columnar"; + private String decodedString = "Message to^encode"; + private String encodedString = "Edeomte ac^gosnse"; + private String keyword = "keyword"; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.put(CipherParameterUtil.KEYWORD, keyword); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.put(CipherParameterUtil.KEYWORD, keyword); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString); + } + + + @Test + public void testEncodeColumnar() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(columnarLogger, times(1)).info("Encoding Columnar"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodeColumnar() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString)); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(columnarLogger, times(1)).info("Decoding Columnar"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/ColumnarCipherControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/ColumnarCipherControllerTest.java new file mode 100644 index 0000000..4eb1a0d --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/ColumnarCipherControllerTest.java @@ -0,0 +1,74 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class ColumnarCipherControllerTest{ + @InjectMocks + private ColumnarCipherController columnarCipherController; + private static final String COLUMNAR_INPUT_STRING = "Message to*encode"; + private static final String COLUMNAR_OUTPUT_STRING = "Edeomte ac*gosnse"; + private static final String COLUMNAR_KEYWORD = CipherParameterUtil.KEYWORD; + private ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void testEncodeColumnar(){ + ObjectNode cipherParams = generateParams(COLUMNAR_KEYWORD, COLUMNAR_INPUT_STRING); + + + ObjectNode returnedJson = columnarCipherController.encodeColumnar(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(COLUMNAR_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + columnarCipherController.encodeColumnar(blankNode); + }); + } + + @Test + public void testDecodeColumnar(){ + ObjectNode cipherParams = generateParams(COLUMNAR_KEYWORD, COLUMNAR_OUTPUT_STRING); + + + ObjectNode returnedJson = columnarCipherController.decodeColumnar(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(COLUMNAR_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + columnarCipherController.decodeColumnar(blankNode); + }); + } + + private ObjectNode generateParams(String keyword, String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + cipherParams.put(CipherParameterUtil.KEYWORD, keyword); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/HillCipherControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/HillCipherControllerIntegrationTest.java new file mode 100644 index 0000000..e5e2b23 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/HillCipherControllerIntegrationTest.java @@ -0,0 +1,89 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = HillCipherController.class) +public class HillCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/cipherStream/hill"; + private String decodedString = "Message to^encoded"; + private String encodedString = "Mgkeqge ul^ikhisplrd"; + private int[][] keyArray = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}}; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.set(CipherParameterUtil.HILL_KEY, mapper.valueToTree(keyArray)); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.set(CipherParameterUtil.HILL_KEY, mapper.valueToTree(keyArray)); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString + "xx"); + } + + + @Test + public void testEncodeHill() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(hillLogger, times(1)).info("Encoding Hill"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodeHill() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString + "xx")); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(hillLogger, times(1)).info("Decoding Hill"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/HillCipherControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/HillCipherControllerTest.java new file mode 100644 index 0000000..87a9f23 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/HillCipherControllerTest.java @@ -0,0 +1,79 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class HillCipherControllerTest{ + @InjectMocks + private HillCipherController hillCipherController; + private static final String HILL_INPUT_STRING = "Message to^encode"; + private static final String HILL_OUTPUT_STRING = "Mgkeqge ul^ikhisp"; + private static final int[][] KEY = {{1, 4, 2}, {2, 4, 1}, {4, 1, 2}}; + private ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void testEncodeHill() throws JsonProcessingException{ + ObjectNode cipherParams = generateParams(HILL_INPUT_STRING); + + + ObjectNode returnedJson = hillCipherController.encodeHill(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(HILL_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + hillCipherController.encodeHill(blankNode); + }); + } + + @Test + public void testDecodeHill() throws JsonProcessingException{ + ObjectNode cipherParams = generateParams(HILL_OUTPUT_STRING); + + + ObjectNode returnedJson = hillCipherController.decodeHill(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(HILL_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + hillCipherController.decodeHill(blankNode); + }); + } + + private ObjectNode generateParams(String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + + JsonNode keyNode = objectMapper.valueToTree(KEY); + cipherParams.set(CipherParameterUtil.HILL_KEY, keyNode); + + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/MorseCodeControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/MorseCodeControllerIntegrationTest.java new file mode 100644 index 0000000..ac98514 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/MorseCodeControllerIntegrationTest.java @@ -0,0 +1,80 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = MorseCodeController.class) +public class MorseCodeControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/cipherStream/morse"; + private String decodedString = "Message to^encode123"; + private String encodedString = "-- . ... ... .- --. . - --- . -. -.-. --- -.. . .---- ..--- ...--"; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString.toUpperCase().replaceAll("[^A-Z0-9]", "")); + } + + + @Test + public void testEncodeMorse() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(morseLogger, times(1)).info("Encoding Morse"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodeMorse() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString.toUpperCase().replaceAll("[^A-Z0-9]", ""))); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(morseLogger, times(1)).info("Decoding Morse"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/MorseCodeControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/MorseCodeControllerTest.java new file mode 100644 index 0000000..24016ee --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/MorseCodeControllerTest.java @@ -0,0 +1,69 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class MorseCodeControllerTest{ + @InjectMocks + private MorseCodeController morseCodeController; + private static final String MORSE_INPUT_STRING = "SOS"; + private static final String MORSE_OUTPUT_STRING = "... --- ..."; + private ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void testEncodeMorse(){ + ObjectNode cipherParams = generateParams(MORSE_INPUT_STRING); + + + ObjectNode returnedJson = morseCodeController.encodeMorse(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(MORSE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + morseCodeController.encodeMorse(blankNode); + }); + } + + @Test + public void testDecodeMorse(){ + ObjectNode cipherParams = generateParams(MORSE_OUTPUT_STRING); + + + ObjectNode returnedJson = morseCodeController.decodeMorse(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(MORSE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + morseCodeController.decodeMorse(blankNode); + }); + } + + private ObjectNode generateParams(String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/PlayfairCipherControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/PlayfairCipherControllerIntegrationTest.java new file mode 100644 index 0000000..9dbb3b7 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/PlayfairCipherControllerIntegrationTest.java @@ -0,0 +1,90 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = PlayfairCipherController.class) +public class PlayfairCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/cipherStream/playfair"; + private String decodedString = "Hide the gold in - the@tree+stump"; + private String decodedStringPadded = "Hide the gold in - the@trexe+stump"; + private String encodedString = "Bmod zbx dnab ek - udm@uixmm+ouvif"; + private String keyword = "Play-fair@Exam ple"; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.put(CipherParameterUtil.KEYWORD, keyword); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.put(CipherParameterUtil.KEYWORD, keyword); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedStringPadded); + } + + + @Test + public void testEncodePlayfair() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(playfiarLogger, times(1)).info("Encoding Playfair"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodePlayfair() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedStringPadded)); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(playfiarLogger, times(1)).info("Decoding Playfair"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/PlayfairCipherControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/PlayfairCipherControllerTest.java new file mode 100644 index 0000000..89ad123 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/PlayfairCipherControllerTest.java @@ -0,0 +1,75 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class PlayfairCipherControllerTest{ + @InjectMocks + private PlayfairCipherController playfairCipherController; + private static final String PLAYFAIR_INPUT_STRING = "Hide the gold in - the@tree+stump"; + private static final String DECODED_INPUT_STRING = "Hide the gold in - the@trexe+stump"; + private static final String PLAYFAIR_OUTPUT_STRING = "Bmod zbx dnab ek - udm@uixmm+ouvif"; + private static final String PLAYFAIR_KEYWORD = "Play-fair@Exam ple"; + private ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void testEncodePlayfair(){ + ObjectNode cipherParams = generateParams(PLAYFAIR_KEYWORD, PLAYFAIR_INPUT_STRING); + + + ObjectNode returnedJson = playfairCipherController.encodePlayfair(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(PLAYFAIR_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + playfairCipherController.encodePlayfair(blankNode); + }); + } + + @Test + public void testDecodePlayfair(){ + ObjectNode cipherParams = generateParams(PLAYFAIR_KEYWORD, PLAYFAIR_OUTPUT_STRING); + + + ObjectNode returnedJson = playfairCipherController.decodePlayfair(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(DECODED_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + playfairCipherController.decodePlayfair(blankNode); + }); + } + + private ObjectNode generateParams(String keyword, String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + cipherParams.put(CipherParameterUtil.KEYWORD, keyword); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/PolybiusSquareControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/PolybiusSquareControllerIntegrationTest.java new file mode 100644 index 0000000..e5a2824 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/PolybiusSquareControllerIntegrationTest.java @@ -0,0 +1,89 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = PolybiusSquareController.class) +public class PolybiusSquareControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/cipherStream/polybius"; + private String decodedString = "Message to^encode"; + private String encodedString = "41124545233212 5115^124225152212"; + private String keyword = "keyword"; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.put(CipherParameterUtil.KEYWORD, keyword); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.put(CipherParameterUtil.KEYWORD, keyword); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString.toUpperCase()); + } + + + @Test + public void testEncodePolybius() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(polybiusLogger, times(1)).info("Encoding Polybius"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodePolybius() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString.toUpperCase())); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(polybiusLogger, times(1)).info("Decoding Polybius"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/PolybiusSquareControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/PolybiusSquareControllerTest.java new file mode 100644 index 0000000..057df38 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/PolybiusSquareControllerTest.java @@ -0,0 +1,74 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class PolybiusSquareControllerTest{ + @InjectMocks + private PolybiusSquareController polybiusSquareController; + private static final String POLYBIUS_INPUT_STRING = "B A-T"; + private static final String POLYBIUS_OUTPUT_STRING = "15 14-52"; + private static final String POLYBIUS_KEYWORD = "Z Y+ X-"; + private ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void testEncodePolybius(){ + ObjectNode cipherParams = generateParams(POLYBIUS_KEYWORD, POLYBIUS_INPUT_STRING); + + + ObjectNode returnedJson = polybiusSquareController.encodePolybius(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(POLYBIUS_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + polybiusSquareController.encodePolybius(blankNode); + }); + } + + @Test + public void testDecodePolybius(){ + ObjectNode cipherParams = generateParams(POLYBIUS_KEYWORD, POLYBIUS_OUTPUT_STRING); + + + ObjectNode returnedJson = polybiusSquareController.decodePolybius(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(POLYBIUS_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + polybiusSquareController.decodePolybius(blankNode); + }); + } + + private ObjectNode generateParams(String keyword, String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + cipherParams.put(CipherParameterUtil.KEYWORD, keyword); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/RailFenceControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/RailFenceControllerIntegrationTest.java new file mode 100644 index 0000000..4279312 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/RailFenceControllerIntegrationTest.java @@ -0,0 +1,89 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = RailFenceController.class) +public class RailFenceControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/cipherStream/railFence"; + private String decodedString = "Message to^encode"; + private String encodedString = "Maooesg te^cdsene"; + private int rails = 3; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.put(CipherParameterUtil.RAIL_FENCE_RAILS, rails); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.put(CipherParameterUtil.RAIL_FENCE_RAILS, rails); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString); + } + + + @Test + public void testEncodeRailFence() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(railFenceLogger, times(1)).info("Encoding Rail Fence"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodeRailFence() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString)); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(railFenceLogger, times(1)).info("Decoding Rail Fence"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/RailFenceControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/RailFenceControllerTest.java new file mode 100644 index 0000000..25bc7b0 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/RailFenceControllerTest.java @@ -0,0 +1,74 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class RailFenceControllerTest{ + @InjectMocks + private RailFenceController railFenceController; + private static final String RAIL_FENCE_INPUT_STRING = "Message to^encode"; + private static final String RAIL_FENCE_OUTPUT_STRING = "Moetese ne^sgcdao"; + private static final int RAIL_FENCE_RAILS = 5; + private ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void testEncodeRailFence(){ + ObjectNode cipherParams = generateParams(RAIL_FENCE_RAILS, RAIL_FENCE_INPUT_STRING); + + + ObjectNode returnedJson = railFenceController.encodeRailFence(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(RAIL_FENCE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify inavlid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + railFenceController.encodeRailFence(blankNode); + }); + } + + @Test + public void testDecodeRailFence(){ + ObjectNode cipherParams = generateParams(RAIL_FENCE_RAILS, RAIL_FENCE_OUTPUT_STRING); + + + ObjectNode returnedJson = railFenceController.decodeRailFence(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(RAIL_FENCE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify inavlid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + railFenceController.encodeRailFence(blankNode); + }); + } + + private ObjectNode generateParams(int rails, String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + cipherParams.put(CipherParameterUtil.RAIL_FENCE_RAILS, rails); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/TrifidCipherControllerIntegrationTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/TrifidCipherControllerIntegrationTest.java new file mode 100644 index 0000000..fe65643 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/TrifidCipherControllerIntegrationTest.java @@ -0,0 +1,95 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("integration-test") +@WebMvcTest(controllers = TrifidCipherController.class) +public class TrifidCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ + @Autowired + private MockMvc mockMvc; + //Fields + private String url = "/cipherStream/trifid"; + private String decodedString = "Message to^encode+"; + private String encodedString = "Gqdokpd od^ljvflf+"; + private String keyword = "keyword"; + private String trifidFill = "="; + private int grouplength = Integer.MAX_VALUE; + + + @BeforeEach + public void setup(){ + decodedNode = mapper.createObjectNode(); + decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + decodedNode.put(CipherParameterUtil.KEYWORD, keyword); + decodedNode.put(CipherParameterUtil.TRIFID_FILL, trifidFill); + decodedNode.put(CipherParameterUtil.TRIFID_GROUP_LENGTH, grouplength); + decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString); + decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString); + + encodedNode = mapper.createObjectNode(); + encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + encodedNode.put(CipherParameterUtil.KEYWORD, keyword); + encodedNode.put(CipherParameterUtil.TRIFID_FILL, trifidFill); + encodedNode.put(CipherParameterUtil.TRIFID_GROUP_LENGTH, grouplength); + encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString); + encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString); + } + + + @Test + public void testEncodeTrifid() throws Exception{ + mockMvc.perform(get(url + "/encode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(decodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString)); + + //Filter + super.verifyFilter(url + "/encode"); + //Controller + verify(trifidLogger, times(1)).info("Encoding Trifid"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); + } + + @Test + public void testDecodeTrifid() throws Exception{ + mockMvc.perform(get(url + "/decode") + .header("X-Request-Id", requestId) + .header("X-Forwarded-For", "192.168.1.1") + .contentType(MediaType.APPLICATION_JSON) + .content(encodedNode.toString())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString)); + + //Filter + super.verifyFilter(url + "/decode"); + //Controller + verify(trifidLogger, times(1)).info("Decoding Trifid"); + //Cipher Aspect + verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/TrifidCipherControllerTest.java b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/TrifidCipherControllerTest.java new file mode 100644 index 0000000..1a253f1 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/controller/polysubstitution/TrifidCipherControllerTest.java @@ -0,0 +1,78 @@ +package com.mattrixwv.cipherstream.controller.polysubstitution; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; +import com.mattrixwv.cipherstream.utils.CipherParameterUtil; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class TrifidCipherControllerTest{ + @InjectMocks + private TrifidCipherController trifidCipherController; + private static final String TRIFID_INPUT_STRING = "Message to^encode"; + private static final String TRIFID_OUTPUT_STRING = "Gpjqdvd of^odlklf"; + private static final String TRIFID_KEYWORD = CipherParameterUtil.KEYWORD; + private static final char TRIFID_FILL_ID = '+'; + private static final int TRIFID_GROUP_LENGTH = 3; + private ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void testEncodeTrifid(){ + ObjectNode cipherParams = generateParams(TRIFID_KEYWORD, TRIFID_FILL_ID, TRIFID_GROUP_LENGTH, TRIFID_INPUT_STRING); + + + ObjectNode returnedJson = trifidCipherController.encodeTrifid(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(TRIFID_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + trifidCipherController.encodeTrifid(blankNode); + }); + } + + @Test + public void testDecodeTrifid(){ + ObjectNode cipherParams = generateParams(TRIFID_KEYWORD, TRIFID_FILL_ID, TRIFID_GROUP_LENGTH, TRIFID_OUTPUT_STRING); + + + ObjectNode returnedJson = trifidCipherController.decodeTrifid(cipherParams); + + + assertEquals(cipherParams, returnedJson); + assertEquals(TRIFID_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); + + //Verify invalid params are caught + final ObjectNode blankNode = objectMapper.createObjectNode(); + assertThrows(InvalidCipherParameterException.class, () -> { + trifidCipherController.decodeTrifid(blankNode); + }); + } + + private ObjectNode generateParams(String keyword, char fill, int groupLength, String inputString){ + ObjectNode cipherParams = objectMapper.createObjectNode(); + cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + cipherParams.put(CipherParameterUtil.KEYWORD, keyword); + cipherParams.put(CipherParameterUtil.TRIFID_FILL, String.valueOf(fill)); + cipherParams.put(CipherParameterUtil.TRIFID_GROUP_LENGTH, groupLength); + cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); + return cipherParams; + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/exception/InvalidCipherParameterExceptionTest.java b/src/test/java/com/mattrixwv/cipherstream/exception/InvalidCipherParameterExceptionTest.java new file mode 100644 index 0000000..387f595 --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/exception/InvalidCipherParameterExceptionTest.java @@ -0,0 +1,51 @@ +package com.mattrixwv.cipherstream.exception; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class InvalidCipherParameterExceptionTest{ + //Fields + private String message = "Error Message"; + private Throwable cause = new Throwable(); + + + @Test + public void testConstructor_default(){ + InvalidCipherParameterException exception = new InvalidCipherParameterException(); + + assertNull(exception.getMessage()); + assertNull(exception.getCause()); + } + + @Test + public void testConstructor_message(){ + InvalidCipherParameterException exception = new InvalidCipherParameterException(message); + + assertEquals(message, exception.getMessage()); + assertNull(exception.getCause()); + } + + @Test + public void testConstructor_cause(){ + InvalidCipherParameterException exception = new InvalidCipherParameterException(cause); + + assertEquals(cause.toString(), exception.getMessage()); + assertEquals(cause, exception.getCause()); + } + + @Test + public void testConstructor_messageCause(){ + InvalidCipherParameterException exception = new InvalidCipherParameterException(message, cause); + + assertEquals(message, exception.getMessage()); + assertEquals(cause, exception.getCause()); + } +} diff --git a/src/test/java/com/mattrixwv/cipherstream/utils/CipherParameterUtilTest.java b/src/test/java/com/mattrixwv/cipherstream/utils/CipherParameterUtilTest.java new file mode 100644 index 0000000..53d6a2c --- /dev/null +++ b/src/test/java/com/mattrixwv/cipherstream/utils/CipherParameterUtilTest.java @@ -0,0 +1,426 @@ +package com.mattrixwv.cipherstream.utils; + + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; + + +@Tag("unit-test") +@ExtendWith(MockitoExtension.class) +public class CipherParameterUtilTest{ + private static final ObjectMapper objectMapper = new ObjectMapper(); + + + @Test + public void testVerifyUniversalParams(){ + ObjectNode params = setUniversalParams(); + + //Missing Capitals + params.remove(CipherParameterUtil.PRESERVE_CAPITALS); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyUniversalParams(params); + }); + //Integer Capitals + params.put(CipherParameterUtil.PRESERVE_CAPITALS, 1); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyUniversalParams(params); + }); + params.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + + //Missing Whitespace + params.remove(CipherParameterUtil.PRESERVE_WHITESPACE); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyUniversalParams(params); + }); + //Integer Whitespace + params.put(CipherParameterUtil.PRESERVE_WHITESPACE, 1); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyUniversalParams(params); + }); + params.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + + //Missing Symbols + params.remove(CipherParameterUtil.PRESERVE_SYMBOLS); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyUniversalParams(params); + }); + //Integer Symbols + params.put(CipherParameterUtil.PRESERVE_SYMBOLS, 1); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyUniversalParams(params); + }); + params.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + + //Missing Input + params.remove(CipherParameterUtil.INPUT_STRING); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyUniversalParams(params); + }); + //Integer Input + params.put(CipherParameterUtil.INPUT_STRING, 1); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyUniversalParams(params); + }); + params.put(CipherParameterUtil.INPUT_STRING, "Input String"); + + //Full + assertDoesNotThrow(() -> { + CipherParameterUtil.verifyUniversalParams(params); + }); + } + + @Test + public void testVerifyParamsWithKeyword(){ + ObjectNode params = setUniversalParams(); + + //Missing Keyword + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyParamsWithKeyword(params); + }); + //Integer Keyword + params.put(CipherParameterUtil.KEYWORD, 1); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyParamsWithKeyword(params); + }); + + //Full + params.put(CipherParameterUtil.KEYWORD, CipherParameterUtil.KEYWORD); + assertDoesNotThrow(() -> { + CipherParameterUtil.verifyParamsWithKeyword(params); + }); + } + + @Test + public void testVerifyCaesarParams(){ + ObjectNode params = setUniversalParams(); + + //Missing shift + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyCaesarParams(params); + }); + //String shift + params.put(CipherParameterUtil.CAESAR_SHIFT_AMOUNT, "1"); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyCaesarParams(params); + }); + + //Full + params.put(CipherParameterUtil.CAESAR_SHIFT_AMOUNT, 1); + assertDoesNotThrow(() -> { + CipherParameterUtil.verifyCaesarParams(params); + }); + } + + @Test + public void testVerifyAffineParams(){ + ObjectNode params = setUniversalParams(); + + //Missing Key1 + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyAffineParams(params); + }); + //String Key1 + params.put(CipherParameterUtil.AFFINE_KEY_1, "1"); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyAffineParams(params); + }); + params.put(CipherParameterUtil.AFFINE_KEY_1, 1); + + //Missing Key2 + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyAffineParams(params); + }); + //String Key2 + params.put(CipherParameterUtil.AFFINE_KEY_2, "1"); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyAffineParams(params); + }); + params.put(CipherParameterUtil.AFFINE_KEY_2, 1); + + //Full + assertDoesNotThrow(() -> { + CipherParameterUtil.verifyAffineParams(params); + }); + } + + @Test + public void testVerifyAtbashParams(){ + ObjectNode params = setUniversalParams(); + + assertDoesNotThrow(() -> { + CipherParameterUtil.verifyAtbashParams(params); + }); + } + + @Test + public void testVerifyBaconianParams(){ + ObjectNode params = objectMapper.createObjectNode(); + + //Missing Capitals + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyBaconianParams(params); + }); + //Integer Capitals + params.put(CipherParameterUtil.PRESERVE_CAPITALS, 1); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyBaconianParams(params); + }); + params.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + + //Missing Input + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyBaconianParams(params); + }); + //Integer Input + params.put(CipherParameterUtil.INPUT_STRING, 1); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyBaconianParams(params); + }); + params.put(CipherParameterUtil.INPUT_STRING, "Input String"); + + //Full + assertDoesNotThrow(() -> { + CipherParameterUtil.verifyBaconianParams(params); + }); + } + + @Test + public void testVerifyBaseXParams(){ + ObjectNode params = setUniversalParams(); + + //Missing Input + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyBaseXParams(params); + }); + //Integer Input + params.put(CipherParameterUtil.INPUT_STRING, 1); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyBaseXParams(params); + }); + params.put(CipherParameterUtil.INPUT_STRING, CipherParameterUtil.INPUT_STRING); + + //Missing BaseX + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyBaseXParams(params); + }); + //String BaseX + params.put(CipherParameterUtil.BASE_X_BASE, "2"); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyBaseXParams(params); + }); + params.put(CipherParameterUtil.BASE_X_BASE, 2); + + //Full + assertDoesNotThrow(() -> { + CipherParameterUtil.verifyBaseXParams(params); + }); + } + + @Test + public void testVerifySquareKeyword(){ + ObjectNode params = setUniversalParams(); + + //Missing Keyword + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifySquareKeyword(params); + }); + + //Integer Keyword + params.put(CipherParameterUtil.SQUARE_KEYWORD, 1); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifySquareKeyword(params); + }); + + //Full + params.put(CipherParameterUtil.SQUARE_KEYWORD, "squareKeyword"); + assertDoesNotThrow(() -> { + CipherParameterUtil.verifySquareKeyword(params); + }); + } + + @Test + public void testVerifyHillParams(){ + ObjectNode params = setUniversalParams(); + + //Missing Key + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyHillParams(params); + }); + + //Integer Key + params.put(CipherParameterUtil.HILL_KEY, 1); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyHillParams(params); + }); + + //1D Array Key + params.set(CipherParameterUtil.HILL_KEY, objectMapper.valueToTree(new int[]{1})); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyHillParams(params); + }); + + //String Array Key + params.set(CipherParameterUtil.HILL_KEY, objectMapper.valueToTree(new String[][]{{"test"}})); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyHillParams(params); + }); + + //Full + params.set(CipherParameterUtil.HILL_KEY, objectMapper.valueToTree(new int[][]{{1}})); + assertDoesNotThrow(() -> { + CipherParameterUtil.verifyHillParams(params); + }); + } + + @Test + public void testVerifyMorseParams(){ + ObjectNode params = objectMapper.createObjectNode(); + + //Missing Input + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyMorseParams(params); + }); + //Integer Input + params.put(CipherParameterUtil.INPUT_STRING, 1); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyMorseParams(params); + }); + params.put(CipherParameterUtil.INPUT_STRING, "Input String"); + + //Full + assertDoesNotThrow(() -> { + CipherParameterUtil.verifyMorseParams(params); + }); + } + + @Test + public void testVerifyPolybiusParams(){ + ObjectNode params = objectMapper.createObjectNode(); + + //Missing Whitespace + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyPolybiusParams(params); + }); + //String Whitespace + params.put(CipherParameterUtil.PRESERVE_WHITESPACE, "true"); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyPolybiusParams(params); + }); + params.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + + //Missing Symbols + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyPolybiusParams(params); + }); + //String Symbols + params.put(CipherParameterUtil.PRESERVE_SYMBOLS, "true"); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyPolybiusParams(params); + }); + params.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + + //Missing Input + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyPolybiusParams(params); + }); + //Integer Input + params.put(CipherParameterUtil.INPUT_STRING, 1); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyPolybiusParams(params); + }); + params.put(CipherParameterUtil.INPUT_STRING, ""); + + //Missing Keyword + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyPolybiusParams(params); + }); + //Integer Keyword + params.put(CipherParameterUtil.KEYWORD, 1); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyPolybiusParams(params); + }); + params.put(CipherParameterUtil.KEYWORD, CipherParameterUtil.KEYWORD); + + //Full + assertDoesNotThrow(() -> { + CipherParameterUtil.verifyPolybiusParams(params); + }); + } + + @Test + public void testVerifyRailFenceParams(){ + ObjectNode params = setUniversalParams(); + + //Missing Rails + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyRailFenceParams(params); + }); + //String rails + params.put(CipherParameterUtil.RAIL_FENCE_RAILS, "1"); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyRailFenceParams(params); + }); + params.put(CipherParameterUtil.RAIL_FENCE_RAILS, 1); + + //Full + assertDoesNotThrow(() -> { + CipherParameterUtil.verifyRailFenceParams(params); + }); + } + + @Test + public void testVerifyTrifidParams(){ + ObjectNode params = setUniversalParams(); + params.put(CipherParameterUtil.KEYWORD, CipherParameterUtil.KEYWORD); + + //Missing fill + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyTrifidParams(params); + }); + //Integer fill + params.put(CipherParameterUtil.TRIFID_FILL, 1); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyTrifidParams(params); + }); + //String fill + params.put(CipherParameterUtil.TRIFID_FILL, "Input"); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyTrifidParams(params); + }); + params.put(CipherParameterUtil.TRIFID_FILL, "+"); + + //Missing length + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyTrifidParams(params); + }); + //String length + params.put(CipherParameterUtil.TRIFID_GROUP_LENGTH, "2"); + assertThrows(InvalidCipherParameterException.class, () -> { + CipherParameterUtil.verifyTrifidParams(params); + }); + params.put(CipherParameterUtil.TRIFID_GROUP_LENGTH, 2); + + //Full + assertDoesNotThrow(() -> { + CipherParameterUtil.verifyTrifidParams(params); + }); + } + + + private ObjectNode setUniversalParams(){ + ObjectNode universalParams = objectMapper.createObjectNode(); + universalParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); + universalParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); + universalParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); + universalParams.put(CipherParameterUtil.INPUT_STRING, "Input String"); + return universalParams; + } +} diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties new file mode 100644 index 0000000..33406d2 --- /dev/null +++ b/src/test/resources/application.properties @@ -0,0 +1 @@ +spring.main.banner-mode=off diff --git a/src/test/resources/log4j2-test.xml b/src/test/resources/log4j2-test.xml new file mode 100644 index 0000000..d725dee --- /dev/null +++ b/src/test/resources/log4j2-test.xml @@ -0,0 +1,19 @@ + + + + + + + + %style{%d{MM-dd-yyyy HH:mm:ss.SSS}}{bright, black} %highlight{%5p} %style{%50.50replace{%c{0}.%M}{}{}}{blue, bright} %highlight{---} %message%n + + + + + + + + + + + diff --git a/version-rules.xml b/version-rules.xml new file mode 100644 index 0000000..7ab49c9 --- /dev/null +++ b/version-rules.xml @@ -0,0 +1,17 @@ + + + + + (?i).*Alpha(?:-?\d+)? + (?i).*a(?:-?\d+)? + (?i).*Beta(?:-?\d+)? + (?i).*-B(?:-?\d+)? + (?i).*RC(?:-?\d+)? + (?i).*CR(?:-?\d+)? + (?i).*M(?:-?\d+)? + + + +