Java Code Examples for org.springframework.test.context.jdbc.Sql.ExecutionPhase#AFTER_TEST_METHOD

The following examples show how to use org.springframework.test.context.jdbc.Sql.ExecutionPhase#AFTER_TEST_METHOD . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AccessKeyRepositoryTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
@Sql(scripts = "/sql/accesskey-test.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/clean.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testFindFirst500ByDataChangeLastModifiedTimeGreaterThanOrderByDataChangeLastModifiedTime() {
  Instant instant = LocalDateTime.of(2019, 12, 19, 13, 44, 20)
      .atZone(ZoneId.systemDefault())
      .toInstant();
  Date date = Date.from(instant);

  List<AccessKey> accessKeyList = accessKeyRepository
      .findFirst500ByDataChangeLastModifiedTimeGreaterThanOrderByDataChangeLastModifiedTimeAsc(date);

  assertThat(accessKeyList).hasSize(2);
  assertThat(accessKeyList.get(0).getAppId()).isEqualTo("100004458");
  assertThat(accessKeyList.get(0).getSecret()).isEqualTo("4003c4d7783443dc9870932bebf3b7fe");
  assertThat(accessKeyList.get(1).getAppId()).isEqualTo("100004458");
  assertThat(accessKeyList.get(1).getSecret()).isEqualTo("c715cbc80fc44171b43732c3119c9456");
}
 
Example 2
Source File: ControllerIntegrationExceptionTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testCreateFailed() {
  AppDTO dto = generateSampleDTOData();

  when(adminService.createNewApp(any(App.class))).thenThrow(new RuntimeException("save failed"));

  try {
    restTemplate.postForEntity(getBaseAppUrl(), dto, AppDTO.class);
  } catch (HttpStatusCodeException e) {
    @SuppressWarnings("unchecked")
    Map<String, String> attr = gson.fromJson(e.getResponseBodyAsString(), Map.class);
    Assert.assertEquals("save failed", attr.get("message"));
  }
  App savedApp = appService.findOne(dto.getAppId());
  Assert.assertNull(savedApp);
}
 
Example 3
Source File: AppControllerTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testCheckIfAppIdUnique() {
  AppDTO dto = generateSampleDTOData();
  ResponseEntity<AppDTO> response =
      restTemplate.postForEntity(getBaseAppUrl(), dto, AppDTO.class);
  AppDTO result = response.getBody();
  Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
  Assert.assertEquals(dto.getAppId(), result.getAppId());
  Assert.assertTrue(result.getId() > 0);

  Boolean falseUnique =
      restTemplate.getForObject(getBaseAppUrl() + dto.getAppId() + "/unique", Boolean.class);
  Assert.assertFalse(falseUnique);
  Boolean trueUnique = restTemplate
      .getForObject(getBaseAppUrl() + dto.getAppId() + "true" + "/unique", Boolean.class);
  Assert.assertTrue(trueUnique);
}
 
Example 4
Source File: AppControllerTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testCreateTwice() {
  AppDTO dto = generateSampleDTOData();
  ResponseEntity<AppDTO> response =
      restTemplate.postForEntity(getBaseAppUrl(), dto, AppDTO.class);
  AppDTO first = response.getBody();
  Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
  Assert.assertEquals(dto.getAppId(), first.getAppId());
  Assert.assertTrue(first.getId() > 0);

  App savedApp = appRepository.findById(first.getId()).orElse(null);
  Assert.assertEquals(dto.getAppId(), savedApp.getAppId());
  Assert.assertNotNull(savedApp.getDataChangeCreatedTime());

  try {
    restTemplate.postForEntity(getBaseAppUrl(), dto, AppDTO.class);
  }catch (HttpClientErrorException e){
    Assert.assertEquals(HttpStatus.BAD_REQUEST, e.getStatusCode());
  }

}
 
Example 5
Source File: SpringDataBaseTest.java    From learning-code with Apache License 2.0 5 votes vote down vote up
@Test
@Sql(value = "classpath:drop.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testFindByUsername() {
  String username = "data.sql";
  String password = "123456";
  User user = userService.findByUsername(username);
  Assert.assertThat(user == null ? null : user.getPassword(), Matchers.equalTo(password));
}
 
