Search in sources :

Example 1 with RefFormat

use of io.swagger.models.refs.RefFormat in project swagger-parser by swagger-api.

the class ExternalRefProcessorTest method testProcessRefToExternalDefinition_NoNameConflict.

@Test
public void testProcessRefToExternalDefinition_NoNameConflict(@Injectable final Model mockedModel) throws Exception {
    final String ref = "http://my.company.com/path/to/file.json#/foo/bar";
    final RefFormat refFormat = RefFormat.URL;
    new StrictExpectations() {

        {
            cache.getRenamedRef(ref);
            times = 1;
            result = null;
            cache.loadRef(ref, refFormat, Model.class);
            times = 1;
            result = mockedModel;
            swagger.getDefinitions();
            times = 1;
            result = null;
            cache.putRenamedRef(ref, "bar");
            swagger.addDefinition("bar", mockedModel);
            times = 1;
            cache.addReferencedKey("bar");
            times = 1;
            result = null;
        }
    };
    String newRef = new ExternalRefProcessor(cache, swagger).processRefToExternalDefinition(ref, refFormat);
    assertEquals(newRef, "bar");
}
Also used : StrictExpectations(mockit.StrictExpectations) RefFormat(io.swagger.models.refs.RefFormat) Test(org.testng.annotations.Test)

Example 2 with RefFormat

use of io.swagger.models.refs.RefFormat in project swagger-parser by swagger-api.

the class ResolverCacheTest method testLoadExternalRef_NoDefinitionPath.

@Test
public void testLoadExternalRef_NoDefinitionPath(@Injectable final Model expectedResult) throws Exception {
    final RefFormat format = RefFormat.URL;
    final String ref = "http://my.company.com/path/to/file.json";
    final String contentsOfExternalFile = "really good json";
    new Expectations() {

        {
            RefUtils.readExternalUrlRef(ref, format, auths, "http://my.company.com/path/parent.json");
            times = 1;
            result = contentsOfExternalFile;
            DeserializationUtils.deserialize(contentsOfExternalFile, ref, Model.class);
            times = 1;
            result = expectedResult;
        }
    };
    ResolverCache cache = new ResolverCache(swagger, auths, "http://my.company.com/path/parent.json");
    Model firstActualResult = cache.loadRef(ref, RefFormat.URL, Model.class);
    assertEquals(contentsOfExternalFile, cache.getExternalFileCache().get(ref));
    assertEquals(expectedResult, cache.getResolutionCache().get(ref));
    assertEquals(expectedResult, firstActualResult);
    // requesting the same ref a second time should not result in reading the external file again
    Model secondActualResult = cache.loadRef(ref, format, Model.class);
    assertEquals(expectedResult, secondActualResult);
}
Also used : Expectations(mockit.Expectations) RefFormat(io.swagger.models.refs.RefFormat) Model(io.swagger.models.Model) Test(org.testng.annotations.Test)

Example 3 with RefFormat

use of io.swagger.models.refs.RefFormat in project swagger-parser by swagger-api.

the class ResolverCacheTest method testLoadExternalRefWithEscapedCharacters.

@Test
public void testLoadExternalRefWithEscapedCharacters() throws Exception {
    final RefFormat format = RefFormat.URL;
    final String ref = "http://my.company.com/path/to/main.yaml";
    final String contentsOfExternalFile = "swagger: \"2.0\"\n" + "\n" + "info:\n" + "  version: 1.0.0\n" + "  title: Path include test case child\n" + "\n" + "paths:\n" + "  /foo~bar~1:\n" + "    get:\n" + "      responses:\n" + "        200:\n" + "          description: \"Request successful\"\n";
    new Expectations() {

        {
            RefUtils.readExternalUrlRef(ref, format, auths, "http://my.company.com/path/parent.json");
            times = 1;
            result = contentsOfExternalFile;
        }
    };
    ResolverCache cache = new ResolverCache(swagger, auths, "http://my.company.com/path/parent.json");
    Path path = cache.loadRef(ref + "#/paths/~1foo~0bar~01", RefFormat.URL, Path.class);
    assertNotNull(path);
}
Also used : Expectations(mockit.Expectations) Path(io.swagger.models.Path) RefFormat(io.swagger.models.refs.RefFormat) Test(org.testng.annotations.Test)

Example 4 with RefFormat

use of io.swagger.models.refs.RefFormat in project swagger-parser by swagger-api.

the class ResolverCacheTest method testMock.

