Modernized by Imogen: AWS CardDemo

Successful modernization has two non-negotiables:

Modernization projects are complex because people want to 1) build new things, but also 2) avoid breaking what’s supporting their business today.

Mainframe applications bring with them massive data dependencies and layers of poorly understood code. Business users want to review, modify, or add to the existing legacy business rules, but they want to do so without breaking hidden rules and crucial system behavior.

Imogen is a purpose-built mainframe modernization solution that solves this conundrum:

  1. Don’t break what’s working

    Imogen protects upstream and downstream dependencies, verifies the details of processing and calculations, and preserves mission-critical rules.

  2. Build new things

    With Imogen, you can reorganize code, adopt new architecture patterns, and confirm new code meets quality standards.


In this sample, we show how you can use Imogen to generate modern code based on updated business rules, and use data-driven verification to confirm you have protected any rules you want unchanged.

Making a change in CardDemo

Most modernization projects start because business leaders have at least one specific change that they'd like to make to their application. The Imogen platform correlates top-down business rules with bottom-up tests based on live behavior, so that change is not only fast, but safe.

Many people have converted CardDemo. We’ll show you how Imogen makes a business rule change to CardDemo, and how verifying that change doesn’t break any of the other existing behavior. Imogen automates this process to make it easy to surgically change only the rules and behavior you want to update.

 

The generated Java covers the complete core of the application and is tested for equivalence with the original COBOL, proving that the modernization process has not introduced any unintentional drift from the bytes generated on a live mainframe implementation.

The point of modernizing is being able to change the application and avoid unintentional breaks. As per the goals of this example, the section Reimagining the application shows the how-to process for a business rule being changed on a branch of the repository, with the tests guarding everything else.

Mechanical Orchard used Imogen to rewrite AWS CardDemo, a COBOL credit-card application written by AWS as a realistic mainframe workload, in Java. The result is published on GitHub as aws-carddemo-imogen.

 

What is in the repository

The rewrite covers all seventeen online (CICS) screens and all ten batch jobs that contain CardDemo's business logic. CardDemo's other batch jobs run only standard IBM utilities to set up, load, back up and sort files, work that a modern platform does with its own tools rather than with application code.

aws-carddemo-batch/     the ten batch jobs
aws-carddemo-web/       the seventeen online screens
docs/                   requirements; notes per screen; known differences
scripts/                builds the original screens for the screen tests
pom.xml                 the Maven build for both modules


In the repository, each module has its own README with a guide to its code and tests:
batch jobs and online screens.

 

Mechanical Orchard’s methodology

  1. We believe in a tests-before-code, data-driven modernization.

  2. Our approach provides incremental verification of unit, functional, and behavioral equivalence.

  3. The Imogen platform compresses the time to capture behavior, build tests, and verify forward-engineered code.


Imogen reduces uncertainty and the long tail of integration testing. Each component is proven to work before integration.


 

Establishing what the application does

Operating and recording CardDemo on a mainframe

Before modernization began, Mechanical Orchard installed CardDemo on a real mainframe and operated it as a bank would operate it: a daily cycle that posts the day's transactions and produces statements, a monthly cycle that calculates interest, and the housekeeping jobs around them, such as the backup that must run each day before transactions can be posted. Each cycle works on the files the previous one left, so the data builds up as it would in production.

Imogen recorded each job run: its inputs before the run and its outputs after it. Because the runs come from operating the application, not from cases written for a test, the recorded runs are realistic: nobody chose their data to suit the Java. Each recorded run is the reference result for one run of one job on the mainframe.

The recorded runs are kept for the end. Once the Java passes every other test, each recorded run is replayed in Java from the same inputs, and the Java's outputs must match what the mainframe wrote.

 

Characterization: tests that record what the original does

For each COBOL program, Imogen compiled the original source with Mechanical Orchard's own COBOL compiler so that it could run off the mainframe, then wrote characterization tests for it: tests that record what existing code does, so that new code can be held to the same behavior. The cases cover an empty file, one record, several records, values at their limits, missing files and invalid data.

The compiled original is called the oracle: its results are taken as correct.

Imogen measured which lines of the original COBOL the cases executed, and kept adding cases until further cases reached no new lines. For every line that no case reaches, Imogen determined why.

In the batch programs, almost all such lines handle a file that fails after it has opened successfully, such as a failed read or close, which a test cannot produce.

