Skip to content

Commit

Permalink
Merge pull request #8318 from shawkins/master
Browse files Browse the repository at this point in the history
chore(dv) ENTESB-13076 adding a hook to reset dv
  • Loading branch information
shawkins committed Apr 24, 2020
2 parents 27a5153 + b533121 commit b20414a
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 10 deletions.
9 changes: 1 addition & 8 deletions app/dv/src/main/java/io/syndesis/dv/server/DvService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,10 @@
import io.syndesis.dv.RepositoryManager;
import io.syndesis.dv.utils.KLog;
/**
* A Komodo service implementation.
* A DV service implementation.
*/
public abstract class DvService implements V1Constants {

/**
* System user for transactions to be executed internally
*/
public static final String SYSTEM_USER_NAME = "SYSTEM";

public static final String ENCRYPTED_PREFIX = "ENCRYPTED-";

protected static final KLog LOGGER = KLog.getLogger();

/**
Expand Down
2 changes: 2 additions & 0 deletions app/dv/src/main/java/io/syndesis/dv/server/V1Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,6 @@ public static String version() {

String REVERT = "revert"; //$NON-NLS-1$

String TEST_SUPPORT = "test-support"; //$NON-NLS-1$

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.syndesis.dv.server.endpoint;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.syndesis.dv.model.DataVirtualization;
import io.syndesis.dv.server.DvService;
import io.syndesis.dv.server.V1Constants;

@RestController
@ConditionalOnProperty(value = "endpoints.test_support.enabled")
@RequestMapping( V1Constants.APP_PATH+V1Constants.FS+V1Constants.TEST_SUPPORT )
@Api( tags = {V1Constants.TEST_SUPPORT} )
public class TestSupportHandler extends DvService {

@Autowired
private DataVirtualizationService dataVirtualizationService;

@GetMapping(value = "/reset-db")
@ApiOperation(value = "Reset the persistent and running state.")
@ApiResponses(value = {
@ApiResponse(code = 400, message = "An error has occurred.")
})
public void resetDBToDefault() throws Exception {
Iterable<? extends DataVirtualization> virtualizations = repositoryManager.runInTransaction(true, ()->{
return repositoryManager.findDataVirtualizations();
});

for (DataVirtualization dv : virtualizations) {
try {
dataVirtualizationService.deletePublishedVirtualization(dv.getName());
dataVirtualizationService.deleteDataVirtualization(dv.getName());
} catch (Exception e) {
LOGGER.info("Could not delete virtualization %s", e, dv.getName()); //$NON-NLS-1$
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.syndesis.dv.server.endpoint;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import io.syndesis.dv.server.Application;
import io.syndesis.dv.server.endpoint.IntegrationTest.IntegrationTestConfiguration;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = {IntegrationTestConfiguration.class, Application.class})
@SuppressWarnings("nls")
public class IntegrationResetTest {

@Autowired
private TestRestTemplate restTemplate;

@Test
public void testReset() throws Exception {
RestDataVirtualization rdv = new RestDataVirtualization();
String dvName = "testPublish";
rdv.setName(dvName);
rdv.setDescription("description");

ResponseEntity<String> response = restTemplate.postForEntity(
"/v1/virtualizations", rdv, String.class);
assertEquals(HttpStatus.OK, response.getStatusCode());

ResponseEntity<List> virts = restTemplate.getForEntity("/v1/virtualizations", List.class);
assertEquals(HttpStatus.OK, virts.getStatusCode());
assertEquals(1, virts.getBody().size());

ResponseEntity<String> resetResponse = restTemplate.getForEntity("/v1/test-support/reset-db", String.class);
assertEquals(HttpStatus.OK, resetResponse.getStatusCode());

virts = restTemplate.getForEntity("/v1/virtualizations", List.class);
assertEquals(HttpStatus.OK, virts.getStatusCode());
assertEquals(0, virts.getBody().size());
}

}
2 changes: 2 additions & 0 deletions app/dv/src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ encrypt.key=foo

spring.flyway.baselineOnMigrate = true
spring.flyway.table = dv_schema_version

endpoints.test_support.enabled = true
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.syndesis.server.endpoint.v1.handler.tests;

import java.io.IOException;
import java.sql.DatabaseMetaData;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -40,6 +41,8 @@
import io.syndesis.server.dao.manager.DataAccessObject;
import io.syndesis.server.dao.manager.DataManager;
import io.syndesis.server.openshift.OpenShiftService;
import okhttp3.OkHttpClient;
import okhttp3.Request;

import org.skife.jdbi.v2.DBI;
import org.slf4j.Logger;
Expand Down Expand Up @@ -86,6 +89,8 @@ public void resetDBToDefault() {
deleteAllDBEntities();
startControllers();
dataMgr.resetDeploymentData();
resetDv();

LOG.warn("user {} reset the DB", user);
}

Expand Down Expand Up @@ -131,6 +136,18 @@ private void deleteAllDBEntities() {
cacheManager.evictAll();
}

private static void resetDv() {
final OkHttpClient httpClient = new OkHttpClient();
try (okhttp3.Response dvResponse = httpClient.newCall(new Request.Builder().get().url("http://syndesis-dv/dv/v1/test-support/reset-db").build()).execute()) {
if (!dvResponse.isSuccessful()) {
LOG.info("cannot reset dv - code {}", dvResponse.code());
}
} catch (IOException e) {
//more than likely it's not enabled
LOG.info("cannot reset dv", e);
}
}

private void startControllers() {
controllers.forEach(BackendController::start);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
value: {{.Syndesis.Components.Database.Name}}
- name: OPENSHIFT_MANAGEMENT_URL_FOR3SCALE
value: '{{.Syndesis.Components.Server.Features.ManagementUrlFor3scale}}'
- name: ENDPOINTS_TEST_SUPPORT_ENABLED
value: '{{ .Syndesis.Components.Server.Features.TestSupport }}'
{{if .DevSupport}}
- name: JAVA_DEBUG
value: "true"
Expand Down
4 changes: 2 additions & 2 deletions install/operator/pkg/generator/assets_vfsdata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b20414a

Please sign in to comment.