@Test
public void testMock(@Injectable final Model expectedResult) throws Exception {
    final RefFormat format = RefFormat.URL;
    final String ref = "http://my.company.com/path/to/file.json";
    final String contentsOfExternalFile = "really good json";
    new Expectations() {

        {
            RefUtils.readExternalUrlRef(ref, format, auths, "http://my.company.com/path/parent.json");
            times = 1;
            result = contentsOfExternalFile;
            DeserializationUtils.deserialize(contentsOfExternalFile, ref, Model.class);
            times = 1;
            result = expectedResult;
        }
    };
    ResolverCache cache = new ResolverCache(swagger, auths, "http://my.company.com/path/parent.json");
    Model firstActualResult = cache.loadRef(ref, RefFormat.URL, Model.class);
    assertEquals(firstActualResult, expectedResult);
}
Also used : Expectations(mockit.Expectations) RefFormat(io.swagger.models.refs.RefFormat) Model(io.swagger.models.Model) Test(org.testng.annotations.Test)

Example 5 with RefFormat

use of io.swagger.models.refs.RefFormat in project swagger-parser by swagger-api.

the class ExternalRefProcessorTest method testNestedExternalRefs.

@Test
public void testNestedExternalRefs(@Injectable final Model mockedModel) {
    final RefFormat refFormat = RefFormat.URL;
    // Swagger test instance
    Swagger testedSwagger = new Swagger();
    // Start with customer, add address property to it
    final String customerURL = "http://my.company.com/path/to/customer.json#/definitions/Customer";
    final Model customerModel = new ModelImpl();
    Map<String, Property> custProps = new HashMap<String, Property>();
    RefProperty address = new RefProperty();
    final String addressURL = "http://my.company.com/path/to/address.json#/definitions/Address";
    address.set$ref(addressURL);
    custProps.put("Address", address);
    // Create a 'local' reference to something in #/definitions, this should be ignored a no longer result in a null pointer exception
    final String loyaltyURL = "#/definitions/LoyaltyScheme";
    RefProperty loyaltyProp = new RefProperty();
    loyaltyProp.set$ref(loyaltyURL);
    loyaltyProp.setName("LoyaltyCardNumber");
    loyaltyProp.setRequired(true);
    custProps.put("Loyalty", loyaltyProp);
    customerModel.setProperties(custProps);
    // create address model, add Contact Ref Property to it
    final Model addressModel = new ModelImpl();
    Map<String, Property> addressProps = new HashMap<String, Property>();
    RefProperty contact = new RefProperty();
    final String contactURL = "http://my.company.com/path/to/Contact.json#/definitions/Contact";
    contact.set$ref(contactURL);
    addressProps.put("Contact", contact);
    addressModel.setProperties(addressProps);
    // Create contact model, with basic type property
    final Model contactModel = new ModelImpl();
    Property contactProp = new StringProperty();
    contactProp.setName("PhoneNumber");
    contactProp.setRequired(true);
    Map<String, Property> contactProps = new HashMap<String, Property>();
    contactProps.put("PhoneNumber", contactProp);
    contactModel.setProperties(contactProps);
    new Expectations() {

        {
            cache.loadRef(customerURL, refFormat, Model.class);
            result = customerModel;
            times = 1;
            cache.loadRef(addressURL, refFormat, Model.class);
            result = addressModel;
            times = 1;
            cache.loadRef(contactURL, refFormat, Model.class);
            result = contactModel;
            times = 1;
        }
    };
    String actualRef = new ExternalRefProcessor(cache, testedSwagger).processRefToExternalDefinition(customerURL, refFormat);
    assertTrue(testedSwagger.getDefinitions().get("Customer") != null);
    assertTrue(testedSwagger.getDefinitions().get("Contact") != null);
    assertTrue(testedSwagger.getDefinitions().get("Address") != null);
}
Also used : Expectations(mockit.Expectations) StrictExpectations(mockit.StrictExpectations) HashMap(java.util.HashMap) Swagger(io.swagger.models.Swagger) RefFormat(io.swagger.models.refs.RefFormat) Model(io.swagger.models.Model) StringProperty(io.swagger.models.properties.StringProperty) ModelImpl(io.swagger.models.ModelImpl) StringProperty(io.swagger.models.properties.StringProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) RefProperty(io.swagger.models.properties.RefProperty) Test(org.testng.annotations.Test)

Aggregations

RefFormat (io.swagger.models.refs.RefFormat)5 Test (org.testng.annotations.Test)5 Expectations (mockit.Expectations)4 Model (io.swagger.models.Model)3 StrictExpectations (mockit.StrictExpectations)2 ModelImpl (io.swagger.models.ModelImpl)1 Path (io.swagger.models.Path)1 Swagger (io.swagger.models.Swagger)1 Property (io.swagger.models.properties.Property)1 RefProperty (io.swagger.models.properties.RefProperty)1 StringProperty (io.swagger.models.properties.StringProperty)1 HashMap (java.util.HashMap)1