For the screen programs, Imogen first listed what each program does, one entry for each thing a user can observe, and the tests were written from that list. In the screen programs, most lines that no case reaches are ones no key, input or file record can reach, such as the fallback for a calling screen that is never blank. For the rest, the Java is tested on its own against what the source says. A reviewer read and approved the reason for every such line.

Why a dedicated COBOL compiler

 

Open-source compilers such as GnuCOBOL are designed to run COBOL on any platform, independent of the mainframe.

Mechanical Orchard’s compiler is designed to behave like the mainframe:
  • it runs batch programs on EBCDIC data without converting it;
  • it follows z/OS where the two differ, such as sort order, truncation of binary numbers and division by zero;
  • it runs the facilities that surround COBOL on a mainframe, such as CICS screens and VSAM files.

Because every characterization test compares the Java with the oracle, the oracle has to behave like the mainframe.

 

 
Concolic testing: paths the characterization tests had not reached

A set of tests is only as thorough as the choice of its cases. Imogen therefore also generates cases directly from the logic of the COBOL, with a second, independent method: concolic testing.

The concolic testing engine runs the program, and at each decision the program makes (whether the file exists, whether the record was found, whether the date is in range) it asks a constraint solver for an input that takes the other branch. Its inputs come from the program's own conditions, so they include ones a person would be unlikely to try.

Imogen ran the engine on all ten batch programs. In the transaction report, for example, it produced an amount of exactly zero, which no earlier case had. The original prints it blank rather than as .00, and the case is now a test.

 

Replicating the application in Java, and proving it

Java written to match the original

Imogen wrote the Java to pass those tests. Each test feeds the same input to the oracle and to the Java and compares: 

  • every output file, byte for byte;
  • everything printed;
  • the return code.


A test passes only when the two agree. On every characterization and concolic testing case, the Java either matches the oracle exactly or differs in one of the ways listed, each with its reason, in the repository’s known-differences.md. Several are places where the original COBOL has a fault that the Java deliberately does not copy, such as an amount too large for its field.

The online screens are tested the same way. Each screen test sends the same keystrokes to the oracle and to the Java web page, then compares what each shows and what each writes to the files.

The screen tests follow a few rules:

  • The tests come first. A screen's tests were written, reviewed and run against the oracle before its Java existed, and each was seen to fail while the screen was missing. The Java was then written to pass them.
  • Everything is compared. A test compares every field of every screen in the conversation and every record written to the files.
  • A difference must be named to be accepted. Where the Java is meant to differ from the oracle, the test names the exact field or bytes and gives the reason. Any other difference fails the test.
  • The page is held to the screen map. Each page is checked against the original's screen definition, and each kind of page is sent input a terminal could never send.
  • Screens are also tested together. Longer tests walk several screens in one conversation on the sample data, from sign-on through the menus, on the oracle and on the Java.

Because the comparison is made key by key, each Java page is modeled on the original's screen: fixed-length fields, function keys, and one handler per screen that branches on the key pressed. This model is what makes the comparison exact.

Next for the screens: a web design rethought for the browser.
The tests that prove the screens equivalent stay in place and guide that redesign, as they guided the
redesign of the batch Java.

 

Backtests: replaying recorded mainframe runs

The oracle models the mainframe closely, but it is not the mainframe. A backtest checks the Java against the mainframe itself, using the runs recorded before modernization began. It replays one recorded run of a job in Java: 

  1. The job's input files are set up from the recorded ones.
  2. The whole Java job is launched as it would run in production, with the recorded run's parameters and its clock set to the moment the mainframe run started.
  3. Each output file the Java writes is compared, byte for byte, with the file the mainframe wrote.

Backtests complement the earlier steps. Characterization and concolic testing check each program's logic, on chosen inputs, against the oracle. A backtest checks a whole job, on the data of a real run, against the real mainframe. It therefore also covers what the oracle cannot: 

  • the mainframe's own compiler and runtime;
  • the sort and copy steps that run between the COBOL programs;
  • data built up by the application's own daily and monthly cycles, rather than written for a test.

A few fields cannot match, for example, timestamps the mainframe takes from its own clock as it writes each record. Those fields are left out of the comparison, and every other byte is compared.

Each backtest is a test in the repository (<Job>BacktestTest.java, for example CreastmtBacktestTest.java), so it can be repeated whenever the Java changes.

 

Fault injection: checking the tests themselves

A test that passes is only useful if it would fail on wrong code. To check the batch tests, Imogen deliberately introduced small faults into the batch Java across all ten jobs, and each fault was expected to make at least one test fail.