Example 6
Source File: AccessKeyRepositoryTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
@Sql(scripts = "/sql/accesskey-test.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/clean.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testFindByAppId() {
  String appId = "someAppId";

  List<AccessKey> accessKeyList = accessKeyRepository.findByAppId(appId);

  assertThat(accessKeyList).hasSize(1);
  assertThat(accessKeyList.get(0).getAppId()).isEqualTo(appId);
  assertThat(accessKeyList.get(0).getSecret()).isEqualTo("someSecret");
}
 
Example 7
Source File: AppControllerTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testCreate() {
  AppDTO dto = generateSampleDTOData();
  ResponseEntity<AppDTO> response =
      restTemplate.postForEntity(getBaseAppUrl(), dto, AppDTO.class);
  AppDTO result = response.getBody();
  Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
  Assert.assertEquals(dto.getAppId(), result.getAppId());
  Assert.assertTrue(result.getId() > 0);

  App savedApp = appRepository.findById(result.getId()).orElse(null);
  Assert.assertEquals(dto.getAppId(), savedApp.getAppId());
  Assert.assertNotNull(savedApp.getDataChangeCreatedTime());
}
 
Example 8
Source File: AppControllerTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testFind() {
  AppDTO dto = generateSampleDTOData();
  App app = BeanUtils.transform(App.class, dto);
  app = appRepository.save(app);

  AppDTO result = restTemplate.getForObject(getBaseAppUrl() + dto.getAppId(), AppDTO.class);
  Assert.assertEquals(dto.getAppId(), result.getAppId());
  Assert.assertEquals(dto.getName(), result.getName());
}
 
Example 9
Source File: AppControllerTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testDelete() {
  AppDTO dto = generateSampleDTOData();
  App app = BeanUtils.transform(App.class, dto);
  app = appRepository.save(app);

  restTemplate.delete("http://localhost:{port}/apps/{appId}?operator={operator}", port, app.getAppId(), "test");

  App deletedApp = appRepository.findById(app.getId()).orElse(null);
  Assert.assertNull(deletedApp);
}
 
Example 10
Source File: AppControllerTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void shouldFailedWhenAppIdIsInvalid() {
  AppDTO dto = generateSampleDTOData();
  dto.setAppId("invalid app id");
  try {
    restTemplate.postForEntity(getBaseAppUrl(), dto, String.class);
    Assert.fail("Should throw");
  } catch (HttpClientErrorException e) {
    Assert.assertEquals(HttpStatus.BAD_REQUEST, e.getStatusCode());
    Assert.assertThat(new String(e.getResponseBodyAsByteArray()), containsString(InputValidator.INVALID_CLUSTER_NAMESPACE_MESSAGE));
  }
}
 
Example 11
Source File: ReleaseControllerTest.java    From apollo with Apache License 2.0 4 votes vote down vote up
@Test
@Sql(scripts = "/controller/test-release.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testReleaseBuild() {
  String appId = "someAppId";
  AppDTO app =
      restTemplate.getForObject("http://localhost:" + port + "/apps/" + appId, AppDTO.class);

  ClusterDTO cluster = restTemplate.getForObject(
      "http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/default",
      ClusterDTO.class);

  NamespaceDTO namespace =
      restTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId()
          + "/clusters/" + cluster.getName() + "/namespaces/application", NamespaceDTO.class);

  Assert.assertEquals("someAppId", app.getAppId());
  Assert.assertEquals("default", cluster.getName());
  Assert.assertEquals("application", namespace.getNamespaceName());

  ItemDTO[] items =
      restTemplate.getForObject(
          "http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/"
              + cluster.getName() + "/namespaces/" + namespace.getNamespaceName() + "/items",
          ItemDTO[].class);
  Assert.assertEquals(3, items.length);

  HttpHeaders headers = new HttpHeaders();
  headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
  MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>();
  parameters.add("name", "someReleaseName");
  parameters.add("comment", "someComment");
  parameters.add("operator", "test");
  HttpEntity<MultiValueMap<String, String>> entity =
      new HttpEntity<MultiValueMap<String, String>>(parameters, headers);
  ResponseEntity<ReleaseDTO> response = restTemplate.postForEntity(
      "http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName()
          + "/namespaces/" + namespace.getNamespaceName() + "/releases",
      entity, ReleaseDTO.class);
  Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
  ReleaseDTO release = response.getBody();
  Assert.assertEquals("someReleaseName", release.getName());
  Assert.assertEquals("someComment", release.getComment());
  Assert.assertEquals("someAppId", release.getAppId());
  Assert.assertEquals("default", release.getClusterName());
  Assert.assertEquals("application", release.getNamespaceName());

  Map<String, String> configurations = new HashMap<String, String>();
  configurations.put("k1", "v1");
  configurations.put("k2", "v2");
  configurations.put("k3", "v3");
  Gson gson = new Gson();
  Assert.assertEquals(gson.toJson(configurations), release.getConfigurations());
}
 
