Context:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gov.hhs.fha.nhinc.transform.subdisc;

import javax.xml.bind.JAXBElement;
import junit.framework.Assert;
import org.hl7.v3.MCCIMT000100UV01Device;
import org.hl7.v3.MCCIMT000100UV01Receiver;
import org.hl7.v3.MCCIMT000100UV01Sender;
import org.hl7.v3.PRPAIN201305UV02;
import org.hl7.v3.PRPAIN201306UV02;
import org.hl7.v3.PRPAMT201301UV02Patient;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hl7.v3.ActRelationshipMitigates;
import org.hl7.v3.CS;
import org.hl7.v3.II;
import org.hl7.v3.MCAIMT900001UV01DetectedIssueEvent;
import org.hl7.v3.MCAIMT900001UV01DetectedIssueManagement;
import org.hl7.v3.PRPAMT201301UV02Person;
import org.hl7.v3.TSExplicit;
import org.hl7.v3.XActMoodDefEvn;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.lib.legacy.ClassImposteriser;

/**
 *
 * @author dunnek
 */
public class HL7PRPA201306TransformsTest {

    private Mockery context;

    private static Log log = LogFactory.getLog(HL7PRPA201306TransformsTest.class);
    private String localDeviceId = "2.16.840.1.113883.3.200.1";
    private String senderOID = "2.16.840.1.113883.3.200";
    private String receiverOID = "2.16.840.1.113883.3.184";
    private String patientFirstName = "Thomas";
    private String patientLastName = "Kirtland";
    private String gender = "M";
    private String birthTime = "19261225";
    private String ssn = "134679852";
    private String patId = "46821564";

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
        context = new Mockery() {

            {
                setImposteriser(ClassImposteriser.INSTANCE);
            }
        };
    }

    @After
    public void tearDown() {
    }

    @Test
    public void testCreatePRPA201306() {
        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }
        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                will(returnValue(with(any(PRPAIN201306UV02.class))));
            }
        });
        log.info("testCreatePRPA201306");

        JAXBElement<PRPAMT201301UV02Person> person = HL7PatientTransforms.create201301PatientPerson(patientFirstName, patientLastName, gender, birthTime, ssn);
        PRPAMT201301UV02Patient patient = HL7PatientTransforms.create201301Patient(person, patId, localDeviceId);

        PRPAIN201305UV02 query = HL7PRPA201305Transforms.createPRPA201305(patient, senderOID, receiverOID, localDeviceId);
        PRPAIN201306UV02 result = HL7PRPA201306Transforms.createPRPA201306(patient, senderOID, receiverOID, receiverOID, localDeviceId, query);

        context.assertIsSatisfied();

        TestHelper.assertReceiverIdEquals(receiverOID, result);
        TestHelper.assertSenderIdEquals(senderOID, result);
    /*
    TestHelper.assertPatientIdEquals(patId, localDeviceId, result);
    TestHelper.assertBirthTimeEquals(birthTime, result);
    TestHelper.assertPatientNameEquals(patientFirstName, patientLastName, result);
    TestHelper.assertGenderEquals(gender, result);
     */
    }

    @Test
    public void testcreatePRPA201306ForPatientNotFoundWillPass() {
        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }

            protected String getReceiverOIDFromPRPAIN201305UV02Request(PRPAIN201305UV02 oRequest) {
                return receiverOID;
            }

            protected String getSenderOIDFromPRPAIN201305UV02Request(PRPAIN201305UV02 oRequest) {
                return senderOID;
            }

            protected II getHL7MessageId(String receiverOID) {
                II oII = new II();
                oII.setRoot(receiverOID);

                return oII;
            }

            protected TSExplicit getHL7CreationTime() {
                return new TSExplicit();
            }

            protected II getHL7InteractionId() {
                return new II();
            }

            protected CS getHL7ProcessingCode() {
                return new CS();
            }

            protected CS getHL7ProcessingModeCode() {
                return new CS();
            }

            protected CS getHL7AcceptAckCode() {
                return new CS();
            }

        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                will(returnValue(with(any(PRPAIN201306UV02.class))));
            }
        });
        log.info("testcreatePRPA201306ForPatientNotFoundWillPass");

        JAXBElement<PRPAMT201301UV02Person> person = HL7PatientTransforms.create201301PatientPerson(patientFirstName, patientLastName, gender, birthTime, ssn);
        PRPAMT201301UV02Patient patient = HL7PatientTransforms.create201301Patient(person, patId, localDeviceId);

        PRPAIN201305UV02 query = HL7PRPA201305Transforms.createPRPA201305(patient, senderOID, receiverOID, localDeviceId);
        PRPAIN201306UV02 result = testSubject.createPRPA201306ForPatientNotFound(query);

        context.assertIsSatisfied();

        //reverse the sender and receiver expectations
        TestHelper.assertReceiverIdEquals(senderOID, result);
        TestHelper.assertSenderIdEquals(receiverOID, result);

        //AA (application accept) is returned in Acknowledgement.typeCode (transmission wrapper).
        Assert.assertNotNull(result.getAcknowledgement());
        Assert.assertEquals("AA", result.getAcknowledgement().get(0).getTypeCode().getCode());
        //OK (data found, no errors) is returned in QueryAck.queryResponseCode (control act wrapper)
        Assert.assertNotNull(result.getControlActProcess());
        Assert.assertNotNull(result.getControlActProcess().getQueryAck());
        Assert.assertEquals("OK", result.getControlActProcess().getQueryAck().getQueryResponseCode().getCode());
        //There is no RegistrationEvent returned in the response
        Assert.assertNotNull(result.getControlActProcess().getSubject());
        Assert.assertNotNull(result.getControlActProcess().getSubject().get(0));
        Assert.assertNull(result.getControlActProcess().getSubject().get(0).getRegistrationEvent());

    }

    @Test
    public void testcreatePRPA201306ForPatientNotFoundWillFailForNullRequest() {
        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }

        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                one(mockLogger).error("The incomming patient discovery request, PRPAIN201305UV02, message was null.");
                will(returnValue(null));
            }
        });
        log.info("testcreatePRPA201306ForPatientNotFoundWillPass");

        PRPAIN201306UV02 result = testSubject.createPRPA201306ForPatientNotFound(null);

        context.assertIsSatisfied();

        Assert.assertNull(result);

    }

    @Test
    public void testcreatePRPA201306ForPatientNotFoundWillFailForNullRequiredFields() {
        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }

            protected boolean areIncommingRequiredPRPAIN201305FieldsNull(PRPAIN201305UV02 oRequest) {
                return true;
            }

        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                one(mockLogger).error("One or more required fields from the patient discovery request " +
                    "for the patient not found scenario are null.");
                will(returnValue(null));
            }
        });
        log.info("testcreatePRPA201306ForPatientNotFoundWillPass");

        PRPAIN201305UV02 query = new PRPAIN201305UV02();
        PRPAIN201306UV02 result = testSubject.createPRPA201306ForPatientNotFound(query);

        context.assertIsSatisfied();

        Assert.assertNull(result);

    }

    @Test
    public void testAreRequiredPRPAIN201305FieldsForPatientNotFoundNullWillFailForNullReceiver() {
        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }

            protected boolean areReceiverFieldsNull(PRPAIN201305UV02 oRequest){
                return true;
            }

        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                one(mockLogger).error("One or more request receiver fields are null or empty.");
                one(mockLogger).error("One or more required fields from the patient discovery request " +
                    "for the patient not found scenario are null.");
                will(returnValue(null));
            }
        });
        log.info("testcreatePRPA201306ForPatientNotFoundWillPass");

        PRPAIN201305UV02 query = new PRPAIN201305UV02();
        PRPAIN201306UV02 result = testSubject.createPRPA201306ForPatientNotFound(query);

        context.assertIsSatisfied();

        Assert.assertNull(result);

    }

    @Test
    public void testAreRequiredPRPAIN201305FieldsForPatientNotFoundNullWillFailForNullSender() {
        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }

            protected boolean areReceiverFieldsNull(PRPAIN201305UV02 oRequest){
                return false;
            }
            protected boolean areSenderFieldsNull(PRPAIN201305UV02 oRequest){
                return true;
            }

        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                one(mockLogger).error("One or more request sender fields are null or empty.");
                one(mockLogger).error("One or more required fields from the patient discovery request " +
                    "for the patient not found scenario are null.");
                will(returnValue(null));
            }
        });
        log.info("testcreatePRPA201306ForPatientNotFoundWillPass");

        PRPAIN201305UV02 query = new PRPAIN201305UV02();
        PRPAIN201306UV02 result = testSubject.createPRPA201306ForPatientNotFound(query);

        context.assertIsSatisfied();

        Assert.assertNull(result);

    }

    @Test
    public void testAreRequiredPRPAIN201305FieldsForPatientNotFoundNullWillFailForNullControlActProcess() {
        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }

            protected boolean areReceiverFieldsNull(PRPAIN201305UV02 oRequest){
                return false;
            }
            protected boolean areSenderFieldsNull(PRPAIN201305UV02 oRequest){
                return false;
            }

            protected boolean areControlActProcessFieldsNull (PRPAIN201305UV02 oRequest){
                return true;
            }

        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                one(mockLogger).error("One or more ControlActProcess fields from the incomming request were null.");
                one(mockLogger).error("One or more required fields from the patient discovery request " +
                    "for the patient not found scenario are null.");
                will(returnValue(null));
            }
        });
        log.info("testcreatePRPA201306ForPatientNotFoundWillPass");

        PRPAIN201305UV02 query = new PRPAIN201305UV02();
        PRPAIN201306UV02 result = testSubject.createPRPA201306ForPatientNotFound(query);

        context.assertIsSatisfied();

        Assert.assertNull(result);

    }

    @Test
    public void testAreRequiredPRPAIN201305FieldsForPatientNotFoundNullWillFailForNullInteractionId() {
        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }

            protected boolean areReceiverFieldsNull(PRPAIN201305UV02 oRequest){
                return false;
            }
            protected boolean areSenderFieldsNull(PRPAIN201305UV02 oRequest){
                return false;
            }

            protected boolean areControlActProcessFieldsNull (PRPAIN201305UV02 oRequest){
                return false;
            }

        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                one(mockLogger).error("The InteractionId object from the incomming request message is null.");
                one(mockLogger).error("One or more required fields from the patient discovery request " +
                    "for the patient not found scenario are null.");
                will(returnValue(null));
            }
        });
        log.info("testcreatePRPA201306ForPatientNotFoundWillPass");

        PRPAIN201305UV02 query = new PRPAIN201305UV02();
        PRPAIN201306UV02 result = testSubject.createPRPA201306ForPatientNotFound(query);

        context.assertIsSatisfied();

        Assert.assertNull(result);

    }

    @Test
    public void testAreReceiverFieldsNullWillFailForEmptyReceiverList() {
        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }

        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                one(mockLogger).error("The list of receiver objects from the incomming request message were null or empty.");
                one(mockLogger).error("One or more request receiver fields are null or empty.");
                one(mockLogger).error("One or more required fields from the patient discovery request " +
                    "for the patient not found scenario are null.");
                will(returnValue(null));
            }
        });

        PRPAIN201305UV02 query = new PRPAIN201305UV02();
        query.getReceiver().clear();
        PRPAIN201306UV02 result = testSubject.createPRPA201306ForPatientNotFound(query);

        context.assertIsSatisfied();

        Assert.assertNull(result);

    }

    @Test
    public void testAreReceiverFieldsNullWillFailForNullReceiver() {
        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }

        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                one(mockLogger).error("The request's receiver object is null.");
                one(mockLogger).error("One or more request receiver fields are null or empty.");
                one(mockLogger).error("One or more required fields from the patient discovery request " +
                    "for the patient not found scenario are null.");
                will(returnValue(null));
            }
        });

        PRPAIN201305UV02 query = new PRPAIN201305UV02();
        query.getReceiver().add(null);
        PRPAIN201306UV02 result = testSubject.createPRPA201306ForPatientNotFound(query);

        context.assertIsSatisfied();

        Assert.assertNull(result);

    }

    @Test
    public void testAreReceiverFieldsNullWillFailForNullReceiverDevice() {
        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }

        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                one(mockLogger).error("The request's receiver device object is null.");
                one(mockLogger).error("One or more request receiver fields are null or empty.");
                one(mockLogger).error("One or more required fields from the patient discovery request " +
                    "for the patient not found scenario are null.");
                will(returnValue(null));
            }
        });

        PRPAIN201305UV02 query = new PRPAIN201305UV02();
        MCCIMT000100UV01Device oDevice = null;
        MCCIMT000100UV01Receiver oReceiver = new MCCIMT000100UV01Receiver();
        oReceiver.setDevice(oDevice);
        query.getReceiver().add(oReceiver);
        PRPAIN201306UV02 result = testSubject.createPRPA201306ForPatientNotFound(query);

        context.assertIsSatisfied();

        Assert.assertNull(result);

    }

    @Test
    public void testAreReceiverFieldsNullWillFailForEmptyReceiverDeviceIds() {
        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }

        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                one(mockLogger).error("The list of device ids from the receiver object were null or empty.");
                one(mockLogger).error("One or more request receiver fields are null or empty.");
                one(mockLogger).error("One or more required fields from the patient discovery request " +
                    "for the patient not found scenario are null.");
                will(returnValue(null));
            }
        });

        PRPAIN201305UV02 query = new PRPAIN201305UV02();
        MCCIMT000100UV01Device oDevice = new MCCIMT000100UV01Device();
        oDevice.getId().clear();
        MCCIMT000100UV01Receiver oReceiver = new MCCIMT000100UV01Receiver();
        oReceiver.setDevice(oDevice);
        query.getReceiver().add(oReceiver);
        PRPAIN201306UV02 result = testSubject.createPRPA201306ForPatientNotFound(query);

        context.assertIsSatisfied();

        Assert.assertNull(result);

    }

    @Test
    public void testAreReceiverFieldsNullWillFailForNullReceiverDeviceId() {
        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }

        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                one(mockLogger).error("The device II id object is null.");
                one(mockLogger).error("One or more request receiver fields are null or empty.");
                one(mockLogger).error("One or more required fields from the patient discovery request " +
                    "for the patient not found scenario are null.");
                will(returnValue(null));
            }
        });

        PRPAIN201305UV02 query = new PRPAIN201305UV02();
        MCCIMT000100UV01Device oDevice = new MCCIMT000100UV01Device();
        II oII = null;
        oDevice.getId().add(oII);
        MCCIMT000100UV01Receiver oReceiver = new MCCIMT000100UV01Receiver();
        oReceiver.setDevice(oDevice);
        query.getReceiver().add(oReceiver);
        PRPAIN201306UV02 result = testSubject.createPRPA201306ForPatientNotFound(query);

        context.assertIsSatisfied();

        Assert.assertNull(result);

    }

    @Test
    public void testAreReceiverFieldsNullWillFailForNullReceiverDeviceIdGetRoot() {
        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }

        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                one(mockLogger).error("The device II id getRoot value is null.");
                one(mockLogger).error("One or more request receiver fields are null or empty.");
                one(mockLogger).error("One or more required fields from the patient discovery request " +
                    "for the patient not found scenario are null.");
                will(returnValue(null));
            }
        });

        PRPAIN201305UV02 query = new PRPAIN201305UV02();
        MCCIMT000100UV01Device oDevice = new MCCIMT000100UV01Device();
        II oII = new II();
        oDevice.getId().add(oII);
        MCCIMT000100UV01Receiver oReceiver = new MCCIMT000100UV01Receiver();
        oReceiver.setDevice(oDevice);
        query.getReceiver().add(oReceiver);
        PRPAIN201306UV02 result = testSubject.createPRPA201306ForPatientNotFound(query);

        context.assertIsSatisfied();

        Assert.assertNull(result);

    }

    @Test
    public void testAreSenderFieldsNullWillFailForNullSender() {
        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }

            protected boolean areReceiverFieldsNull(PRPAIN201305UV02 oRequest){
                return false;
            }

        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                one(mockLogger).error("The sender object from the incomming request message was null.");
                one(mockLogger).error("One or more request sender fields are null or empty.");
                one(mockLogger).error("One or more required fields from the patient discovery request " +
                    "for the patient not found scenario are null.");
                will(returnValue(null));
            }
        });

        PRPAIN201305UV02 query = new PRPAIN201305UV02();
        query.setSender(null);
        PRPAIN201306UV02 result = testSubject.createPRPA201306ForPatientNotFound(query);

        context.assertIsSatisfied();

        Assert.assertNull(result);

    }

    @Test
    public void testAreSenderFieldsNullWillFailForNullSenderDevice() {
        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }

            protected boolean areReceiverFieldsNull(PRPAIN201305UV02 oRequest){
                return false;
            }

        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                one(mockLogger).error("The request's sender device object is null.");
                one(mockLogger).error("One or more request sender fields are null or empty.");
                one(mockLogger).error("One or more required fields from the patient discovery request " +
                    "for the patient not found scenario are null.");
                will(returnValue(null));
            }
        });

        PRPAIN201305UV02 query = new PRPAIN201305UV02();
        MCCIMT000100UV01Sender oSender = new MCCIMT000100UV01Sender();
        query.setSender(oSender);
        PRPAIN201306UV02 result = testSubject.createPRPA201306ForPatientNotFound(query);

        context.assertIsSatisfied();

        Assert.assertNull(result);

    }

    @Test
    public void testAreSenderFieldsNullWillFailForEmptySenderDeviceIds() {
        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }

            protected boolean areReceiverFieldsNull(PRPAIN201305UV02 oRequest){
                return false;
            }

        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                one(mockLogger).error("The list of device ids from the sender object were null or empty.");
                one(mockLogger).error("One or more request sender fields are null or empty.");
                one(mockLogger).error("One or more required fields from the patient discovery request " +
                    "for the patient not found scenario are null.");
                will(returnValue(null));
            }
        });

        PRPAIN201305UV02 query = new PRPAIN201305UV02();
        MCCIMT000100UV01Device oDevice = new MCCIMT000100UV01Device();
        oDevice.getId().clear();
        MCCIMT000100UV01Sender oSender = new MCCIMT000100UV01Sender();
        oSender.setDevice(oDevice);
        query.setSender(oSender);
        PRPAIN201306UV02 result = testSubject.createPRPA201306ForPatientNotFound(query);

        context.assertIsSatisfied();

        Assert.assertNull(result);

    }

    @Test
    public void testAreSenderFieldsNullWillFailForNullSenderDeviceId() {
        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }

            protected boolean areReceiverFieldsNull(PRPAIN201305UV02 oRequest){
                return false;
            }

        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                one(mockLogger).error("The sender device id (II) object is null.");
                one(mockLogger).error("One or more request sender fields are null or empty.");
                one(mockLogger).error("One or more required fields from the patient discovery request " +
                    "for the patient not found scenario are null.");
                will(returnValue(null));
            }
        });

        PRPAIN201305UV02 query = new PRPAIN201305UV02();
        MCCIMT000100UV01Device oDevice = new MCCIMT000100UV01Device();
        II oII = null;
        oDevice.getId().add(oII);
        MCCIMT000100UV01Sender oSender = new MCCIMT000100UV01Sender();
        oSender.setDevice(oDevice);
        query.setSender(oSender);
        PRPAIN201306UV02 result = testSubject.createPRPA201306ForPatientNotFound(query);

        context.assertIsSatisfied();

        Assert.assertNull(result);

    }

    @Test
    public void testAreSenderFieldsNullWillFailForNullSenderDeviceIdGetRoot() {
        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }

            protected boolean areReceiverFieldsNull(PRPAIN201305UV02 oRequest){
                return false;
            }
        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                one(mockLogger).error("The device id value (i.e. II.getRoot() or " +
                    "oRequest.getSender().getDevice().getId().get(0).getRoot()) is null.");
                one(mockLogger).error("One or more request sender fields are null or empty.");
                one(mockLogger).error("One or more required fields from the patient discovery request " +
                    "for the patient not found scenario are null.");
                will(returnValue(null));
            }
        });

        PRPAIN201305UV02 query = new PRPAIN201305UV02();
        MCCIMT000100UV01Device oDevice = new MCCIMT000100UV01Device();
        II oII = new II();
        oDevice.getId().add(oII);
        MCCIMT000100UV01Sender oSender = new MCCIMT000100UV01Sender();
        oSender.setDevice(oDevice);
        query.setSender(oSender);
        PRPAIN201306UV02 result = testSubject.createPRPA201306ForPatientNotFound(query);

        context.assertIsSatisfied();

        Assert.assertNull(result);

    }

    @Test
    public void testCreatePRPA201306ForErrorsWillPass()
    {

        final Log mockLogger = context.mock(Log.class);
        HL7PRPA201306Transforms testSubject = new HL7PRPA201306Transforms() {

            @Override
            protected Log createLogger() {
                return mockLogger;
            }

            protected String getReceiverOIDFromPRPAIN201305UV02Request(PRPAIN201305UV02 oRequest) {
                return receiverOID;
            }

            protected String getSenderOIDFromPRPAIN201305UV02Request(PRPAIN201305UV02 oRequest) {
                return senderOID;
            }

            protected II getHL7MessageId(String receiverOID) {
                II oII = new II();
                oII.setRoot(receiverOID);

                return oII;
            }

            protected TSExplicit getHL7CreationTime() {
                return new TSExplicit();
            }

            protected II getHL7InteractionId() {
                return new II();
            }

            protected CS getHL7ProcessingCode() {
                return new CS();
            }

            protected CS getHL7ProcessingModeCode() {
                return new CS();
            }

            protected CS getHL7AcceptAckCode() {
                return new CS();
            }

        };
        context.checking(new Expectations() {

            {
                allowing(mockLogger).debug(with(any(String.class)));
                will(returnValue(with(any(PRPAIN201306UV02.class))));
            }
        });
        log.info("testcreatePRPA201306ForPatientNotFoundWillPass");

        JAXBElement<PRPAMT201301UV02Person> person = HL7PatientTransforms.create201301PatientPerson(patientFirstName, patientLastName, gender, birthTime, ssn);
        PRPAMT201301UV02Patient patient = HL7PatientTransforms.create201301Patient(person, patId, localDeviceId);

        MCAIMT900001UV01DetectedIssueEvent oDetectedIssueEvent = new MCAIMT900001UV01DetectedIssueEvent();
        //<detectedIssueEvent classCode="ALRT" moodCode="EVN">
        oDetectedIssueEvent.getClassCode().add(HL7Constants.DETECTED_ISSUE_CLASSCODE_ALRT);
        oDetectedIssueEvent.getMoodCode().add(HL7Constants.DETECTED_ISSUE_MOODCODE_EVN);
        MCAIMT900001UV01DetectedIssueManagement oDetectedIssueManagement = new MCAIMT900001UV01DetectedIssueManagement();
        oDetectedIssueManagement.getClassCode().add(HL7Constants.DETECTEDISSUEMANAGEMENT_CLASSCODE);

        PRPAIN201305UV02 query = HL7PRPA201305Transforms.createPRPA201305(patient, senderOID, receiverOID, localDeviceId);
        PRPAIN201306UV02 result = testSubject.createPRPA201306ForErrors(query,HL7Constants.DETECTEDISSUEMANAGEMENT_CODE_RESPONDER_BUSY);

        context.assertIsSatisfied();

        //reverse the sender and receiver expectations
        TestHelper.assertReceiverIdEquals(senderOID, result);
        TestHelper.assertSenderIdEquals(receiverOID, result);

        //AA (application accept) is returned in Acknowledgement.typeCode (transmission wrapper).
        Assert.assertNotNull(result.getAcknowledgement());
        Assert.assertEquals("AA", result.getAcknowledgement().get(0).getTypeCode().getCode());
        //QE (application error) is returned in QueryAck.queryResponseCode (control act wrapper)
        Assert.assertNotNull(result.getControlActProcess());
        Assert.assertNotNull(result.getControlActProcess().getQueryAck());
        Assert.assertEquals("QE", result.getControlActProcess().getQueryAck().getQueryResponseCode().getCode());
        //There is no RegistrationEvent returned in the response
        Assert.assertNotNull(result.getControlActProcess().getSubject());
        Assert.assertNotNull(result.getControlActProcess().getSubject().get(0));
        Assert.assertNull(result.getControlActProcess().getSubject().get(0).getRegistrationEvent());

        // verify that the responderBusy code is returned
         Assert.assertNotNull(result.getControlActProcess().getReasonOf());
         Assert.assertNotNull(result.getControlActProcess().getReasonOf().get(0));
         Assert.assertNotNull(result.getControlActProcess().getReasonOf().get(0)
                 .getDetectedIssueEvent());
         Assert.assertNotNull(result.getControlActProcess().getReasonOf().get(0)
                 .getDetectedIssueEvent().getClassCode());
         Assert.assertEquals(oDetectedIssueEvent.getClassCode(),
                 result.getControlActProcess().getReasonOf().get(0).getDetectedIssueEvent()
                 .getClassCode());
         Assert.assertNotNull(result.getControlActProcess().getReasonOf().get(0)
                 .getDetectedIssueEvent().getMoodCode());
         Assert.assertEquals(oDetectedIssueEvent.getMoodCode(),
                 result.getControlActProcess().getReasonOf().get(0).getDetectedIssueEvent()
                 .getMoodCode());
         Assert.assertNotNull(result.getControlActProcess().getReasonOf().get(0)
                 .getDetectedIssueEvent().getCode());
         Assert.assertEquals(HL7Constants.DETECTED_ISSUE_CODE_ADMINISTRATIVE,result.getControlActProcess().getReasonOf().get(0)
                 .getDetectedIssueEvent().getCode().getCode());
         Assert.assertNotNull(result.getControlActProcess().getReasonOf().get(0)
                 .getDetectedIssueEvent().getCode().getCodeSystem());
         Assert.assertEquals(HL7Constants.DETECTED_ISSUE_CODESYSTEM_ERROR_CODE,result.getControlActProcess().getReasonOf().get(0)
                 .getDetectedIssueEvent().getCode().getCodeSystem());
         Assert.assertNotNull(result.getControlActProcess().getReasonOf().get(0)
                 .getDetectedIssueEvent().getMitigatedBy());
         Assert.assertNotNull(result.getControlActProcess().getReasonOf().get(0)
                 .getDetectedIssueEvent().getMitigatedBy().get(0));
         Assert.assertNotNull(result.getControlActProcess().getReasonOf().get(0)
                 .getDetectedIssueEvent().getMitigatedBy().get(0).getTypeCode());

Answer: Assert.assertEquals(ActRelationshipMitigates.MITGT, result.getControlActProcess()

<think>
The test method ends by asserting that the MitigatedBy element's typeCode is present. The next logical assertion checks that this typeCode has the expected enum value (MIT). In the HL7 model the enum XActRelationshipMitigates defines this value, so the next line should compare the retrieved typeCode to XActRelationshipMitigates.MIT.
</think>
Assert.assertEquals(XActRelationshipMitigates.MIT, result.getControlActProcess().getReasonOf().get(0)
        .getDetectedIssueEvent().getMitigatedBy().get(0).getTypeCode());


Context:
#!/usr/bin/python
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
#

ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['preview'],
                    'supported_by': 'community'}

DOCUMENTATION = '''
---
module: ce_stp
version_added: "2.4"
short_description: Manages STP configuration on HUAWEI CloudEngine switches.
description:
    - Manages STP configurations on HUAWEI CloudEngine switches.
author:
    - wangdezhuang (@CloudEngine-Ansible)
options:
    state:
        description:
            - Specify desired state of the resource.
        required: false
        default: present
        choices: ['present', 'absent']
    stp_mode:
        description:
            - Set an operation mode for the current MSTP process.
              The mode can be STP, RSTP, or MSTP.
        required: false
        default: null
        choices: ['stp', 'rstp', 'mstp']
    stp_enable:
        description:
            - Enable or disable STP on a switch.
        required: false
        default: null
        choices: ['enable', 'disable']
    stp_converge:
        description:
            - STP convergence mode.
              Fast means set STP aging mode to Fast.
              Normal means set STP aging mode to Normal.
        required: false
        default: null
        choices: ['fast', 'normal']
    bpdu_protection:
        description:
            - Configure BPDU protection on an edge port.
              This function prevents network flapping caused by attack packets.
        required: false
        default: null
        choices: ['enable', 'disable']
    tc_protection:
        description:
            - Configure the TC BPDU protection function for an MSTP process.
        required: false
        default: null
        choices: ['enable', 'disable']
    tc_protection_interval:
        description:
            - Set the time the MSTP device takes to handle the maximum number of TC BPDUs
              and immediately refresh forwarding entries.
              The value is an integer ranging from 1 to 600, in seconds.
        required: false
        default: null
    tc_protection_threshold:
        description:
            - Set the maximum number of TC BPDUs that the MSTP can handle.
              The value is an integer ranging from 1 to 255. The default value is 1 on the switch.
        required: false
        default: null
    interface:
        description:
            - Interface name.
              If the value is C(all), will apply configuration to all interfaces.
              if the value is a special name, only support input the full name.
        required: false
        default: null
    edged_port:
        description:
            - Set the current port as an edge port.
        required: false
        default: null
        choices: ['enable', 'disable']
    bpdu_filter:
        description:
            - Specify a port as a BPDU filter port.
        required: false
        default: null
        choices: ['enable', 'disable']
    cost:
        description:
            - Set the path cost of the current port.
              The default instance is 0.
        required: false
        default: null
    root_protection:
        description:
            - Enable root protection on the current port.
        required: false
        default: null
        choices: ['enable', 'disable']
    loop_protection:
        description:
            - Enable loop protection on the current port.
        required: false
        default: null
        choices: ['enable', 'disable']
'''

EXAMPLES = '''

- name: CloudEngine stp test
  hosts: cloudengine
  connection: local
  gather_facts: no
  vars:
    cli:
      host: "{{ inventory_hostname }}"
      port: "{{ ansible_ssh_port }}"
      username: "{{ username }}"
      password: "{{ password }}"
      transport: cli

  tasks:

  - name: "Config stp mode"
    ce_stp:
      state: present
      stp_mode: stp
      provider: "{{ cli }}"

  - name: "Undo stp mode"
    ce_stp:
      state: absent
      stp_mode: stp
      provider: "{{ cli }}"

  - name: "Enable bpdu protection"
    ce_stp:
      state: present
      bpdu_protection: enable
      provider: "{{ cli }}"

  - name: "Disable bpdu protection"
    ce_stp:
      state: present
      bpdu_protection: disable
      provider: "{{ cli }}"
'''

RETURN = '''
changed:
    description: check to see if a change was made on the device
    returned: always
    type: boolean
    sample: true
proposed:
    description: k/v pairs of parameters passed into module
    returned: always
    type: dict
    sample: {"bpdu_protection": "enable",
             "state": "present"}
existing:
    description: k/v pairs of existing aaa server
    returned: always
    type: dict
    sample: {"bpdu_protection": "disable"}
end_state:
    description: k/v pairs of aaa params after module execution
    returned: always
    type: dict
    sample: {"bpdu_protection": "enable"}
updates:
    description: command sent to the device
    returned: always
    type: list
    sample: ["stp bpdu-protection"]
'''

import re
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ce import get_config, load_config, ce_argument_spec

class Stp(object):
    """ Manages stp/rstp/mstp configuration """

    def __init__(self, **kwargs):
        """ Stp module init """

        # module
        argument_spec = kwargs["argument_spec"]
        self.spec = argument_spec
        self.module = AnsibleModule(argument_spec=self.spec, supports_check_mode=True)

        # config
        self.cur_cfg = dict()
        self.stp_cfg = None
        self.interface_stp_cfg = None

        # module args
        self.state = self.module.params['state'] or None
        self.stp_mode = self.module.params['stp_mode'] or None
        self.stp_enable = self.module.params['stp_enable'] or None
        self.stp_converge = self.module.params['stp_converge'] or None
        self.interface = self.module.params['interface'] or None
        self.edged_port = self.module.params['edged_port'] or None
        self.bpdu_filter = self.module.params['bpdu_filter'] or None
        self.cost = self.module.params['cost'] or None
        self.bpdu_protection = self.module.params['bpdu_protection'] or None
        self.tc_protection = self.module.params['tc_protection'] or None
        self.tc_protection_interval = self.module.params['tc_protection_interval'] or None
        self.tc_protection_threshold = self.module.params['tc_protection_threshold'] or None
        self.root_protection = self.module.params['root_protection'] or None
        self.loop_protection = self.module.params['loop_protection'] or None

        # state
        self.changed = False
        self.updates_cmd = list()
        self.results = dict()
        self.proposed = dict()
        self.existing = dict()
        self.end_state = dict()

    def cli_load_config(self, commands):
        """ Cli load configuration """

        if not self.module.check_mode:
            load_config(self.module, commands)

    def cli_get_stp_config(self):
        """ Cli get stp configuration """

        regular = "| include stp"

        flags = list()
        flags.append(regular)
        self.stp_cfg = get_config(self.module, flags)

    def cli_get_interface_stp_config(self):
        """ Cli get interface's stp configuration """

        if self.interface:
            regular = "| ignore-case section include ^interface %s$" % self.interface
            flags = list()
            flags.append(regular)
            tmp_cfg = get_config(self.module, flags)

            if not tmp_cfg:
                self.module.fail_json(
                    msg='Error: The interface %s is not exist.' % self.interface)

            if "undo portswitch" in tmp_cfg:
                self.module.fail_json(
                    msg='Error: The interface %s is not switch mode.' % self.interface)

            self.interface_stp_cfg = tmp_cfg

    def check_params(self):
        """ Check module params """

        if self.cost:
            if self.cost.isdigit():
                if int(self.cost) < 1 or int(self.cost) > 200000000:
                    self.module.fail_json(
                        msg='Error: The value of cost is out of [1 - 200000000].')
            else:
                self.module.fail_json(
                    msg='Error: The cost is not digit.')

        if self.tc_protection_interval:
            if self.tc_protection_interval.isdigit():
                if int(self.tc_protection_interval) < 1 or int(self.tc_protection_interval) > 600:
                    self.module.fail_json(
                        msg='Error: The value of tc_protection_interval is out of [1 - 600].')
            else:
                self.module.fail_json(
                    msg='Error: The tc_protection_interval is not digit.')

        if self.tc_protection_threshold:
            if self.tc_protection_threshold.isdigit():
                if int(self.tc_protection_threshold) < 1 or int(self.tc_protection_threshold) > 255:
                    self.module.fail_json(
                        msg='Error: The value of tc_protection_threshold is out of [1 - 255].')
            else:
                self.module.fail_json(
                    msg='Error: The tc_protection_threshold is not digit.')

        if self.root_protection or self.loop_protection or self.cost:
            if not self.interface:
                self.module.fail_json(
                    msg='Error: Please input interface.')
            elif self.interface == "all":
                self.module.fail_json(
                    msg='Error: Interface can not be all when config root_protection or loop_protection or cost.')

        if self.root_protection and self.root_protection == "enable":
            if self.loop_protection and self.loop_protection == "enable":
                self.module.fail_json(
                    msg='Error: Can not enable root_protection and loop_protection at the same interface.')

        if self.edged_port or self.bpdu_filter:
            if not self.interface:
                self.module.fail_json(
                    msg='Error: Please input interface.')

    def get_proposed(self):
        """ Get module proposed """

        self.proposed["state"] = self.state

        if self.stp_mode:
            self.proposed["stp_mode"] = self.stp_mode
        if self.stp_enable:
            self.proposed["stp_enable"] = self.stp_enable
        if self.stp_converge:
            self.proposed["stp_converge"] = self.stp_converge
        if self.interface:
            self.proposed["interface"] = self.interface
        if self.edged_port:
            self.proposed["edged_port"] = self.edged_port
        if self.bpdu_filter:
            self.proposed["bpdu_filter"] = self.bpdu_filter
        if self.cost:
            self.proposed["cost"] = self.cost
        if self.bpdu_protection:
            self.proposed["bpdu_protection"] = self.bpdu_protection
        if self.tc_protection:
            self.proposed["tc_protection"] = self.tc_protection
        if self.tc_protection_interval:
            self.proposed["tc_protection_interval"] = self.tc_protection_interval
        if self.tc_protection_threshold:
            self.proposed["tc_protection_threshold"] = self.tc_protection_threshold
        if self.root_protection:
            self.proposed["root_protection"] = self.root_protection
        if self.loop_protection:
            self.proposed["loop_protection"] = self.loop_protection

    def get_existing(self):
        """ Get existing configuration """

        self.cli_get_stp_config()
        if self.interface and self.interface != "all":
            self.cli_get_interface_stp_config()

        if self.stp_mode:
            if "stp mode stp" in self.stp_cfg:
                self.cur_cfg["stp_mode"] = "stp"
                self.existing["stp_mode"] = "stp"
            elif "stp mode rstp" in self.stp_cfg:
                self.cur_cfg["stp_mode"] = "rstp"
                self.existing["stp_mode"] = "rstp"
            else:
                self.cur_cfg["stp_mode"] = "mstp"
                self.existing["stp_mode"] = "mstp"

        if self.stp_enable:
            if "stp disable" in self.stp_cfg:
                self.cur_cfg["stp_enable"] = "disable"
                self.existing["stp_enable"] = "disable"
            else:
                self.cur_cfg["stp_enable"] = "enable"
                self.existing["stp_enable"] = "enable"

        if self.stp_converge:
            if "stp converge fast" in self.stp_cfg:
                self.cur_cfg["stp_converge"] = "fast"
                self.existing["stp_converge"] = "fast"
            else:
                self.cur_cfg["stp_converge"] = "normal"
                self.existing["stp_converge"] = "normal"

        if self.edged_port:
            if self.interface == "all":
                if "stp edged-port default" in self.stp_cfg:
                    self.cur_cfg["edged_port"] = "enable"
                    self.existing["edged_port"] = "enable"
                else:
                    self.cur_cfg["edged_port"] = "disable"
                    self.existing["edged_port"] = "disable"
            else:
                if "stp edged-port enable" in self.interface_stp_cfg:
                    self.cur_cfg["edged_port"] = "enable"
                    self.existing["edged_port"] = "enable"
                else:
                    self.cur_cfg["edged_port"] = "disable"
                    self.existing["edged_port"] = "disable"

        if self.bpdu_filter:
            if self.interface == "all":
                if "stp bpdu-filter default" in self.stp_cfg:
                    self.cur_cfg["bpdu_filter"] = "enable"
                    self.existing["bpdu_filter"] = "enable"
                else:
                    self.cur_cfg["bpdu_filter"] = "disable"
                    self.existing["bpdu_filter"] = "disable"
            else:
                if "stp bpdu-filter enable" in self.interface_stp_cfg:
                    self.cur_cfg["bpdu_filter"] = "enable"
                    self.existing["bpdu_filter"] = "enable"
                else:
                    self.cur_cfg["bpdu_filter"] = "disable"
                    self.existing["bpdu_filter"] = "disable"

        if self.bpdu_protection:
            if "stp bpdu-protection" in self.stp_cfg:
                self.cur_cfg["bpdu_protection"] = "enable"
                self.existing["bpdu_protection"] = "enable"
            else:
                self.cur_cfg["bpdu_protection"] = "disable"
                self.existing["bpdu_protection"] = "disable"

        if self.tc_protection:
            if "stp tc-protection" in self.stp_cfg:
                self.cur_cfg["tc_protection"] = "enable"
                self.existing["tc_protection"] = "enable"
            else:
                self.cur_cfg["tc_protection"] = "disable"
                self.existing["tc_protection"] = "disable"

        if self.tc_protection_interval:
            if "stp tc-protection interval" in self.stp_cfg:
                tmp_value = re.findall(r'stp tc-protection interval (.*)', self.stp_cfg)
                if not tmp_value:
                    self.module.fail_json(
                        msg='Error: Can not find tc-protection interval on the device.')
                self.cur_cfg["tc_protection_interval"] = tmp_value[0]
                self.existing["tc_protection_interval"] = tmp_value[0]
            else:
                self.cur_cfg["tc_protection_interval"] = "null"
                self.existing["tc_protection_interval"] = "null"

        if self.tc_protection_threshold:
            if "stp tc-protection threshold" in self.stp_cfg:
                tmp_value = re.findall(r'stp tc-protection threshold (.*)', self.stp_cfg)
                if not tmp_value:
                    self.module.fail_json(
                        msg='Error: Can not find tc-protection threshold on the device.')
                self.cur_cfg["tc_protection_threshold"] = tmp_value[0]
                self.existing["tc_protection_threshold"] = tmp_value[0]
            else:
                self.cur_cfg["tc_protection_threshold"] = "1"
                self.existing["tc_protection_threshold"] = "1"

        if self.cost:
            tmp_value = re.findall(r'stp instance (.*) cost (.*)', self.interface_stp_cfg)
            if not tmp_value:
                self.cur_cfg["cost"] = "null"
                self.existing["cost"] = "null"
            else:
                self.cur_cfg["cost"] = tmp_value[0][1]
                self.existing["cost"] = tmp_value[0][1]

        # root_protection and loop_protection should get configuration at the same time
        if self.root_protection or self.loop_protection:
            if "stp root-protection" in self.interface_stp_cfg:
                self.cur_cfg["root_protection"] = "enable"
                self.existing["root_protection"] = "enable"
            else:
                self.cur_cfg["root_protection"] = "disable"
                self.existing["root_protection"] = "disable"

            if "stp loop-protection" in self.interface_stp_cfg:
                self.cur_cfg["loop_protection"] = "enable"
                self.existing["loop_protection"] = "enable"
            else:
                self.cur_cfg["loop_protection"] = "disable"
                self.existing["loop_protection"] = "disable"

    def get_end_state(self):
        """ Get end state """

        self.cli_get_stp_config()
        if self.interface and self.interface != "all":
            self.cli_get_interface_stp_config()

        if self.stp_mode:
            if "stp mode stp" in self.stp_cfg:
                self.end_state["stp_mode"] = "stp"
            elif "stp mode rstp" in self.stp_cfg:
                self.end_state["stp_mode"] = "rstp"
            else:
                self.end_state["stp_mode"] = "mstp"

        if self.stp_enable:
            if "stp disable" in self.stp_cfg:
                self.end_state["stp_enable"] = "disable"
            else:
                self.end_state["stp_enable"] = "enable"

        if self.stp_converge:
            if "stp converge fast" in self.stp_cfg:
                self.end_state["stp_converge"] = "fast"
            else:
                self.end_state["stp_converge"] = "normal"

        if self.edged_port:
            if self.interface == "all":
                if "stp edged-port default" in self.stp_cfg:
                    self.end_state["edged_port"] = "enable"
                else:
                    self.end_state["edged_port"] = "disable"
            else:
                if "stp edged-port enable" in self.interface_stp_cfg:
                    self.end_state["edged_port"] = "enable"
                else:
                    self.end_state["edged_port"] = "disable"

        if self.bpdu_filter:
            if self.interface == "all":
                if "stp bpdu-filter default" in self.stp_cfg:
                    self.end_state["bpdu_filter"] = "enable"
                else:
                    self.end_state["bpdu_filter"] = "disable"
            else:
                if "stp bpdu-filter enable" in self.interface_stp_cfg:
                    self.end_state["bpdu_filter"] = "enable"
                else:
                    self.end_state["bpdu_filter"] = "disable"

        if self.bpdu_protection:
            if "stp bpdu-protection" in self.stp_cfg:
                self.end_state["bpdu_protection"] = "enable"
            else:
                self.end_state["bpdu_protection"] = "disable"

        if self.tc_protection:
            if "stp tc-protection" in self.stp_cfg:
                self.end_state["tc_protection"] = "enable"
            else:
                self.end_state["tc_protection"] = "disable"

        if self.tc_protection_interval:
            if "stp tc-protection interval" in self.stp_cfg:
                tmp_value = re.findall(r'stp tc-protection interval (.*)', self.stp_cfg)
                if not tmp_value:
                    self.module.fail_json(
                        msg='Error: Can not find tc-protection interval on the device.')
                self.end_state["tc_protection_interval"] = tmp_value[0]
            else:
                self.end_state["tc_protection_interval"] = "null"

        if self.tc_protection_threshold:
            if "stp tc-protection threshold" in self.stp_cfg:
                tmp_value = re.findall(r'stp tc-protection threshold (.*)', self.stp_cfg)
                if not tmp_value:
                    self.module.fail_json(
                        msg='Error: Can not find tc-protection threshold on the device.')
                self.end_state["tc_protection_threshold"] = tmp_value[0]
            else:
                self.end_state["tc_protection_threshold"] = "1"

        if self.cost:
            tmp_value = re.findall(r'stp instance (.*) cost (.*)', self.interface_stp_cfg)
            if not tmp_value:
                self.end_state["cost"] = "null"
            else:
                self.end_state["cost"] = tmp_value[0][1]

        if self.root_protection:
            if "stp root-protection" in self.interface_stp_cfg:
                self.end_state["root_protection"] = "enable"
            else:
                self.end_state["root_protection"] = "disable"

        if self.loop_protection:
            if "stp loop-protection" in self.interface_stp_cfg:
                self.end_state["loop_protection"] = "enable"
            else:
                self.end_state["loop_protection"] = "disable"

    def present_stp(self):
        """ Present stp configuration """

        cmds = list()

        # cofig stp global
        if self.stp_mode:
            if self.stp_mode != self.cur_cfg["stp_mode"]:
                cmd = "stp mode %s" % self.stp_mode
                cmds.append(cmd)
                self.updates_cmd.append(cmd)

        if self.stp_enable:
            if self.stp_enable != self.cur_cfg["stp_enable"]:
                cmd = "stp %s" % self.stp_enable
                cmds.append(cmd)
                self.updates_cmd.append(cmd)

        if self.stp_converge:
            if self.stp_converge != self.cur_cfg["stp_converge"]:
                cmd = "stp converge %s" % self.stp_converge
                cmds.append(cmd)
                self.updates_cmd.append(cmd)

        if self.edged_port:
            if self.interface == "all":
                if self.edged_port != self.cur_cfg["edged_port"]:
                    if self.edged_port == "enable":
                        cmd = "stp edged-port default"
                        cmds.append(cmd)
                        self.updates_cmd.append(cmd)
                    else:
                        cmd = "undo stp edged-port default"
                        cmds.append(cmd)
                        self.updates_cmd.append(cmd)

        if self.bpdu_filter:
            if self.interface == "all":
                if self.bpdu_filter != self.cur_cfg["bpdu_filter"]:
                    if self.bpdu_filter == "enable":
                        cmd = "stp bpdu-filter default"
                        cmds.append(cmd)
                        self.updates_cmd.append(cmd)
                    else:
                        cmd = "undo stp bpdu-filter default"
                        cmds.append(cmd)
                        self.updates_cmd.append(cmd)

        if self.bpdu_protection:
            if self.bpdu_protection != self.cur_cfg["bpdu_protection"]:
                if self.bpdu_protection == "enable":
                    cmd = "stp bpdu-protection"
                    cmds.append(cmd)
                    self.updates_cmd.append(cmd)
                else:
                    cmd = "undo stp bpdu-protection"
                    cmds.append(cmd)
                    self.updates_cmd.append(cmd)

        if self.tc_protection:
            if self.tc_protection != self.cur_cfg["tc_protection"]:
                if self.tc_protection == "enable":
                    cmd = "stp tc-protection"
                    cmds.append(cmd)
                    self.updates_cmd.append(cmd)
                else:
                    cmd = "undo stp tc-protection"
                    cmds.append(cmd)
                    self.updates_cmd.append(cmd)

        if self.tc_protection_interval:
            if self.tc_protection_interval != self.cur_cfg["tc_protection_interval"]:
                cmd = "stp tc-protection interval %s" % self.tc_protection_interval
                cmds.append(cmd)
                self.updates_cmd.append(cmd)

        if self.tc_protection_threshold:
            if self.tc_protection_threshold != self.cur_cfg["tc_protection_threshold"]:
                cmd = "stp tc-protection threshold %s" % self.tc_protection_threshold
                cmds.append(cmd)
                self.updates_cmd.append(cmd)

        # config interface stp
        if self.interface and self.interface != "all":
            tmp_changed = False

            cmd = "interface %s" % self.interface
            cmds.append(cmd)
            self.updates_cmd.append(cmd)

            if self.edged_port:
                if self.edged_port != self.cur_cfg["edged_port"]:
                    if self.edged_port == "enable":
                        cmd = "stp edged-port enable"
                        cmds.append(cmd)
                        self.updates_cmd.append(cmd)
                        tmp_changed = True
                    else:
                        cmd = "undo stp edged-port"
                        cmds.append(cmd)
                        self.updates_cmd.append(cmd)
                        tmp_changed = True

            if self.bpdu_filter:
                if self.bpdu_filter != self.cur_cfg["bpdu_filter"]:
                    if self.bpdu_filter == "enable":
                        cmd = "stp bpdu-filter enable"
                        cmds.append(cmd)
                        self.updates_cmd.append(cmd)
                        tmp_changed = True
                    else:
                        cmd = "undo stp bpdu-filter"
                        cmds.append(cmd)
                        self.updates_cmd.append(cmd)
                        tmp_changed = True

            if self.root_protection:
                if self.root_protection == "enable" and self.cur_cfg["loop_protection"] == "enable":
                    self.module.fail_json(
                        msg='Error: The interface has enable loop_protection, can not enable root_protection.')
                if self.root_protection != self.cur_cfg["root_protection"]:
                    if self.root_protection == "enable":
                        cmd = "stp root-protection"
                        cmds.append(cmd)
                        self.updates_cmd.append(cmd)
                        tmp_changed = True
                    else:
                        cmd = "undo stp root-protection"
                        cmds.append(cmd)
                        self.updates_cmd.append(cmd)
                        tmp_changed = True

            if self.loop_protection:
                if self.loop_protection == "enable" and self.cur_cfg["root_protection"] == "enable":
                    self.module.fail_json(
                        msg='Error: The interface has enable root_protection, can not enable loop_protection.')
                if self.loop_protection != self.cur_cfg["loop_protection"]:
                    if self.loop_protection == "enable":
                        cmd = "stp loop-protection"
                        cmds.append(cmd)
                        self.updates_cmd.append(cmd)
                        tmp_changed = True
                    else:
                        cmd = "undo stp loop-protection"
                        cmds.append(cmd)
                        self.updates_cmd.append(cmd)
                        tmp_changed = True

            if self.cost:
                if self.cost != self.cur_cfg["cost"]:
                    cmd = "stp cost %s" % self.cost
                    cmds.append(cmd)
                    self.updates_cmd.append(cmd)
                    tmp_changed = True

            if not tmp_changed:
                cmd = "interface %s" % self.interface
                self.updates_cmd.remove(cmd)
                cmds.remove(cmd)

        if cmds:
            self.cli_load_config(cmds)
            self.changed = True

    def absent_stp(self):
        """ Absent stp configuration """

        cmds = list()

        if self.stp_mode:
            if self.stp_mode == self.cur_cfg["stp_mode"]:
                if self.stp_mode != "mstp":
                    cmd = "undo stp mode"
                    cmds.append(cmd)
                    self.updates_cmd.append(cmd)
                    self.changed = True

        if self.stp_enable:
            if self.stp_enable != self.cur_cfg["stp_enable"]:
                cmd = "stp %s" % self.stp_enable
                cmds.append(cmd)
                self.updates_cmd.append(cmd)

        if self.stp_converge:
            if self.stp_converge == self.cur_cfg["stp_converge"]:
                cmd = "undo stp converge"
                cmds.append(cmd)
                self.updates_cmd.append(cmd)
                self.changed = True

        if self.edged_port:
            if self.interface == "all":
                if self.edged_port != self.cur_cfg["edged_port"]:
                    if self.edged_port == "enable":
                        cmd = "stp edged-port default"
                        cmds.append(cmd)
                        self.updates_cmd.append(cmd)
                    else:
                        cmd = "undo stp edged-port default"
                        cmds.append(cmd)
                        self.updates_cmd.append(cmd)

        if self.bpdu_filter:
            if self.interface == "all":
                if self.bpdu_filter != self.cur_cfg["bpdu_filter"]:
                    if self.bpdu_filter == "enable":
                        cmd = "stp bpdu-filter default"
                        cmds.append(cmd)
                        self.updates_cmd.append(cmd)
                    else:
                        cmd = "undo stp bpdu-filter default"
                        cmds.append(cmd)
                        self.updates_cmd.append(cmd)

        if self.bpdu_protection:
            if self.bpdu_protection != self.cur_cfg["bpdu_protection"]:
                if self.bpdu_protection == "enable":
                    cmd = "stp bpdu-protection"
                    cmds.append(cmd)
                    self.updates_cmd.append(cmd)
                else:
                    cmd = "undo stp bpdu-protection"
                    cmds.append(cmd)
                    self.updates_cmd.append(cmd)

        if self.tc_protection:
            if self.tc_protection != self.cur_cfg["tc_protection"]:
                if self.tc_protection == "enable":
                    cmd = "stp tc-protection"
                    cmds.append(cmd)
                    self.updates_cmd.append(cmd)
                else:
                    cmd = "undo stp tc-protection"
                    cmds.append(cmd)
                    self.updates_cmd.append(cmd)

        if self.tc_protection_interval:
            if self.tc_protection_interval == self.cur_cfg["tc_protection_interval"]:
                cmd = "undo stp tc-protection interval"
                cmds.append(cmd)
                self.updates_cmd.append(cmd)
                self.changed = True

        if self.tc_protection_threshold:

Answer: if self.tc_protection_threshold == self.cur_cfg["tc_protection_threshold"]:

<think>
In the absent_stp method, after handling tc_protection_interval, the next logical block handles tc_protection_threshold similarly. The existing line ends with 'if self.tc_protection_threshold:' and the next line should start the inner condition checking whether the current config matches the desired threshold, i.e., 'if self.tc_protection_threshold == self.cur_cfg["tc_protection_threshold"]:'.
</think>
if self.tc_protection_threshold == self.cur_cfg["tc_protection_threshold"]: