cognition.pipeline.sanity.AnonymisationSanityChecker.java Source code

Java tutorial

Introduction

Here is the source code for cognition.pipeline.sanity.AnonymisationSanityChecker.java

Source

/*
Designed and developed by Ismail E. Kartoglu
Copyright 2015 King's College London
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
    http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

package cognition.pipeline.sanity;

import cognition.pipeline.service.anonymisation.AnonymisationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import cognition.common.utils.TimeUtil;
import cognition.common.model.Individual;
import cognition.common.model.PatientAddress;

import javax.annotation.PostConstruct;
import java.text.ParseException;

import static junit.framework.TestCase.assertFalse;

@Component
public class AnonymisationSanityChecker {

    @Autowired
    private AnonymisationService anonymisationService;

    @PostConstruct
    public void checkBasicAnonymisationRules() throws ParseException {
        Individual individual = new Individual();
        individual.addForeName("TestName1");
        individual.addForeName("TestName2");
        individual.addSurname("TestLastName1");
        individual.addSurname("TestLastName1");
        individual.addNhsNumber("11122");
        PatientAddress patientAddress1 = new PatientAddress();
        patientAddress1.setAddress("Kidderpore Avenue Hampstead, London");
        patientAddress1.setPostCode("cb4 2za");
        PatientAddress patientAddress2 = new PatientAddress();
        patientAddress2.setAddress("addressText");
        patientAddress2.setPostCode("cb4 2za");
        individual.addAddress(patientAddress1);
        individual.addAddress(patientAddress2);
        individual.addPhoneNumber("50090051234");
        individual.addPhoneNumber("11090051234");
        individual.addDateOfBirth(TimeUtil.getDateFromString("09/05/1990", "dd/MM/yyyy"));

        String anonymisedText = anonymisationService.pseudonymisePersonPlainText(individual,
                "\n" + "\n" + "TestName1 TestName2 TestLastName1 TestLastName2\n" + "\n" + "\n" + "11122\n"
                        + "50090051234" + "\n 09/05/1990" + "\n" + "cb42za"
                        + " Some random text that should not be anonymised. \n"
                        + "Address is Kidderpore Ave, (Hampstead, London.");
        System.out.println(anonymisedText);
        if (anonymisedText.contains("TestName1") || anonymisedText.contains("TestName2")) {
            throw new AssertionError(
                    "First name pseudonymisation is not working! Please check config/anonymisation/nameRules");
        }
        if (anonymisedText.contains("TestLastName1") || anonymisedText.contains("TestLastName2")) {
            throw new AssertionError(
                    "Last name pseudonymisation is not working! Please check config/anonymisation/nameRules");
        }
        if (anonymisedText.contains("11122")) {
            throw new AssertionError(
                    "NHS Number pseudonymisation is not working! Please check config/anonymisation/nhsIdRules");
        }
        if (anonymisedText.contains("50090051234")) {
            throw new AssertionError(
                    "Phone number pseudonymisation is not working! Please check config/anonymisation/phoneRules");
        }
        if (anonymisedText.contains("09/05/1990")) {
            throw new AssertionError(
                    "Date of birth pseudonymisation is not working! Please check config/anonymisation/dateOfBirthRules");
        }
        if (anonymisedText.contains("cb42za")) {
            throw new AssertionError(
                    "Post code pseudonymisation is not working! Please check config/anonymisation/addressRules");
        }
        if (!anonymisedText.contains("Some random text that should not be anonymised.")) {
            throw new AssertionError("Pseudonymisation rules anonymise everything? Please check your rules!");
        }

        if (!anonymisedText.contains("Address is AAAAA")) {
            throw new AssertionError(
                    "Approximate address pseudonymisation is not working. Please check config/anonymisation/addressRules!");
        }
    }

}