Example 12
Source File: ItemSetControllerTest.java    From apollo with Apache License 2.0 4 votes vote down vote up
@Test
@Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testItemSetCreated() {
  String appId = "someAppId";
  AppDTO app =
      restTemplate.getForObject("http://localhost:" + port + "/apps/" + appId, AppDTO.class);

  ClusterDTO cluster = restTemplate.getForObject(
      "http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/default",
      ClusterDTO.class);

  NamespaceDTO namespace =
      restTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId()
          + "/clusters/" + cluster.getName() + "/namespaces/application", NamespaceDTO.class);

  Assert.assertEquals("someAppId", app.getAppId());
  Assert.assertEquals("default", cluster.getName());
  Assert.assertEquals("application", namespace.getNamespaceName());

  ItemChangeSets itemSet = new ItemChangeSets();
  itemSet.setDataChangeLastModifiedBy("created");
  RestTemplate createdTemplate = (new TestRestTemplate()).getRestTemplate();
  createdTemplate.setMessageConverters(restTemplate.getMessageConverters());
  
  int createdSize = 3;
  for (int i = 0; i < createdSize; i++) {
    ItemDTO item = new ItemDTO();
    item.setNamespaceId(namespace.getId());
    item.setKey("key_" + i);
    item.setValue("created_value_" + i);
    itemSet.addCreateItem(item);
  }

  ResponseEntity<Void> response =
      createdTemplate.postForEntity(
          "http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/"
              + cluster.getName() + "/namespaces/" + namespace.getNamespaceName() + "/itemset",
          itemSet, Void.class);
  Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
  List<Item> items = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespace.getId());
  Assert.assertEquals(createdSize, items.size());
  Item item0 = items.get(0);
  Assert.assertEquals("key_0", item0.getKey());
  Assert.assertEquals("created_value_0", item0.getValue());
  Assert.assertEquals("created", item0.getDataChangeCreatedBy());
  Assert.assertNotNull(item0.getDataChangeCreatedTime());
}
 
Example 13
Source File: ItemSetControllerTest.java    From apollo with Apache License 2.0 4 votes vote down vote up
@Test
@Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testItemSetUpdated() {
  String appId = "someAppId";
  AppDTO app =
      restTemplate.getForObject("http://localhost:" + port + "/apps/" + appId, AppDTO.class);

  ClusterDTO cluster = restTemplate.getForObject(
      "http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/default",
      ClusterDTO.class);

  NamespaceDTO namespace =
      restTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId()
          + "/clusters/" + cluster.getName() + "/namespaces/application", NamespaceDTO.class);

  Assert.assertEquals("someAppId", app.getAppId());
  Assert.assertEquals("default", cluster.getName());
  Assert.assertEquals("application", namespace.getNamespaceName());

  ItemChangeSets createChangeSet = new ItemChangeSets();
  createChangeSet.setDataChangeLastModifiedBy("created");
  RestTemplate createdRestTemplate = (new TestRestTemplate()).getRestTemplate();
  createdRestTemplate.setMessageConverters(restTemplate.getMessageConverters());
  
  int createdSize = 3;
  for (int i = 0; i < createdSize; i++) {
    ItemDTO item = new ItemDTO();
    item.setNamespaceId(namespace.getId());
    item.setKey("key_" + i);
    item.setValue("created_value_" + i);
    createChangeSet.addCreateItem(item);
  }

  ResponseEntity<Void> response = createdRestTemplate.postForEntity(
      "http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName()
          + "/namespaces/" + namespace.getNamespaceName() + "/itemset",
      createChangeSet, Void.class);
  Assert.assertEquals(HttpStatus.OK, response.getStatusCode());

  ItemDTO[] items =
      createdRestTemplate.getForObject(
          "http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/"
              + cluster.getName() + "/namespaces/" + namespace.getNamespaceName() + "/items",
          ItemDTO[].class);

  ItemChangeSets updateChangeSet = new ItemChangeSets();
  updateChangeSet.setDataChangeLastModifiedBy("updated");

  RestTemplate updatedRestTemplate = (new TestRestTemplate()).getRestTemplate();
  updatedRestTemplate.setMessageConverters(restTemplate.getMessageConverters());
  
  int updatedSize = 2;
  for (int i = 0; i < updatedSize; i++) {
    items[i].setValue("updated_value_" + i);
    updateChangeSet.addUpdateItem(items[i]);
  }

  response = updatedRestTemplate.postForEntity(
      "http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName()
          + "/namespaces/" + namespace.getNamespaceName() + "/itemset",
      updateChangeSet, Void.class);
  Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
  List<Item> savedItems = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespace.getId());
  Assert.assertEquals(createdSize, savedItems.size());
  Item item0 = savedItems.get(0);
  Assert.assertEquals("key_0", item0.getKey());
  Assert.assertEquals("updated_value_0", item0.getValue());
  Assert.assertEquals("created", item0.getDataChangeCreatedBy());
  Assert.assertEquals("updated", item0.getDataChangeLastModifiedBy());
  Assert.assertNotNull(item0.getDataChangeCreatedTime());
  Assert.assertNotNull(item0.getDataChangeLastModifiedTime());
}
 
Example 14
Source File: ItemSetControllerTest.java    From apollo with Apache License 2.0 4 votes vote down vote up
@Test
@Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testItemSetDeleted() {
  String appId = "someAppId";
  AppDTO app =
      restTemplate.getForObject("http://localhost:" + port + "/apps/" + appId, AppDTO.class);

  ClusterDTO cluster = restTemplate.getForObject(
      "http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/default",
      ClusterDTO.class);

  NamespaceDTO namespace =
      restTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId()
          + "/clusters/" + cluster.getName() + "/namespaces/application", NamespaceDTO.class);

  Assert.assertEquals("someAppId", app.getAppId());
  Assert.assertEquals("default", cluster.getName());
  Assert.assertEquals("application", namespace.getNamespaceName());

  ItemChangeSets createChangeSet = new ItemChangeSets();
  createChangeSet.setDataChangeLastModifiedBy("created");
  RestTemplate createdTemplate = (new TestRestTemplate()).getRestTemplate();
  createdTemplate.setMessageConverters(restTemplate.getMessageConverters());
  
  int createdSize = 3;
  for (int i = 0; i < createdSize; i++) {
    ItemDTO item = new ItemDTO();
    item.setNamespaceId(namespace.getId());
    item.setKey("key_" + i);
    item.setValue("created_value_" + i);
    createChangeSet.addCreateItem(item);
  }

  ResponseEntity<Void> response = createdTemplate.postForEntity(
      "http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName()
          + "/namespaces/" + namespace.getNamespaceName() + "/itemset",
      createChangeSet, Void.class);
  Assert.assertEquals(HttpStatus.OK, response.getStatusCode());

  ItemDTO[] items =
      restTemplate.getForObject(
          "http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/"
              + cluster.getName() + "/namespaces/" + namespace.getNamespaceName() + "/items",
          ItemDTO[].class);

  ItemChangeSets deleteChangeSet = new ItemChangeSets();
  deleteChangeSet.setDataChangeLastModifiedBy("deleted");
  RestTemplate deletedTemplate = (new TestRestTemplate()).getRestTemplate();
  deletedTemplate.setMessageConverters(restTemplate.getMessageConverters());
  
  int deletedSize = 1;
  for (int i = 0; i < deletedSize; i++) {
    items[i].setValue("deleted_value_" + i);
    deleteChangeSet.addDeleteItem(items[i]);
  }

  response = deletedTemplate.postForEntity(
      "http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName()
          + "/namespaces/" + namespace.getNamespaceName() + "/itemset",
      deleteChangeSet, Void.class);
  Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
  List<Item> savedItems = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespace.getId());
  Assert.assertEquals(createdSize - deletedSize, savedItems.size());
  Item item0 = savedItems.get(0);
  Assert.assertEquals("key_1", item0.getKey());
  Assert.assertEquals("created_value_1", item0.getValue());
  Assert.assertEquals("created", item0.getDataChangeCreatedBy());
  Assert.assertNotNull(item0.getDataChangeCreatedTime());
}
 
Example 15
Source File: AppControllerTest.java    From apollo with Apache License 2.0 4 votes vote down vote up
@Test(expected = HttpClientErrorException.class)
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testFindNotExist() {
  restTemplate.getForEntity(getBaseAppUrl() + "notExists", AppDTO.class);
}