Where a fault went undetected, Imogen added tests. The characterization tests of whole jobs (<Job>JobEquivalenceTest.java) came from this step: they launch each job as it would run in production and compare its exit code, printed output and output files with the oracle.

 

Reimagining the application

The first two parts produce Java that does what the COBOL did, with tests that prove it. This part uses those tests to let the business change the application. The business rules are written as requirements, the Java is redesigned around the business, and a requirement is changed on a branch with the tests guarding everything else.

 

Business rules written as requirements

The tests, and the business rules they state, stay with the Java after the COBOL is retired. Each batch program's rules are written in its characterization tests, one plain sentence per rule. Every check in a test is labeled with the rule it shows, so a failing check names the rule that broke. 

The tests can also run on the Java alone, against the expected values written in them (-Dcarddemo.oracle=skip), so they do not need the COBOL to keep working.

 For the monthly interest job, the rules are written as requirements in EARS (the Easy Approach to Requirements Syntax) and collected on one page: requirements of the interest job. The page lists each requirement with the tests that show it. It is written from the tests, and a test fails when the page and the tests disagree.

 

Refactoring the Java

The Java as first written followed the structure of the COBOL: one class per COBOL program, named after it, with comments naming the COBOL paragraphs each part came from. Imogen then redesigned the batch Java around the business, one job at a time. The characterization tests and the rule checks all pass on the redesigned Java.

For example, the original COBOL calculates a month's interest in the middle of its main loop:

IF DIS-INT-RATE NOT = 0

PERFORM 1300-COMPUTE-INTEREST

PERFORM 1400-COMPUTE-FEES

END-IF

...

1300-COMPUTE-INTEREST.

COMPUTE WS-MONTHLY-INT

= ( TRAN-CAT-BAL * DIS-INT-RATE) / 1200


In the Java, the same rule is a type of its own, with no file handling (
InterestRate.java, shown here without its Javadoc):

record InterestRate(BigDecimal annualPercent) {

  // Twelve months, and percent to a fraction.
  private static final BigDecimal TWELVE_MONTHS_OF_PERCENT = BigDecimal.valueOf(1200);
  // Interest is kept to the cent; the original's amount field drops the rest, it does not round.
  private static final int CENTS = 2;
  private static final RoundingMode DROP_FRACTIONS_OF_A_CENT = RoundingMode.DOWN;

  boolean chargesInterest() {
    return annualPercent.signum() != 0;
  }

  BigDecimal monthlyInterestOn(BigDecimal balance) {
    return balance.multiply(annualPercent)
        .divide(TWELVE_MONTHS_OF_PERCENT, CENTS, DROP_FRACTIONS_OF_A_CENT);
  }
}


The COBOL does not say that part of a cent is dropped; that follows from the size of the field the result is stored in. The Java names it, and tests hold it to one of the interest job's
requirements: "The interest job shall drop any part of a cent from the monthly interest, cutting toward zero for a negative amount as for a positive one."

The batch module's README describes how each job's package is laid out.

 

Changing a requirement

When reviewing the business rules for the original program (INTCALC-E2), a business analyst noticed a behavior that needs updating. Specifically, INTCALC-E2 has logic that adds an account's interest to the account's balance when it reaches the next account, so the last account in the file is charged interest that its balance never shows.

Imogen extracted the business rules for this code, and can automatically produce new modern code which is capable of delivering the same behavior. Or, if the analyst provides a new, natural language update to the rule, the new rule can inform how tests will be updated to match the newly desired behavior.

Making this new business requirement a reality is automated with the Imogen platform. In the linked repo branch change-last-account-requirement, you will see the 4 steps that are taken to go from a new idea to soundly tested code:

  1. The business requirement, written as a sentence changes: the interest job shall update the last account as it updates every other.
  2. The checks labeled INTCALC-E2 get their new figures and fail, each naming INTCALC-E2 and no other requirement.
  3. One statement is added to InterestPosting.java and every test passes, with everything but that one account still compared with the local reference, byte for byte.
  4. The local reference is retired for this job: its tests keep their inputs and their expected figures, and run on the Java alone. The comparison of the whole job with the oracle and the replay of the recorded mainframe run are removed with it, because both carry the fault the requirement corrected.

INTCALC-E2 github

See the four commits side by side on GitHub.

 

 

Contact

To see Imogen work on your own application, contact us.