liveBook · Manning

Resource naming

As explained in Data-Oriented Programming in Java

Resource naming is the practice of assigning structured identifiers to external resources so they can be grouped, located, and removed systematically.

Resource naming gives temporary external resources structured identities instead of anonymous random names. In integration tests, that structure preserves isolation while making stale queues, buckets, and other resources discoverable for cleanup. section 12.6

A practical naming scheme combines:

  • A stable root identifying the integration-test namespace. section 12.6
  • Owner, suite, and test identifiers that support cleanup at increasingly narrow scopes. section 12.6
  • A random suffix that prevents collisions and isolates each test run from prior failures. section 12.6
Listing 12.15 An example approach to name spacing resources
record Namespace(String owner, String suite, String testName){  #A
    static String ROOT = "integ";  #B
}
 
class Scope {
    public static String everything(Namespace ns) {  #C
        return Namespace.ROOT;
    }
    public static String ownedBy(Namespace ns) {     #C
        return String.join("-", Namespace.ROOT, ns.owner);     
    }
    public static String thisSuite(Namespace ns) {   #C
        return String.join("-", Namespace.ROOT, ns.owner, ns.suite);
    }
    public static String thisTest(Namespace ns) {    #C
        return String.join("-", Namespace.ROOT, ns.owner, ns.suite, ns.testName);
    }
}
 
String randomName(Namespace namespace) {   #D
    return format(
        "%s-%s",
        Scope.thisTest(namespace),
        UUID.randomUUID().toString().substring(0, 8)
    );
}

The Namespace and Scope types make cleanup boundaries explicit, while randomName combines the test scope with a short UUID suffix. section 12.6

Listing 12.16 Supporting code
class InfraTooling {
    private CloudSDK cloudSDK;
    private Namespace namespace;
 
    Queue createQueue() {
        return cloudSDK.createQueue(randomName(namespace));
    }
    Bucket createBucket(){
        return cloudSDK.createBucket(randomName(namespace));
    }
    void cleanup(Function<Namespace, String> prefix) {
        deleteQueues(prefix);
        deleteBuckets(prefix);
        deleteWhatever(prefix);
    }
 
    void deleteQueues(Function<Namespace, String> strat) {
        for (String name : cloudSDK.listQueues()) {
            if (name.startsWith(strat.apply(namespace))) {
                cloudSDK.deleteQueue(name);
            }
        }
    }
}
 
@Test
void myCoolIntegrationTest() {
    String owner = myDependencyInjector.environment();
    Namespace namespace = new Namespace(
            owner, 
            "Chapter12", 
            "myCoolIntegrationTest");
    InfraTooling infra = new InfraTooling(cloudSDK, namespace);
 
    Queue inputQueue = infra.createQueue();
    Queue outputQueue = infra.createQueue();
    Bucket bucket = infra.createBucket();
 
    // test code here
 
    infra.cleanup(Scope::thisTest);
}

Support code centralizes resource creation and deletes resources whose names begin with a selected namespace prefix. section 12.6

How Data-Oriented Programming in Java covers it

How deep it goes
Defines tiered prefixes, random suffixes, and prefix-based deletion, but leaves application-specific conventions to experimentation.
Examples
Uses queues and buckets named from owner, suite, test, and UUID components.
What it assumes
Assumes integration tests create cloud resources and use dependency injection to supply them to the service under test.
What you can do after
Operate/configure integration-test infrastructure so resources can be isolated, located, and removed at multiple scopes.
How it connects
Connects test isolation, randomization, cleanup, dependency injection, and cloud infrastructure.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.