Search in sources :

Example 1 with Scanner

use of io.swagger.config.Scanner in project swagger-core by swagger-api.

the class SwaggerContextServiceTest method initializeAndGetScannerWithoutServletConfig.

@Test(description = "should add scanner to ScannerFactory")
public void initializeAndGetScannerWithoutServletConfig() {
    new SwaggerContextService().initScanner();
    new SwaggerContextService().initScanner();
    verify(servletConfig1, never()).getInitParameter(eq(SCANNER_ID_KEY));
    verify(servletContext1, never()).setAttribute(eq(SCANNER_ID_DEFAULT), any(Scanner.class));
    verify(servletContext1, never()).setAttribute(eq(SCANNER_ID_PREFIX + "test.1"), any(Scanner.class));
    verify(servletContext2, never()).setAttribute(eq(SCANNER_ID_PREFIX + "test.2"), any(Scanner.class));
    verify(servletConfig2, never()).getInitParameter(eq(SCANNER_ID_KEY));
    verify(servletContext2, never()).setAttribute(eq(SCANNER_ID_DEFAULT), any(Scanner.class));
    verify(servletContext2, never()).setAttribute(eq(SCANNER_ID_PREFIX + "test.2"), any(Scanner.class));
    verify(servletContext1, never()).setAttribute(eq(SCANNER_ID_PREFIX + "test.1"), any(Scanner.class));
    assertNotNull(ScannerFactory.getScanner());
    assertEquals(new SwaggerContextService().getScanner(), SwaggerScannerLocator.getInstance().getScanner(SCANNER_ID_DEFAULT));
}
Also used : SwaggerContextService(io.swagger.jaxrs.config.SwaggerContextService) DefaultJaxrsScanner(io.swagger.jaxrs.config.DefaultJaxrsScanner) Scanner(io.swagger.config.Scanner) Test(org.testng.annotations.Test)

Example 2 with Scanner

use of io.swagger.config.Scanner in project swagger-core by swagger-api.

the class SwaggerContextServiceTest method getScannerWithInitParam.

@Test(description = "should call servletContext getAttribute with param SCANNER_ID_PREFIX + \"test.1/2\"")
public void getScannerWithInitParam() {
    stubWithInitParam();
    Scanner scanner1 = new SwaggerContextService().withServletConfig(servletConfig1).getScanner();
    Scanner scanner2 = new SwaggerContextService().withServletConfig(servletConfig2).getScanner();
    assertEquals(beanConfigScanner1, scanner1);
    assertEquals(beanConfigScanner2, scanner2);
    verify(servletConfig1, times(2)).getInitParameter(eq(SCANNER_ID_KEY));
    verify(servletContext1, times(1)).getAttribute(eq(SCANNER_ID_PREFIX + "test.1"));
    verify(servletContext2, never()).getAttribute(eq(SCANNER_ID_PREFIX + "test.1"));
    verify(servletConfig2, times(2)).getInitParameter(eq(SCANNER_ID_KEY));
    verify(servletContext2, times(1)).getAttribute(eq(SCANNER_ID_PREFIX + "test.2"));
    verify(servletContext1, never()).getAttribute(eq(SCANNER_ID_PREFIX + "test.2"));
}
Also used : DefaultJaxrsScanner(io.swagger.jaxrs.config.DefaultJaxrsScanner) Scanner(io.swagger.config.Scanner) SwaggerContextService(io.swagger.jaxrs.config.SwaggerContextService) Test(org.testng.annotations.Test)

Example 3 with Scanner

use of io.swagger.config.Scanner in project swagger-core by swagger-api.

the class SwaggerScannerLocatorTest method putConfigSecondTime.

@Test(description = "shouldn't add given scanner to map because already set")
public void putConfigSecondTime() {
    putScannerFirstTime();
    Scanner scanner = new Scanner() {

        @Override
        public Set<Class<?>> classes() {
            return null;
        }

        @Override
        public boolean getPrettyPrint() {
            return false;
        }

        @Override
        public void setPrettyPrint(boolean shouldPrettyPrint) {
        }
    };
    SwaggerScannerLocator.getInstance().putScanner(id, scanner);
    assertNotEquals(SwaggerScannerLocator.getInstance().getScanner(id), scanner);
}
Also used : Scanner(io.swagger.config.Scanner) Test(org.testng.annotations.Test)

Example 4 with Scanner

use of io.swagger.config.Scanner in project swagger-core by swagger-api.

the class SwaggerScannerLocatorTest method putScannerFirstTime.

@Test(description = "should add given scanner to map ")
public void putScannerFirstTime() {
    Scanner scanner = new Scanner() {

        @Override
        public Set<Class<?>> classes() {
            return null;
        }

        @Override
        public boolean getPrettyPrint() {
            return false;
        }

        @Override
        public void setPrettyPrint(boolean shouldPrettyPrint) {
        }
    };
    SwaggerScannerLocator.getInstance().putScanner(id, scanner);
    assertEquals(SwaggerScannerLocator.getInstance().getScanner(id), scanner);
}
Also used : Scanner(io.swagger.config.Scanner) Test(org.testng.annotations.Test)

Example 5 with Scanner

use of io.swagger.config.Scanner in project swagger-core by swagger-api.

the class SwaggerContextService method getScanner.

public Scanner getScanner() {
    String scannerIdKey;
    Scanner value = null;
    if (scannerId != null) {
        scannerIdKey = SCANNER_ID_PREFIX + scannerId;
    } else if (contextId != null) {
        scannerIdKey = SCANNER_ID_PREFIX + contextId;
    } else {
        if (isServletConfigAvailable(sc)) {
            scannerIdKey = (sc.getInitParameter(SCANNER_ID_KEY) != null) ? SCANNER_ID_PREFIX + sc.getInitParameter(SCANNER_ID_KEY) : null;
            if (scannerIdKey == null) {
                boolean usePathBasedConfig = Boolean.valueOf(sc.getInitParameter(USE_PATH_BASED_CONFIG));
                if (usePathBasedConfig && StringUtils.isNotBlank(basePath)) {
                    scannerIdKey = SCANNER_ID_PREFIX + basePath;
                } else {
                    scannerIdKey = (sc.getInitParameter(CONTEXT_ID_KEY) != null) ? SCANNER_ID_PREFIX + sc.getInitParameter(CONTEXT_ID_KEY) : SCANNER_ID_DEFAULT;
                }
            }
            value = (Scanner) sc.getServletContext().getAttribute(scannerIdKey);
        } else {
            scannerIdKey = SCANNER_ID_DEFAULT;
        }
    }
    if (value != null) {
        return value;
    }
    value = SwaggerScannerLocator.getInstance().getScanner(scannerIdKey);
    if (value != null) {
        return value;
    }
    return ScannerFactory.getScanner();
}
Also used : Scanner(io.swagger.config.Scanner)

Aggregations

Scanner (io.swagger.config.Scanner)11 Test (org.testng.annotations.Test)5 SwaggerConfig (io.swagger.config.SwaggerConfig)4 SwaggerContextService (io.swagger.jaxrs.config.SwaggerContextService)4 Reader (io.swagger.jaxrs.Reader)3 DefaultJaxrsScanner (io.swagger.jaxrs.config.DefaultJaxrsScanner)3 Swagger (io.swagger.models.Swagger)3 JaxrsScanner (io.swagger.jaxrs.config.JaxrsScanner)1 ServletContext (javax.servlet.ServletContext)1